templates

G

graham

hi ,

I'm written a template for my applications options. Options are like
this;

-option1 stringValue -option2 23.5 -option3 -option3 'c'

my "options" template is parameterized by a type (std::string, double
and char above), depending on how its instantiated <T = string, double
or char> it will have a T value that corresponds to option1, option2
or option3 above. This is ok so far.

I need an OptionManager to "manage" my Options and to have the data
each option holds returned. I need the "get" method of each Option
type below to be usable in a generic fashion.

However , I want my clients to be able to do somthing like this

double dValue;
OptionManager->get("-option2", d);

or

std::string str
OptionManager->get("option1" , str);

etc. etc.

For this I need to store my Options in a container and have a method
exposed. Howeve I can't see how I can achieve this.

The following code probably explains it better. I need the "get"
method to be useable to clients.. I cant store Options in an stl
container unless I use pointers. Once I have a pointer, I cannot call
the get method cos I don't have a base class which defines get for all
types.


template<typename T>
class Option :
{

public:
Option(const std::vector<std::string> args, const std::string&
optionName) : m_option_name(optionName)
{
std::vector<std::string>::const_iterator iter =
std::find(args.begin(), args.end(), optionName);
if (iter!=args.end() && iter < (args.end() -1))
{
std::istringstream iss( *++iter);
iss >> m_option_value;
broken = !iss.good();

}
}

void get(T& t)
{
if (broken)
{
throw std::runtime_error("bad config value");
}

t = m_option_value;
}


T m_option_value;
std::string m_option_name;
bool broken;


};

Could anybody help me out?

thanks and have a nice evening.

G
 
J

Jeff Flinn

Leigh said:
hi ,

I'm written a template for my applications options. Options are like
this;
[...]
Could anybody help me out?

thanks and have a nice evening.

Take a look at boost.variant; it will allow you to have a single
container for all your option types without you having to use inheritance.

And while he's there he can take a look at boost.program_options. ;-)

See: http://www.boost.org/doc/libs/1_44_0/doc/html/program_options.html

Jeff
 
G

graham

Leigh said:
hi ,
I'm written a template for my applications options. Options are like
this;
[...]
Could anybody help me out?
thanks and have a nice evening.
Take a look at boost.variant; it will allow you to have a single
container for all your option types without you having to use inheritance.

And while he's there he can take a look at boost.program_options. ;-)

See:http://www.boost.org/doc/libs/1_44_0/doc/html/program_options.html

Jeff

thanks Jeff and Leigh but boost is out of the question. I have to code
this up as simply as possible on my own. Ive tried pushing boost but
its not allowed in this shop.. don't shoot the messenger :)

Is there a relatively simple approach available? I really want to do
it with templates and any techniques that are out there...

thanks again,

Graham
 
G

graham

Leigh said:
hi ,
I'm written a template for my applications options. Options are like
this;
[...]
Could anybody help me out?
thanks and have a nice evening.
Take a look at boost.variant; it will allow you to have a single
container for all your option types without you having to use inheritance.

And while he's there he can take a look at boost.program_options. ;-)

See:http://www.boost.org/doc/libs/1_44_0/doc/html/program_options.html

Jeff

Just read this again... "without you having to use inheritance." ..
does this imply its possible *using* inheritance but without boost? If
I'm interested

thanks again

G
 
G

graham

It seems you are trying to use the templates in the wrong place. All
command-line options are strings initially, so you can easily store them in
a container of strings. When the later code queries for them as of a
certain type, then you can have a template function for converting the
string to the needed type, something along the lines:

template<typename T>
void OptionManager::get(const std::string& name, T& value);

hth
Paavo

Thanks for the reply. It's as much a learning exercise as a real
scenario. I'm going to quick finish it as you mentioned and try
implement leighs invariant suggestion in the background.

Thanks for replies.

G
 
J

Jorgen Grahn

Leigh said:
On 26/04/2011 17:30, graham wrote:
hi ,
I'm written a template for my applications options. Options are like
this;
[...]

Could anybody help me out?
thanks and have a nice evening.
Take a look at boost.variant; it will allow you to have a single
container for all your option types without you having to use inheritance.

And while he's there he can take a look at boost.program_options. ;-)

See:http://www.boost.org/doc/libs/1_44_0/doc/html/program_options.html

Jeff

thanks Jeff and Leigh but boost is out of the question. I have to code
this up as simply as possible on my own.

Personally I find that using the OS's mechanism (getopt(3) on Unix in
my case) is the simplest. Unless you have to write dozens of
similar parsers, writing your own template-based one is almost
certainly a waste of time. (Unless it's an exercise, but that doesn't
seem to be the case here.)

You have to do the conversion from string to e.g. integer yourself,
but IMO you often have to do application logic things here like "-f
and -g are mutually exclusive, unless -o is also given".

