00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef TJINDEX_H
00019 #define TJINDEX_H
00020
00021 #include <tjutils/tjutils.h>
00022 #include <tjutils/tjstatic.h>
00023 #include <tjutils/tjlabel.h>
00024 #include <tjutils/tjhandler.h>
00025
00026
00027
00034 class Index {
00035 public:
00036 static const char* get_compName();
00037 };
00038
00039
00041
00042
00043 struct UniqueIndexMap : public STD_map<STD_string, STD_list<unsigned int> >, public Labeled {
00044
00045 unsigned int get_index(STD_list<unsigned int>::iterator& index, const STD_string& type, unsigned int max_instances);
00046
00047 void remove_index(const STD_list<unsigned int>::iterator& index, const STD_string& type);
00048
00049
00050 private:
00051 bool contiguous;
00052 unsigned int assign_index(STD_list<unsigned int>::iterator& index, const STD_string& type);
00053 };
00054
00055
00057
00058
00059 class UniqueIndexBase : public StaticHandler<UniqueIndexBase> {
00060
00061 public:
00062
00063
00064 static void init_static() {indices_map.init("indices_map");}
00065 static void destroy_static() {indices_map.destroy();}
00066
00067 protected:
00068
00069 UniqueIndexBase() {}
00070
00071 static SingletonHandler<UniqueIndexMap,true> indices_map;
00072
00073 };
00074
00075
00077
00078
00091 template<class T>
00092 class UniqueIndex : public UniqueIndexBase {
00093
00094 public:
00095 UniqueIndex() {init();}
00096
00097 UniqueIndex(const UniqueIndex<T>&) {init();}
00098
00099 UniqueIndex<T>& operator = (const UniqueIndex<T>&) {erase(); init(); return *this;}
00100
00101 ~UniqueIndex() {erase();}
00102
00103
00107 unsigned int get_index() const {
00108 return indices_map->get_index(index, T::get_typename(), T::get_max_instances());
00109 }
00110
00111
00112 private:
00113 void init() {
00114 index=indices_map->operator[](T::get_typename()).end();
00115 }
00116
00117 void erase() {
00118 indices_map->remove_index(index, T::get_typename());
00119 }
00120
00121
00122 mutable STD_list<unsigned int>::iterator index;
00123
00124
00125 };
00126
00129 #endif