00001 #ifndef CDBBASE_ANY_TYPE_DICT_HH
00002 #define CDBBASE_ANY_TYPE_DICT_HH
00003
00004
00005
00006
00007 #include "CdbBase/CdbCommon.hh"
00008
00009 #include <map>
00010 #include <vector>
00011
00012
00013
00014 namespace CdbAnyTypeDictImpl {
00015
00016 struct TypeIdGenerator {
00017 static int next( ) { static int id = 0; return id++; }
00018 };
00019
00020 template< typename T >
00021 struct Type2Id {
00022 static int id( ) { static int myId = TypeIdGenerator::next( ); return myId; }
00023 };
00024
00025 struct HolderBase {
00026 HolderBase( int theTypeId ) : typeId(theTypeId) { }
00027 virtual ~HolderBase( ) { }
00028 virtual HolderBase* clone( ) const = 0;
00029 int typeId;
00030 };
00031
00032 template< typename T >
00033 struct Holder : public HolderBase {
00034 Holder( T* thePtr ) : HolderBase( Type2Id<T>::id( )), ptr(thePtr) { }
00035 Holder( const Holder<T>& other ) : HolderBase( other ), ptr(new T( *other.ptr )) { }
00036 Holder<T>& operator=( const Holder<T>& other )
00037 {
00038 if( this != &other ) {
00039 HolderBase::operator=( other );
00040 delete ptr;
00041 ptr = new T( *other.ptr );
00042 }
00043 return *this;
00044 }
00045 virtual ~Holder( ) { delete ptr; }
00046 virtual HolderBase* clone( ) const { return new Holder( *this ); }
00047 T* ptr;
00048 private:
00049 Holder( );
00050 };
00051 }
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092 template< class K >
00093 class CdbAnyTypeDict {
00094
00095 private:
00096
00097
00098
00099 typedef std::map<K,CdbAnyTypeDictImpl::HolderBase*> HolderBaseMap;
00100
00101 public:
00102
00103
00104
00105 CdbAnyTypeDict( ) { }
00106
00107
00108
00109 CdbAnyTypeDict( const CdbAnyTypeDict<K>& other )
00110 {
00111 for( typename HolderBaseMap::const_iterator itr = other._data.begin( );
00112 itr != other._data.end( );
00113 ++itr ) _data[itr->first] = itr->second->clone( );
00114 }
00115
00116
00117
00118 virtual ~CdbAnyTypeDict( )
00119 {
00120 for( typename HolderBaseMap::iterator itr = _data.begin( );
00121 itr != _data.end( );
00122 ++itr ) delete itr->second;
00123 }
00124
00125
00126
00127 CdbAnyTypeDict<K>& operator=( const CdbAnyTypeDict<K>& other )
00128 {
00129 if( this != &other ) {
00130 for( typename HolderBaseMap::iterator itr = _data.begin( );
00131 itr != _data.end( );
00132 ++itr ) delete itr->second;
00133 _data.clear( );
00134 for( typename HolderBaseMap::const_iterator itr = other._data.begin( );
00135 itr != other._data.end( );
00136 ++itr ) _data[itr->first] = itr->second->clone( );
00137 }
00138 return *this;
00139 }
00140
00141
00142
00143 bool empty( ) const
00144 {
00145 return _data.empty( );
00146 }
00147
00148
00149
00150 bool exists( const K& theKey ) const
00151 {
00152 typename HolderBaseMap::const_iterator itr = _data.find( theKey );
00153 return itr != _data.end( );
00154 }
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165 template< typename V >
00166 CdbStatus insert( const K& theKey,
00167 const V& theValue
00168 )
00169 {
00170 if( exists( theKey )) return CdbStatus::Error;
00171 _data[theKey] = new CdbAnyTypeDictImpl::Holder<V>( new V( theValue ));
00172 return CdbStatus::Success;
00173 }
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187 template< typename V >
00188 CdbStatus replace( const K& theKey,
00189 const V& theValue,
00190 const bool forceCreateFlag = true )
00191 {
00192 typename HolderBaseMap::const_iterator itr = _data.find( theKey );
00193 if( itr != _data.end( )) {
00194 if( itr->second->typeId != CdbAnyTypeDictImpl::Type2Id<V>::id( )) return CdbStatus::ConflictOfParameters;
00195 delete itr->second;
00196 } else {
00197 if( !forceCreateFlag) return CdbStatus::NotFound;
00198 }
00199 _data[theKey] = new CdbAnyTypeDictImpl::Holder<V>( new V( theValue ));
00200 return CdbStatus::Success;
00201 }
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212 template< typename V >
00213 CdbStatus find( const K& theKey,
00214 V& theValue ) const
00215 {
00216 typename HolderBaseMap::const_iterator itr = _data.find( theKey );
00217 if( itr == _data.end( )) return CdbStatus::NotFound;
00218
00219 const CdbAnyTypeDictImpl::HolderBase* holderBasePtr = itr->second;
00220 if( holderBasePtr->typeId != CdbAnyTypeDictImpl::Type2Id<V>::id( )) return CdbStatus::ConflictOfParameters;
00221
00222 const CdbAnyTypeDictImpl::Holder<V>* holderPtr = (const CdbAnyTypeDictImpl::Holder<V>*)holderBasePtr;
00223 theValue = V( *holderPtr->ptr );
00224
00225 return CdbStatus::Success;
00226 }
00227
00228
00229
00230
00231
00232
00233 void keys( std::vector<K>& theVectorOfKeys ) const
00234 {
00235 theVectorOfKeys.resize( 0 );
00236 for( typename HolderBaseMap::const_iterator itr = _data.begin( );
00237 itr != _data.end( );
00238 ++itr ) theVectorOfKeys.push_back( itr->first );
00239 return;
00240 }
00241
00242 private:
00243
00244
00245
00246 HolderBaseMap _data;
00247 };
00248
00249 #endif // CDBBASE_ANY_TYPE_DICT_HH