Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members

typemap.h

00001 // Copyright (C) 2001 Jean-Marc Valin
00002 #ifndef TYPE_MAP_H
00003 #define TYPE_MAP_H
00004 
00005 #ifdef _MSC_VER
00006 #pragma warning (disable: 4786)
00007 #endif
00008 
00009 #include <algorithm>
00010 #include <map>
00011 #include <typeinfo>
00012 
00013 
00014 //Yes, this is yet another Visual C++ bug. It seems like the type_info::before() 
00015 //method is buggy, or it could be the STL map, I don't know. All I know is that
00016 //the follwing code doesn't work under MSVC++. That's why there's a (ugly) workaround.
00017 #ifndef WIN32
00018 
00019 struct compare_const_type_info_ptr : public std::binary_function<const std::type_info *, const std::type_info *, bool>
00020 {
00021    bool operator()(const std::type_info *lhs, const std::type_info *rhs) const
00022    {
00023       return lhs->before(*rhs);
00024    }
00025 };
00026 
00027 template<typename T>
00028 class TypeMap : public std::map<const std::type_info *, T, compare_const_type_info_ptr >
00029 {
00030 };
00031 
00032 #else
00033 
00034 
00035 #include <vector>
00036 
00037 
00038 template<typename T>
00039 class TypeMap {
00040   public:
00041    typedef std::pair<const type_info *, T> element;
00042    typedef element *iterator;
00043    typedef const element *const_iterator;
00044   private:
00045    std::vector<element> tmap;
00046   public:
00047    
00048    const_iterator begin() const {return tmap.begin();}
00049    iterator begin() {return tmap.begin();}
00050 
00051    const_iterator end() const {return tmap.end();}
00052    iterator end() {return tmap.end();}
00053 
00054    iterator find(const type_info *t) 
00055    {
00056       for (int i=0;i<tmap.size();i++)
00057          if (*t == *(tmap[i].first))
00058             return &tmap[i];
00059       return end();
00060    }
00061 
00062    const_iterator find(const type_info *t) const
00063    {
00064       for (int i=0;i<tmap.size();i++)
00065          if (*t == *(tmap[i].first))
00066             return &tmap[i];
00067       return end();
00068    }
00069 
00070    T &operator[] (const type_info *t) 
00071    {
00072       iterator it = find(t);
00073       if (it != end())
00074          return it->second;
00075       else
00076       {
00077          element it;
00078          it.first = t;
00079          tmap.push_back(it);
00080          return tmap[tmap.size()-1].second;
00081       }
00082    }
00083 
00084    
00085 };
00086 
00087 
00088 
00089 #endif
00090 
00091 
00092 #endif

Generated on Wed Oct 5 14:28:56 2005 for FlowDesigner by  doxygen 1.4.4