different types in the same map

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JonLT
    New Member
    • Jul 2007
    • 41

    #1

    different types in the same map

    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:
    Code:
    //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..
    }
    ints and floats are maps of the type
    Code:
    std::map<const char*, int>
    std::map<const char*, float>
    but i'd like to have a function like this
    Code:
    template <typename T>
    void add(const char* name, T value)
    {
        attributes[name] = value;
    }
    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?
  • JonLT
    New Member
    • Jul 2007
    • 41

    #2
    Ok, no need to answer. I'v settled with the one map per type solution

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      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:

      [code=cpp]
      map<char*, T> attributes;
      [/code]

      Comment

      • JonLT
        New Member
        • Jul 2007
        • 41

        #4
        just to update a bit this is what i'm doing (left the typeid thing)
        [CODE=cpp]
        template <class T>
        class AttributeMap
        {
        public:

        //! Gets a value from the map passed
        static T get(const std::map<const char*, T>& map, const char* name)
        {
        T tmp;
        typename std::map<const char*, T>::const_itera tor it;
        for(it = map.begin(); it != map.end() ; it++)
        {
        if(it->first == name) //value found in map
        tmp = it->second;
        }

        return tmp;
        }
        };
        [/CODE]
        and when i want to get something from a map i use it like this
        [CODE=cpp]
        AttributeMap<fl oat>::get(float Map, floatName);
        [/CODE]
        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
        [CODE=cpp]
        floatMap[floatName] = value
        [/CODE]

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

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          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.

          Comment

          Working...