473,385 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Define a mapping from class types to strings?

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_f rom_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?).

Nov 21 '07 #1
5 2540
alan 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?
Yes, it most certainly is. I would probably use 'typeid' for that.
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_f rom_runtime);
}
No, you can have your string_mapper template specialised for any of
the involved types. Give them the operator const char* (which will
return the static const pointer) and they should convert very
easily to a char const* like you show.
>
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);}
I don't think there is a need to call 'set' here. Just create
a template specialisation for every type you know/can think of.
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 21 '07 #2
On Nov 21, 9:30 pm, alan <almkg...@gmail.comwrote:
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_f rom_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<Tfor 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?
Nov 21 '07 #3
On Nov 21, 2:30 pm, alan <almkg...@gmail.comwrote:
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>();
Something like typeid(thetype).name() ?
However I do not know the string to be associated with the
type at compile time,
In which case, you probably want to use std::string, instead of
char const*. This means some sort of map, with the values read
in at runtime. Possibly an std::map< std::type_info const*,
std::string >, but you still have the problem of initialization.

The real question is where the names are coming from: the format
of the source data, and how the type associated with the name is
specified. External files can't contain std::type_info, only
text. And std::type_info::name() isn't portable (although if
you're only targeting one compiler, and that compiler does
something useful with this function, you may be able to use it).
What you'll probably have to do is define some sort of canonical
name, and map it to the user defined name (using std::map<
std::string, std::string >, for example).
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_f rom_runtime);
}
If you can do that, then you can use std::type_info as your key
type in the map. Or rather std::type_info const*, as a
type_info isn't copiable. You'll have to provide a comparison
function, of course, based on type_info::before().

I'm rather wondering, however, how you establish the initial
mapping which allows you to call this function.
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;}
I think a class template would be simpler, with two different
functions, and a static class member.
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?).
The static member is in a separate template class, so a new
instance will be created for each instantiation of the template.
The capability is independent of the target class, and will even
work for built-in types.

Something like:

template< typename T >
class StringMapper
{
public:
explicit StringMapper( std::string const& name )
{
ourName = name ;
}

operator std::string() const
{
return ourName ;
}

operator char const*() const
{
return ourName.c_str() ;
}

private:
static std::string ourName ;
} ;

template< typename T >
std::string StringMapper< T >::ourName = std::string() ;

The basic problem remains: how to you find the mapping to start
with.

(I still prefer the solution with std::map, since this allows
you to find the typename of an object, as well as a that of a
statically known type.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 22 '07 #4
On Nov 22, 5:22 pm, James Kanze <james.ka...@gmail.comwrote:
On Nov 21, 2:30 pm, alan <almkg...@gmail.comwrote:
Hello world, I'm wondering if it's possible to implement some sort of
class/object that can performmappingfrom class types to strings?
I will know the classtypeat compile time, like so:
const char *s = string_mapper<thetype>();

Something like typeid(thetype).name() ?
However I do not know the string to be associated with the
typeat compile time,

In which case, you probably want to use std::string, instead of
char const*. This means some sort of map, with the values read
in at runtime. Possibly an std::map< std::type_info const*,
std::string >, but you still have the problem of initialization.

The real question is where the names are coming from: the format
of the source data, and how thetypeassociated with the name is
specified. External files can't contain std::type_info, only
text. And std::type_info::name() isn't portable (although if
you're only targeting one compiler, and that compiler does
something useful with this function, you may be able to use it).
What you'll probably have to do is define some sort of canonical
name, and map it to the user defined name (using std::map<
std::string, std::string >, for example).
From the user, as a string. The class associated to the string is
determined from an object of that class. An object of that class will
provide a method that will allow querying and setting the string.
>
and will need a way to set up themapping, to be created
at run time, possibly like so:
void foo(char* some_string_from_runtime){
string_mapper_obj<thetype>.setstring(some_string_f rom_runtime);
}

If you can do that, then you can use std::type_info as your keytypein the map. Or rather std::type_info const*, as a
type_info isn't copiable. You'll have to provide a comparison
function, of course, based on type_info::before().

I'm rather wondering, however, how you establish the initialmappingwhich allows you to call this function.
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;}

I think a class template would be simpler, with two different
functions, and a static class member.
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 basetype) 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?).

The static member is in a separate template class, so a new
instance will be created for each instantiation of the template.
The capability is independent of the target class, and will even
work for built-in types.

Something like:

template< typename T >
class StringMapper
{
public:
explicit StringMapper( std::string const& name )
{
ourName = name ;
}

operator std::string() const
{
return ourName ;
}

operator char const*() const
{
return ourName.c_str() ;
}

private:
static std::string ourName ;
} ;

template< typename T >
std::string StringMapper< T >::ourName = std::string() ;

The basic problem remains: how to you find themappingto start
with.
The user-defined class call name defaults to an empty string.

In the course of the game, when the user picks up an item (generated
by the game) he or she may use a command to call the item, so that all
other items of the same class (generated and not yet generated) will
have the same call name, even if the item is consumed somehow.

So the mapping is created by the item object itself; any object of a
class can change the mapping of that object to the class. And there
will be several classes of that item, each with this ability.

So basically, the item object itself must provide a method that will
change the class name. However, items are passed around as smart
pointers to an abstract base class; therefore, the base class must
itself provide the ability to change the call name.

Here's a solution I've thought up:
template<class T>
//I will use std::string in final code.
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_get(){
return string_mapper_base<T>(false,0);
}
template<class T>
inline const char* string_mapper_set(const char* set_to){
return string_mapper_base<T>(true, set_to);
}
class item{
public:
virtual const char* get_call_name()const=0;
virtual const char* set_call_name(const char* c)=0;
virtual void do_action()=0;
virtual void flip_something()=0;
};

