A
alan
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?
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);}
---
What I'm trying to do is set up a class of objects so that they have a
possibly-user-defined name. All objects will then be able to access
that name and report to the user that their name is so-and-so.
Worse, I would like to keep this open in the future so that new
classes of objects (inherited from a base type) will automatically
have this ability too (so static member data doesn't work too well,
because I'd then have to retype that code - I think?).
class/object that can perform mapping from class types to strings?
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);}
---
What I'm trying to do is set up a class of objects so that they have a
possibly-user-defined name. All objects will then be able to access
that name and report to the user that their name is so-and-so.
Worse, I would like to keep this open in the future so that new
classes of objects (inherited from a base type) will automatically
have this ability too (so static member data doesn't work too well,
because I'd then have to retype that code - I think?).