A
Aaron Walker
Hey folks,
I've got some code for an options class that stores user run time options (as
static members) so that they are availably any time an instance is created;
currently the code has a set_OPTION get_OPTION kind of function interface, but
as you can imagine that can get quite large after a while.
I've never messed with templates before, but this seemed like a good
opportunity to learn. The code (that doesn't compile) is below, but my main
question is in regards to the std::map I create (arg 2). I'd like the map to
be able to hold any kind of option_type, but options_T cannot be a template
(otherwise it'd hold all option_type<T>'s, not any kind).
<code>
template<typename T >
class option_type
{
public:
T value;
option_type& operator=(const T &v)
{ value = v; return *this; }
};
class options_T
{
protected:
static std::map<std::string, option_type > optmap;
static option_type<bool > _verbose;
static option_type<std::string > _user;
/* more opts of different types */
public:
options_T();
template<typename T > void set_value(std::string const &id,
T value) { optmap[id] = value; }
template<typename T > T get_value(std::string const &id)
{ return optmap[id].value; }
};
</code>
default option values are set in the source file, and the constructor is solely
for inserting the id's/option_type's into optmap.
Now, I know I cannot do it the way it is above, but is there anyway to specify
optmap as being able to hold option_type of any type? The only thing I can
think of is using option_type<void * > but I'd *really* like to avoid doing
that if possible.
Any comments, suggestions would be very appreciated.
Cheers
I've got some code for an options class that stores user run time options (as
static members) so that they are availably any time an instance is created;
currently the code has a set_OPTION get_OPTION kind of function interface, but
as you can imagine that can get quite large after a while.
I've never messed with templates before, but this seemed like a good
opportunity to learn. The code (that doesn't compile) is below, but my main
question is in regards to the std::map I create (arg 2). I'd like the map to
be able to hold any kind of option_type, but options_T cannot be a template
(otherwise it'd hold all option_type<T>'s, not any kind).
<code>
template<typename T >
class option_type
{
public:
T value;
option_type& operator=(const T &v)
{ value = v; return *this; }
};
class options_T
{
protected:
static std::map<std::string, option_type > optmap;
static option_type<bool > _verbose;
static option_type<std::string > _user;
/* more opts of different types */
public:
options_T();
template<typename T > void set_value(std::string const &id,
T value) { optmap[id] = value; }
template<typename T > T get_value(std::string const &id)
{ return optmap[id].value; }
};
</code>
default option values are set in the source file, and the constructor is solely
for inserting the id's/option_type's into optmap.
Now, I know I cannot do it the way it is above, but is there anyway to specify
optmap as being able to hold option_type of any type? The only thing I can
think of is using option_type<void * > but I'd *really* like to avoid doing
that if possible.
Any comments, suggestions would be very appreciated.
Cheers