00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifndef _moot_ENUM_H
00030 #define _moot_ENUM_H
00031
00032 #include <mootTypes.h>
00033
00034 using namespace std;
00035 using namespace moot_STL_NAMESPACE;
00036
00040 typedef unsigned int mootEnumID;
00041
00045 const mootEnumID mootEnumNone = 0;
00046
00059 template <class NameType,
00060 class NameHashFcn = hash <NameType>,
00061 class NameEqualFcn = equal_to <NameType> >
00062 class mootEnum {
00063 public:
00064
00066 typedef hash_map<NameType,mootEnumID,NameHashFcn,NameEqualFcn> Name2IdMap;
00067
00069 typedef vector<NameType> Id2NameMap;
00070
00071 public:
00072
00073 Name2IdMap names2ids;
00074 Id2NameMap ids2names;
00075
00076 public:
00078 mootEnum(void)
00079 {
00080 ids2names.push_back(NameType());
00081 };
00082
00084 mootEnum(const NameType &unknownName)
00085 {
00086 ids2names.push_back(unknownName);
00087 };
00088
00090 ~mootEnum(void)
00091 {
00092 clear();
00093 };
00094
00095
00097 inline void unknown_name(const NameType &name)
00098 {
00099 names2ids[name] = 0;
00100 ids2names[0] = name;
00101 };
00102
00103
00105 inline bool nameExists(const NameType &name) const
00106 {
00107 return names2ids.find(name) != names2ids.end();
00108 };
00109
00111 inline bool idExists(const mootEnumID id) const
00112 {
00113 return id && ids2names.size() > id;
00114 };
00115
00116
00117
00119 inline mootEnumID size(void) const
00120 {
00121 return ids2names.size();
00122 };
00123
00125 inline mootEnumID name2id(const NameType &name) const
00126 {
00127 typename Name2IdMap::const_iterator i = names2ids.find(name);
00128 return i == names2ids.end() ? 0 : i->second;
00129 };
00130
00135 inline const NameType &id2name(const mootEnumID id) const
00136 {
00137 return ids2names.size() <= id ? ids2names[0] : ids2names[id];
00138 };
00139
00140
00141
00147 inline mootEnumID insert(const NameType &name, mootEnumID id=0)
00148 {
00149 if (!id) id = ids2names.size();
00150 if (ids2names.size() <= id) ids2names.resize(id+1);
00151 ids2names[id] = name;
00152 names2ids[name] = id;
00153 return id;
00154 };
00155
00157 inline void clear(void)
00158 {
00159 names2ids.clear();
00160 ids2names.resize(1);
00161 };
00162 };
00163
00164 #endif