By the way, the example you gave:
-option1 stringValue -option2 23.5 -option3 -option3 'c'
looks really old-fashioned if you're on Unix. "Long options"
are written --option1 these days, not -option1.

/Jorgen
 
G

graham

Leigh Johnston wrote:
On 26/04/2011 17:30, graham wrote:
hi ,
I'm written a template for my applications options. Options are like
this;
[...]
Could anybody help me out?
thanks and have a nice evening.
Take a look at boost.variant; it will allow you to have a single
container for all your option types without you having to use inheritance.
And while he's there he can take a look at boost.program_options. ;-)
See:http://www.boost.org/doc/libs/1_44_0/doc/html/program_options.html
Jeff
thanks Jeff and Leigh but boost is out of the question. I have to code
this up as simply as possible on my own.

Personally I find that using the OS's mechanism (getopt(3) on Unix in
my case) is the simplest. Unless you have to write dozens of
similar parsers, writing your own template-based one is almost
certainly a waste of time. (Unless it's an exercise, but that doesn't
seem to be the case here.)

You have to do the conversion from string to e.g. integer yourself,
but IMO you often have to do application logic things here like "-f
and -g are mutually exclusive, unless -o is also given".

By the way, the example you gave:
  -option1 stringValue -option2 23.5 -option3  -option3 'c'
looks really old-fashioned if you're on Unix. "Long options"
are written --option1 these days, not -option1.

/Jorgen

Thanks for that reply,

I'm sorta trying to ramp up on templates when coding all this.
Don't need any of the template stuff ultimately... but its interesting
stuff so
I'm going that route ;)

What's the best book out there on template (meta) programming?

Cheers,

Graham
 
G

graham

Leigh Johnston wrote:
On 26/04/2011 17:30, graham wrote:
hi ,
I'm written a template for my applications options. Options are like
this;
[...]
Could anybody help me out?
thanks and have a nice evening.
Take a look at boost.variant; it will allow you to have a single
container for all your option types without you having to use inheritance.
And while he's there he can take a look at boost.program_options. ;-)
See:http://www.boost.org/doc/libs/1_44_0/doc/html/program_options.html
Jeff
thanks Jeff and Leigh but boost is out of the question. I have to code
this up as simply as possible on my own.

Personally I find that using the OS's mechanism (getopt(3) on Unix in
my case) is the simplest. Unless you have to write dozens of
similar parsers, writing your own template-based one is almost
certainly a waste of time. (Unless it's an exercise, but that doesn't
seem to be the case here.)

You have to do the conversion from string to e.g. integer yourself,
but IMO you often have to do application logic things here like "-f
and -g are mutually exclusive, unless -o is also given".

By the way, the example you gave:
  -option1 stringValue -option2 23.5 -option3  -option3 'c'
looks really old-fashioned if you're on Unix. "Long options"
are written --option1 these days, not -option1.

/Jorgen

Thanks for that reply,

I'm sorta trying to ramp up on templates when coding all this.
Don't need any of the template stuff ultimately... but its interesting
stuff so
I'm going that route ;)

What's the best book out there on template (meta) programming?

Cheers,

Graham
 
R

Ruben Safir

graham said:
Leigh Johnston wrote:
On 26/04/2011 17:30, graham wrote:
hi ,
I'm written a template for my applications options. Options are like
this;

Could anybody help me out?
thanks and have a nice evening.
Take a look at boost.variant; it will allow you to have a single
container for all your option types without you having to use inheritance.
And while he's there he can take a look at boost.program_options. ;-)


thanks Jeff and Leigh but boost is out of the question. I have to code
this up as simply as possible on my own.

Personally I find that using the OS's mechanism (getopt(3) on Unix in
my case) is the simplest. Unless you have to write dozens of
similar parsers, writing your own template-based one is almost
certainly a waste of time. (Unless it's an exercise, but that doesn't
seem to be the case here.)

You have to do the conversion from string to e.g. integer yourself,
but IMO you often have to do application logic things here like "-f
and -g are mutually exclusive, unless -o is also given".

By the way, the example you gave:
  -option1 stringValue -option2 23.5 -option3  -option3 'c'
looks really old-fashioned if you're on Unix. "Long options"
are written --option1 these days, not -option1.

/Jorgen

Thanks for that reply,

I'm sorta trying to ramp up on templates when coding all this.
Don't need any of the template stuff ultimately... but its interesting
stuff so
I'm going that route ;)

What's the best book out there on template (meta) programming?

http://www.josuttis.com/tmplbook/

Cheers,

Graham
 
K

Krice

Don't need any of the template stuff ultimately... but its interesting
stuff so I'm going that route ;)

When (if, in case you end up flipping burgers) you become a better
programmer you learn to avoid anything extra that is not really
needed. I would start by asking myself do I really need sucky
command line options or is there a better way to store the
options my program is using?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top