src/work/deba/map_registry.h
changeset 676 7ec5e7e6c7b4
parent 627 6cc21a9c9fda
child 701 c03e073b8394
equal deleted inserted replaced
3:65b68a0d195c 4:151b7e8528ac
     2 #define MAP_REGISTRY_H
     2 #define MAP_REGISTRY_H
     3 
     3 
     4 #include <vector>
     4 #include <vector>
     5 
     5 
     6 using namespace std;
     6 using namespace std;
       
     7 
       
     8 namespace hugo {
     7 
     9 
     8 /** 
    10 /** 
     9     Registry class to register edge or node maps in the graph. The
    11     Registry class to register edge or node maps in the graph. The
    10     registry helps you to implement an observer pattern. If you add
    12     registry helps you to implement an observer pattern. If you add
    11     or erase an edge or node you must notify all the maps about the
    13     or erase an edge or node you must notify all the maps about the
    12     event.
    14     event.
    13 */
    15 */
    14 
       
    15 namespace hugo {
       
    16 
       
    17   template <typename G, typename K, typename KIt>
    16   template <typename G, typename K, typename KIt>
    18   class MapRegistry {
    17   class MapRegistry {
    19   public:
    18   public:
    20     typedef G Graph;
    19     typedef G Graph;
    21     typedef K Key;
    20     typedef K Key;
    22     typedef KIt KeyIt;
    21     typedef KIt KeyIt;
    23 	
    22 	
    24 
    23 
    25 
    24 
       
    25     ///. 
       
    26 
       
    27     ///. 
       
    28     /// 
    26     class MapBase {
    29     class MapBase {
    27     public:
    30     public:
    28       typedef G Graph;
    31       typedef G Graph;
    29       typedef MapRegistry<G, K, KIt> Registry;
    32       typedef MapRegistry<G, K, KIt> Registry;
    30       typedef K Key;
    33       typedef K Key;
   135     Container container;
   138     Container container;
   136 
   139 
   137 		
   140 		
   138     public:
   141     public:
   139 	
   142 	
       
   143     ///. 
   140     MapRegistry() {}
   144     MapRegistry() {}
   141 	
   145 	
       
   146     ///.
   142     MapRegistry(const MapRegistry&) {}
   147     MapRegistry(const MapRegistry&) {}
   143 		
   148 		
       
   149     ///.
   144     MapRegistry& operator=(const MapRegistry&) {
   150     MapRegistry& operator=(const MapRegistry&) {
   145       for (it = container.begin(); it != container.end(); ++it) {
   151       for (it = container.begin(); it != container.end(); ++it) {
   146 	(*it)->destroy();
   152 	(*it)->destroy();
   147 	(*it)->graph = 0;
   153 	(*it)->graph = 0;
   148 	(*it)->registry = 0;
   154 	(*it)->registry = 0;
   149       }
   155       }
   150     }
   156     }
   151 				
   157 				
       
   158     ///.
   152     ~MapRegistry() {
   159     ~MapRegistry() {
   153       typename Container::iterator it;
   160       typename Container::iterator it;
   154       for (it = container.begin(); it != container.end(); ++it) {
   161       for (it = container.begin(); it != container.end(); ++it) {
   155 	(*it)->destroy();
   162 	(*it)->destroy();
   156 	(*it)->registry = 0;
   163 	(*it)->registry = 0;
   159     }
   166     }
   160 	
   167 	
   161 	
   168 	
   162     public:
   169     public:
   163 	
   170 	
       
   171     ///.
   164     void attach(MapBase& map) {
   172     void attach(MapBase& map) {
   165       if (map.registry) {
   173       if (map.registry) {
   166 	map.registry->detach(map);
   174 	map.registry->detach(map);
   167       }
   175       }
   168       container.push_back(&map);
   176       container.push_back(&map);
   169       map.registry = this;
   177       map.registry = this;
   170       map.registry_index = container.size()-1;
   178       map.registry_index = container.size()-1;
   171     } 
   179     } 
   172 	
   180 	
       
   181     ///.
   173     void detach(MapBase& map) {
   182     void detach(MapBase& map) {
   174       container.back()->registry_index = map.registry_index; 
   183       container.back()->registry_index = map.registry_index; 
   175       container[map.registry_index] = container.back();
   184       container[map.registry_index] = container.back();
   176       container.pop_back();
   185       container.pop_back();
   177       map.registry = 0;
   186       map.registry = 0;
   178       map.graph = 0;
   187       map.graph = 0;
   179     }
   188     }
   180 	
   189 	
   181 		
   190 		
       
   191     ///. 
   182     virtual void add(Key& key) {
   192     virtual void add(Key& key) {
   183       typename Container::iterator it;
   193       typename Container::iterator it;
   184       for (it = container.begin(); it != container.end(); ++it) {
   194       for (it = container.begin(); it != container.end(); ++it) {
   185 	(*it)->add(key);
   195 	(*it)->add(key);
   186       }
   196       }
   187     }	
   197     }	
   188 		
   198 		
       
   199     ///.
   189     virtual void erase(Key& key) {
   200     virtual void erase(Key& key) {
   190       typename Container::iterator it;
   201       typename Container::iterator it;
   191       for (it = container.begin(); it != container.end(); ++it) {
   202       for (it = container.begin(); it != container.end(); ++it) {
   192 	(*it)->erase(key);
   203 	(*it)->erase(key);
   193       }
   204       }