Connecting Tech Pros Worldwide Forums | Help | Site Map

different types in the same map

Member
 
Join Date: Jul 2007
Posts: 41
#1: Jul 24 '07
Hi, trying to add scripting support for a game i'm making.
I'd like to be able to add an arbitrary type to any object in the game, and i'm using a base class with maps to hold the types. Currently a have a map for every known type (bool, int, float, double and some templates), but i'd like to have just one map to hold them all. This is what i got so far:
Expand|Select|Wrap|Line Numbers
  1. //adds a variable to the map with with the name 'name' and the value 'value'
  2. template <typename T>
  3. void add(const char* name, T value)
  4. {
  5.      if( typeid(T) == typeid(int) )
  6.         ints[name] = value;
  7.  
  8.      if( typeid(T) == typeid(float) )
  9.         floats[name] = value;
  10.  
  11.     //ect..
  12. }
  13.  
ints and floats are maps of the type
Expand|Select|Wrap|Line Numbers
  1. std::map<const char*, int>
  2. std::map<const char*, float>
  3.  
but i'd like to have a function like this
Expand|Select|Wrap|Line Numbers
  1. template <typename T>
  2. void add(const char* name, T value)
  3. {
  4.     attributes[name] = value;
  5. }
  6.  
but i can't figure out how the map should be declared or how to get things out of the map without knowing the type.

Is this at all understandable? Does anyone have an idear for a solution?
Should i just settle with have to have a map for every type?

Member
 
Join Date: Jul 2007
Posts: 41
#2: Jul 24 '07

re: different types in the same map


Ok, no need to answer. I'v settled with the one map per type solution
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,382
#3: Jul 24 '07

re: different types in the same map


Quote:

Originally Posted by JonLT

//adds a variable to the map with with the name 'name' and the value 'value'
template <typename T>
void add(const char* name, T value)
{
if( typeid(T) == typeid(int) )
ints[name] = value;

if( typeid(T) == typeid(float) )
floats[name] = value;

//ect..
}

Try not to do this. This is not what typeid is for plus every time you have a new T you have to change this template.

Your map is a map of pairs and probably would be declared as:

Expand|Select|Wrap|Line Numbers
  1. map<char*, T> attributes;
  2.  
Member
 
Join Date: Jul 2007
Posts: 41
#4: Jul 24 '07

re: different types in the same map


just to update a bit this is what i'm doing (left the typeid thing)
Expand|Select|Wrap|Line Numbers
  1. template <class T>
  2. class AttributeMap
  3. {
  4.     public:
  5.  
  6.         //! Gets a value from the map passed
  7.         static T get(const std::map<const char*, T>& map, const char* name)
  8.         {
  9.             T tmp;
  10.             typename std::map<const char*, T>::const_iterator it;
  11.             for(it = map.begin(); it != map.end() ; it++)
  12.             {
  13.                 if(it->first == name) //value found in map
  14.                     tmp = it->second;
  15.             }
  16.  
  17.             return tmp;
  18.         }
  19. };
  20.  
and when i want to get something from a map i use it like this
Expand|Select|Wrap|Line Numbers
  1. AttributeMap<float>::get(floatMap, floatName);
  2.  
where float map is a std::map<const char*, float> and floatName is the key (a c-style string)

and when a add something to a map i do the usual
Expand|Select|Wrap|Line Numbers
  1. floatMap[floatName] = value
  2.  
This works fine, althoug my compiler tells me that "tmp might be used uninitialized in this function"
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,382
#5: Jul 25 '07

re: different types in the same map


Quote:

Originally Posted by JonLT

This works fine, althoug my compiler tells me that "tmp might be used uninitialized in this function"

Yes. That's because tmp is declared and not initialized. There is no guarantee that the loop will be entered. If so, the function returns the uninitialized tmp.

This warning is based on the type used to specialize the function template. In this case a float. Were you to use a class that had a default constructor and copy constructor, you would get no warning.
Reply