Re: Define a mapping from class types to strings?
On Nov 21, 9:30 pm, alan <almkg...@gmail.com> wrote:
Hello world, I'm wondering if it's possible to implement some sort of
class/object that can perform mapping from class types to strings?
Dang, is this so simple that it's not even worth a comment? How *is*
this supposed to be done?
I will know the class type at compile time, like so:
const char *s = string_mapper<thetype>();
However I do not know the string to be associated with the type at
compile time, and will need a way to set up the mapping, to be created
at run time, possibly like so:
void foo(char* some_string_from_runtime){
string_mapper_obj<thetype>.setstring(some_string_from_runtime);
}
Possibly I might want to also mix up the string mappings too, haha,
but I suppose I can do that before they even reach the string mapper.
Maybe something crazy like this? Would this work? (I'm purposefully
glossing over the memory management concerns)
template<class T>
const char* string_mapper_base(bool set, const char* set_to){
static const char* thestring;
if(set) internal = set_to;
return thestring;}
template<class T>
inline const char* string_mapper(){ return string_mapper_base(false,
0);}
template<class T>
inline const char* string_mapper_set(const char* set_to){return
string_mapper_base(true, set_to);}
The above seems to work, except s/string_mapper_base/
string_mapper_base<T>/ in the later two functions.
Which then gives me the problem, how can I make a base class such that
it will be able to get the correct string_mapper_base<T> for derived
classes. I think I'll need a helper class that I'll have to
instantiate (possibly by hand, oh well) for each derived class.
Alternatively I might have to just use template classes. Currently
I'm using inheritance from a base object (so that I can have a list of
objects of various classes by simply getting a vector of pointers to
base classes). Could templates be made to work?