473,404 Members | 2,178 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,404 software developers and data experts.

different types in the same map

41
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?
Jul 24 '07 #1
4 2487
JonLT
41
Ok, no need to answer. I'v settled with the one map per type solution
Jul 24 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
//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.  
Jul 24 '07 #3
JonLT
41
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"
Jul 24 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
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.
Jul 25 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: alg | last post by:
Is it possible to put different types of objects in a single STL list? If not what STL template should be used which will contains various objects of different types? Thanks for your help!
2
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't...
4
by: troloo | last post by:
Hello, I hope you can help me :)) The story goes as follows: I have a class with different methods and member variables. I store pointers to objects of this class inside a vector. Now, I would like...
2
by: Che | last post by:
Greetings, I am writing an application that uses an extendible XML file. in order to validate that XML I use a main XSD and in additional - few more extensions XSD's that uses the types in the...
6
by: Moshe Kravchik | last post by:
Hi all! I have 2 web services, one writtenin C++ (ATL) and another one in C#. Is there a way to define data stuctures in a single place both services could use? The structures are the same, but if...
20
by: pinkfloydhomer | last post by:
Is it well-defined and portable to do something like: typedef struct { int type; char c; } S1; typedef struct {
3
by: WaterWalk | last post by:
I read from c99 std TC2 community draft, and found the following statement in 6.7.2.3: "Tw o declarations of structure, union, or enumerated types which are in different scopes or use different...
2
by: Frederick Gotham | last post by:
I just want to clarify my understanding of arithmetic and comparison between two different integer types. Phase (1): Integer Promotion ---------- All of the following types always get...
8
by: Born Bugler | last post by:
What I'm actually talking about is, when you put the same class in different assemblies, you get two different types. This is reasonable (if you would call it) in most cases as it avoids possible...
15
by: Juha Nieminen | last post by:
I'm sure this is not a new idea, but I have never heard about it before. I'm wondering if this could work: Assume that you have a common base class and a bunch of classes derived from it, and...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.