template<class T>
class item_class: public item{
public:
virtual const char* get_call_name() const{
return string_mapper_get<T>();
}
virtual const char* set_call_name(const char* c){
return string_mapper_set<T>(c);
}
}

class knife: item_class<knife>{ //works in g++, but not 100% sure if
this is legal
public:
bool dull;
virtual void do_action(){
if(dull){
std::cout <<"Doing an action with a dull
knife!"<<std::endl;
} else {
std::cout << "Some nasty slicing action!" <<
std::endl;
}
}
virtual void flip_something(){
dull = true;
}
}
int
main(){
item *k1 = new knife; //I will use smart pointers in final
code.
item *k2 = new knife;
k2->flip_something();
k1->do_action();
k2->do_action();
k1->set_call_name("Some sort of knife");
std::cout << k1->get_call_name() << std::endl;
std::cout << k2->get_call_name() << std::endl;
k2->set_call_name("A renamed sort of knife");
std::cout << k1->get_call_name() << std::endl;
std::cout << k2->get_call_name() << std::endl;
delete: k1; delete k2;
k1->do_action();
k2->do_action();
}

I'm aware that std::string is much superior to char*, but this is just
a test program I hacked together to see if the concept, by itself, was
feasible (it would very well work with integers or bools, for that
matter). I'll use std::string in actual code.

Anyway I'll probably have to define mappings not only to strings, but
also to other data types - for example I'll need a bool that will
inform me if the player has already identified a class of items. This
bool can be queried by any item of that class, and will be set if the
player, for example, reads a scroll of identify and selects any item
of that class; again, any individual object of that class can set the
identified flag, and any individual object of that class must be able
to determine if its class is already identified.

Additionally, I *think* it's possible to put the dorky
string_mapper_base() function into the item_class class; the
item_class will simply define a private static string that can be
referred directly by its get_call_name() and set_call_name() methods.

Victor Bazarov wrote:
>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);}

I don't think there is a need to call 'set' here. Just create
a template specialisation for every type you know/can think of.
Sorry, but I have two constraints:
1. The string associated with the type must be changeable by the user
at run time.
2. I want to make it easy to add new classes in the future that will
be able to provide this feature without repeating code.

I'll look into the typeid thing; I also need to serialize the data
into a save file, so I'll need a way to map a class name to a unique
string, and a reversed map to associate that unique string to a class
that will generate an item of that class.

Finally, thanks to you both for replies; I was wondering why no one
bothered to reply.
Nov 22 '07 #5
On Nov 22, 7:39 pm, alan <almkg...@gmail.comwrote:
The user-defined class call name defaults to an empty string.

In the course of the game, when the user picks up an item (generated
by the game) he or she may use a command to call the item, so that all
other items of the same class (generated and not yet generated) will
have the same call name, even if the item is consumed somehow.

So the mapping is created by the item object itself; any object of a
class can change the mapping of that object to the class. And there
will be several classes of that item, each with this ability.
Dang; my brain is not working properly. Should be:
So the mapping is created by the item object itself; any object of a
class can change the mapping of that class to any arbitrary string.
And there will be several item classes, each with this ability.
>
So basically, the item object itself must provide a method that will
change the class name. However, items are passed around as smart
pointers to an abstract base class; therefore, the base class must
itself provide the ability to change the call name.

Here's a solution I've thought up:
I have since modified the solution so that the data is privately
stored in the item_class<template, although it does require
initializing the static member separately as a template (a little like
in James Kanze's code, except the data member can be mutated by
virtual functions derived from the base class, item, and I don't
create casts to string and const char*).

So I can use this way for both user-called names and in-game
identification; I just have to figure out how to do randomized
appearances properly (like, "puce potion" is a healing_potion in one
game, but sleeping_potion in another). I may have to build an object/
templated object just for randomized appearances.

Nov 22 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Tim Henderson | last post by:
Hi i have some errors in my code that i can't find the reason for here is the error i have 3 classes: -Song -Album -Artist Song has 3 pieces of data with getter and setter meathods: -Name...
9
by: Jerry Sievers | last post by:
Fellow Pythonists; I am totally puzzled on the use of slicing on mapping types and especially unsure on use of the Ellipsis... and slicing syntax that has two or more groups seperated by comma....
0
by: Chris | last post by:
I am using Castor to marshal and unmharshal some objects. I have an object that has a List of strings. I can marshal it without using a mapping file. However, when I try to unmarshal it without...
2
by: mars | last post by:
Alexandrescu(2000) advance a method about mapping intergral constants to types like this: template<int v> struct Int2Type { enum { value = v }; }; Why cann't use like this: template<int v>...
2
by: Kenny | last post by:
Can someone point me to details about mapping various MSSQL server data types to C# data types, particular "bit" and timestamp etc. etc. Strings/varchar work fine.. Thanks, KAC -- Kenny A....
1
by: yuri | last post by:
Hello, Is it possible to create a binding that would map an input message with a part defined as a complex-type element to a url-encoded string? For example, wsdl file defines a message as ...
11
by: Frank Silvermann | last post by:
#define q p I must admit to a most elementary confusion. I have only ever used #define to, in point of fact, #redefine . The above is standard. Is the following: #define...
20
by: tshad | last post by:
Using VS 2003, I am trying to take a class that I created to create new variable types to handle nulls and track changes to standard variable types. This is for use with database variables. This...
13
by: Andreas Eibach | last post by:
Hi, let's say I have this: #include <string.h> #define BLAH "foo" Later on, I do this:
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.