00001
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
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
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
00185 if( typeid(T) != typeid(prop->val) )
00186 throw GraphExMsg(invalid_property_type, std::string("The actual typeid's differ ! ") );
00187
00188
00189
00190
00191
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
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
00228 PropertyMap::iterator itmap = map->find(pname);
00229 if( itmap == map->end() )
00230 throw GraphEx(invalid_property);
00231
00232
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
00239 if( typeid(T) != typeid(prop->val) )
00240 throw GraphExMsg(invalid_property_type, std::string("The actual typeid's differ ! ") );
00241
00242
00243
00244
00245
00246
00247 return prop->val;
00248 };
00249
00250
00251 }
00252
00253 #endif