Main Page | Class Hierarchy | Data Structures | File List | Data Fields | Globals

PropertyFacility.hpp

Go to the documentation of this file.
00001 // vim:sw=2:ts=8
00017 #ifndef PROPERTIES__HPP
00018 #define PROPERTIES__HPP
00019 
00020 #include <string>
00021 #include <iterator>
00022 
00023 #include "def_graph.hpp"
00024 #include "GraphException.hpp"
00025 #include "Property.hpp"
00026 
00027 
00028 
00029 namespace gns {
00030 
00031 
00068   class PropertyFacility
00069   {
00070     public:
00072       typedef std::map<std::string, AbstractProperty*> PropertyMap;
00074       typedef std::hash_map<std::string, std::pair<int, PropertyMap*> > Map;
00075 
00076     private:
00078       Map *_m;
00079 
00081       int users;
00082 
00083 
00084     public:
00086       PropertyFacility();
00088       PropertyFacility(const PropertyFacility &p);
00089 
00091       ~PropertyFacility();
00092 
00094       void register_user();
00095 
00097       void unregister_user();
00098 
00100       int user_count();
00101 
00103       void add_id(std::string id);
00104 
00106       void del_id(std::string id);
00107 
00109       template <class T>
00110         void set(std::string id, std::string pname, T val);
00111 
00113       template <class T>
00114         T get(std::string id, std::string pname);
00115 
00117       bool isset(std::string id, std::string pname);
00118 
00120       void unset(std::string id, std::string pname);
00121 
00123       void clone(std::string id1, std::string id2, PropertyFacility* prop2 = NULL);
00124 
00125 
00127       typedef PropertyMap::iterator PropertyIterator;
00128  
00130       PropertyIterator begin(std::string id);
00132       PropertyIterator end(std::string id);
00133   };
00134 
00135 
00160   template <class T>
00161     void PropertyFacility::set(std::string id, std::string pname, T val)
00162     {
00163       Map::iterator it = _m->find(id);
00164       if( it == _m->end() )
00165         throw GraphExMsg(invalid_id, std::string("ID: ")+id);
00166 
00167       PropertyMap *map = (*it).second.second;
00168       PropertyMap::iterator itmap = map->find(pname);
00169       if( itmap == map->end() )
00170       {
00171         // Create new property and insert it
00172         Property<T> *newp = new Property<T>(val);
00173         AbstractProperty *newap = dynamic_cast<AbstractProperty*>(newp);
00174         map->insert(std::make_pair(pname, newap));
00175       }
00176       else
00177       {
00178         // Chance value of already existing property
00179         AbstractProperty *ap = (*itmap).second;
00180 
00181         if( typeid(T).name() != ap->get_type() )
00182           throw GraphExMsg(invalid_property_type, typeid(T).name() + std::string(" != ") + ap->get_type() );
00183         Property<T> *prop =  static_cast<Property<T>*>(ap);
00184         // 2.check: makes sense ?
00185         if( typeid(T) != typeid(prop->val) )
00186           throw GraphExMsg(invalid_property_type, std::string("The actual typeid's differ ! ") );
00187 
00188         /*
00189         Property<T> *prop = dynamic_cast<Property<T>*>(ap);
00190         if( prop == NULL )
00191           throw GraphEx(invalid_property_type);
00192         */
00193         prop->val = val;
00194       }
00195     };
00196 
00197 
00217   template <class T>
00218     T PropertyFacility::get(std::string id, std::string pname)
00219     {
00220       // Find node property map
00221       Map::iterator it = _m->find(id);
00222       if( it == _m->end() )
00223         throw GraphExMsg(invalid_id, std::string("ID: ") + id);
00224 
00225       PropertyMap *map = (*it).second.second;
00226 
00227       // Find property
00228       PropertyMap::iterator itmap = map->find(pname);
00229       if( itmap == map->end() )
00230         throw GraphEx(invalid_property);
00231 
00232       // Get property
00233       AbstractProperty *ap = (*itmap).second;
00234 
00235       if( typeid(T).name() != ap->get_type() )
00236         throw GraphExMsg(invalid_property_type, typeid(T).name() + std::string(" != ") + ap->get_type() );
00237       Property<T> *prop = static_cast<Property<T>*>(ap);
00238       // 2.check
00239       if( typeid(T) != typeid(prop->val) )
00240         throw GraphExMsg(invalid_property_type, std::string("The actual typeid's differ ! ") );
00241 
00242       /*
00243       Property<T> *prop = dynamic_cast<Property<T>*>(ap);
00244       if( prop == NULL )
00245         throw GraphEx(invalid_property_type);
00246        */
00247       return prop->val;
00248     };
00249 
00250 
00251 }
00252 
00253 #endif

Generated on Sun Nov 5 12:05:19 2006 for Graph by  doxygen 1.4.1