00001 #ifndef s11n_PROPERTY_SERVER_H_INCLUDED
00002 #define s11n_PROPERTY_SERVER_H_INCLUDED 1
00003 #include <string>
00004
00005 #include <s11n/to_string.h>
00006 #include <s11n/phoenix.h>
00007
00008 namespace s11n {
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 template <typename ContextType, typename ObjIDType = ContextType *, typename KeyType = std::string>
00023 class property_server
00024 {
00025 public:
00026
00027
00028
00029 typedef ContextType context_type;
00030
00031
00032 typedef ObjIDType object_id_type;
00033
00034
00035 typedef KeyType key_type;
00036
00037
00038 typedef std::map<key_type,std::string> property_map_type;
00039
00040
00041
00042
00043
00044 template <typename ValueType>
00045 static void set( const object_id_type objid, const key_type & key, const ValueType & val )
00046 {
00047 object_map()[objid][key] = s11n::to_string( val );
00048 }
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062 template <typename ValueType>
00063 static ValueType get( const object_id_type objid, const key_type & key, const ValueType & defaultval )
00064 {
00065 typename object_map_type::const_iterator it = object_map().find( objid );
00066 if( object_map().end() == it ) return defaultval;
00067 const property_map_type & pmap = it.second;
00068 typename property_map_type::const_iterator pit = pmap.find( key );
00069 if( pmap.end() == pit ) return defaultval;
00070 return s11n::from_string<ValueType>( pit.second, defaultval );
00071 }
00072
00073
00074
00075
00076 static void unset( const object_id_type objid, const key_type & key )
00077 {
00078 typename object_map_type::const_iterator it = object_map().find( objid );
00079 if( object_map().end() == it ) return;
00080 const property_map_type & pmap = it.second;
00081 typename property_map_type::const_iterator pit = pmap.find( key );
00082 if( pmap.end() == pit ) return;
00083 pmap.erase( pit );
00084 }
00085
00086
00087
00088
00089
00090 static void clear( const object_id_type objid )
00091 {
00092 typename object_map_type::iterator it = object_map().find( objid );
00093 if( object_map().end() == it ) return;
00094 object_map().erase( it );
00095 }
00096
00097
00098 static void clear_all()
00099 {
00100 object_map().clear();
00101 }
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117 static property_map_type * get_map( const object_id_type objid )
00118 {
00119 typename object_map_type::iterator it = object_map().find( objid );
00120 if( object_map().end() == it ) return 0;
00121 property_map_type * pm = & ((*it).second);
00122 return pm;
00123 }
00124
00125 private:
00126 typedef property_server< ContextType, ObjIDType > this_type;
00127 typedef std::map<object_id_type,property_map_type> object_map_type;
00128
00129 static object_map_type & object_map()
00130 {
00131 return s11n::phoenix<object_map_type,this_type>::instance();
00132 }
00133
00134 };
00135
00136 }
00137
00138 #endif // PROPERTY_SERVER_H_INCLUDED