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
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