00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #ifndef __PROPERTYINTERFACE_HPP
00032 #define __PROPERTYINTERFACE_HPP
00033
00034 #include "AssocVector.h"
00035
00036 #include "libecs.hpp"
00037 #include "PropertySlot.hpp"
00038 #include "PropertySlotProxy.hpp"
00039
00040 #define PROPERTY_FIELD "Property__"
00041 #define PROPERTYLIST_FIELD "PropertyList"
00042
00043 namespace libecs
00044 {
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 class ECELL_API PropertyInterfaceBase
00055 {
00056 public:
00057
00058 ~PropertyInterfaceBase()
00059 {
00060 ;
00061 }
00062
00063 protected:
00064
00065 PropertyInterfaceBase()
00066 {
00067 ;
00068 }
00069
00070 static void throwNoSlot( StringCref aClassName, StringCref aPropertyName );
00071
00072 static void throwNotLoadable( PropertiedClassCref aClassName,
00073 StringCref aPropertyName );
00074 static void throwNotSavable( PropertiedClassCref aClassName,
00075 StringCref aPropertyName );
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097 };
00098
00099
00100 template
00101 <
00102 class T
00103 >
00104 class PropertyInterface
00105 :
00106 public PropertyInterfaceBase
00107 {
00108
00109 public:
00110
00111 typedef PropertySlot<T> PropertySlot_;
00112 DECLARE_TYPE( PropertySlot_, PropertySlot );
00113
00114 DECLARE_ASSOCVECTOR_TEMPLATE( String, PropertySlotPtr,
00115 std::less<const String>, PropertySlotMap );
00116
00117 DECLARE_ASSOCVECTOR_TEMPLATE( String, Polymorph,
00118 std::less<const String>, PolymorphAssocVector);
00119
00120 PropertyInterface()
00121 {
00122 theInfoMap[ String( PROPERTYLIST_FIELD )] = Polymorph( PolymorphVector() ) ;
00123 T::initializePropertyInterface( Type2Type<T>() );
00124 }
00125
00126 ~PropertyInterface()
00127 {
00128
00129
00130
00131
00132
00133
00134
00135
00136 }
00137
00138
00139
00140
00141
00142
00143 static PolymorphMapCref getInfoMap( void )
00144 {
00145 static PolymorphMap aPolymorphMap;
00146 for (PolymorphAssocVectorIterator i(theInfoMap.begin()); i != theInfoMap.end() ; ++i)
00147 {
00148 aPolymorphMap[i->first] = i->second;
00149 }
00150 return aPolymorphMap;
00151 }
00152
00153
00154
00155
00156
00157
00158 static void setInfoField( StringCref aFieldName, PolymorphCref aValue )
00159 {
00160 theInfoMap[ aFieldName ] = aValue;
00161
00162 }
00163
00164
00165
00166
00167
00168
00169
00170 static void setPropertyInfoField( StringCref aPropertyName, StringCref aTypeString,
00171 Integer setFlag, Integer getFlag,
00172 Integer saveFlag, Integer loadFlag )
00173 {
00174 String PROP_FIELD( PROPERTY_FIELD );
00175 String PROPLIST_FIELD ( PROPERTYLIST_FIELD );
00176
00177 PolymorphVector aPropertyDescriptor;
00178 aPropertyDescriptor.push_back( aTypeString );
00179 aPropertyDescriptor.push_back( setFlag );
00180 aPropertyDescriptor.push_back( getFlag );
00181 aPropertyDescriptor.push_back( saveFlag );
00182 aPropertyDescriptor.push_back( loadFlag );
00183 String aPropertyNameField( aPropertyName );
00184 aPropertyNameField.insert( 0, PROP_FIELD );
00185 setInfoField( aPropertyNameField, aPropertyDescriptor );
00186
00187
00188 PolymorphVector aPolymorphVector( getInfoField( PROPLIST_FIELD ).asPolymorphVector());
00189 aPolymorphVector.push_back( aPropertyName );
00190 setInfoField( PROPLIST_FIELD, Polymorph( aPolymorphVector ) );
00191
00192 }
00193
00194
00195
00196
00197
00198 static PolymorphCref getInfoField( StringCref aFieldName )
00199 {
00200 return theInfoMap[ aFieldName ];
00201 }
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212 static PropertySlotPtr getPropertySlot( StringCref aPropertyName )
00213 {
00214 PropertySlotMapConstIterator i( findPropertySlot( aPropertyName ) );
00215
00216 if( i == thePropertySlotMap.end() )
00217 {
00218 throwNoSlot( "This class", aPropertyName );
00219 }
00220
00221 return i->second;
00222 }
00223
00224 static PropertySlotProxyPtr
00225 createPropertySlotProxy( T& anObject,
00226 StringCref aPropertyName )
00227 {
00228 try
00229 {
00230 PropertySlotPtr aPropertySlot( getPropertySlot( aPropertyName ) );
00231 return new ConcretePropertySlotProxy<T>( anObject, *aPropertySlot );
00232 }
00233 catch( NoSlotCref )
00234 {
00235 throwNoSlot( anObject.getClassName(), aPropertyName );
00236 }
00237 }
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251 static void setProperty( T& anObject, StringCref aPropertyName,
00252 PolymorphCref aValue )
00253 {
00254 PropertySlotMapConstIterator
00255 aPropertySlotMapIterator( findPropertySlot( aPropertyName ) );
00256
00257 if( aPropertySlotMapIterator != thePropertySlotMap.end() )
00258 {
00259 aPropertySlotMapIterator->second->setPolymorph( anObject, aValue );
00260 }
00261 else
00262 {
00263 anObject.defaultSetProperty( aPropertyName, aValue );
00264 }
00265 }
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279 static const Polymorph getProperty( const T& anObject,
00280 StringCref aPropertyName )
00281 {
00282 PropertySlotMapConstIterator
00283 aPropertySlotMapIterator( findPropertySlot( aPropertyName ) );
00284
00285 if( aPropertySlotMapIterator != thePropertySlotMap.end() )
00286 {
00287 return aPropertySlotMapIterator->second->getPolymorph( anObject );
00288 }
00289 else
00290 {
00291 return anObject.defaultGetProperty( aPropertyName );
00292 }
00293 }
00294
00295
00296 static void loadProperty( T& anObject, StringCref aPropertyName,
00297 PolymorphCref aValue )
00298 {
00299 PropertySlotMapConstIterator
00300 aPropertySlotMapIterator( findPropertySlot( aPropertyName ) );
00301
00302 if( aPropertySlotMapIterator != thePropertySlotMap.end() )
00303 {
00304 PropertySlotPtr aPropertySlotPtr( aPropertySlotMapIterator->second );
00305
00306 if( aPropertySlotPtr->isLoadable() )
00307 {
00308 aPropertySlotPtr->loadPolymorph( anObject, aValue );
00309 }
00310 else
00311 {
00312 throwNotLoadable( anObject, aPropertyName );
00313 }
00314 }
00315 else
00316 {
00317 anObject.defaultSetProperty( aPropertyName, aValue );
00318 }
00319 }
00320
00321
00322 static const Polymorph
00323 saveProperty( const T& anObject, StringCref aPropertyName )
00324 {
00325 PropertySlotMapConstIterator
00326 aPropertySlotMapIterator( findPropertySlot( aPropertyName ) );
00327
00328 if( aPropertySlotMapIterator != thePropertySlotMap.end() )
00329 {
00330 PropertySlotPtr aPropertySlotPtr( aPropertySlotMapIterator->second );
00331 if( aPropertySlotPtr->isSavable() )
00332 {
00333 return aPropertySlotPtr->savePolymorph( anObject );
00334 }
00335 else
00336 {
00337 throwNotSavable( anObject, aPropertyName );
00338 }
00339 }
00340 else
00341 {
00342 return anObject.defaultGetProperty( aPropertyName );
00343 }
00344 }
00345
00346 static const Polymorph getPropertyList( const T& anObject )
00347 {
00348 PolymorphVector aVector1, aVector2;
00349
00350
00351 for( PropertySlotMapConstIterator i( thePropertySlotMap.begin() );
00352 i != thePropertySlotMap.end() ; ++i )
00353 {
00354 aVector1.push_back( i->first );
00355 }
00356
00357 aVector2 = anObject.defaultGetPropertyList();
00358
00359 if( aVector2.size() > 0 )
00360 {
00361 for( PolymorphVectorIterator i( aVector2.begin() );
00362 i != aVector2.end(); ++i )
00363 {
00364 aVector1.push_back( i->asString() );
00365 }
00366 }
00367
00368 return aVector1;
00369 }
00370
00371
00372 static void
00373 registerPropertySlot( StringCref aName, PropertySlotPtr aPropertySlotPtr )
00374 {
00375 if( findPropertySlot( aName ) != thePropertySlotMap.end() )
00376 {
00377
00378 delete thePropertySlotMap[ aName ];
00379 thePropertySlotMap.erase( aName );
00380 }
00381
00382
00383 thePropertySlotMap.insert( std::make_pair( aName, aPropertySlotPtr ) );
00384 }
00385
00386
00387 static const Polymorph
00388 getPropertyAttributes( const T& anObject, StringCref aPropertyName )
00389 {
00390 PropertySlotMapConstIterator i( findPropertySlot( aPropertyName ) );
00391
00392 if( i != thePropertySlotMap.end() )
00393 {
00394 PropertySlotBasePtr
00395 aPropertySlotPtr( getPropertySlot( aPropertyName ) );
00396
00397 PolymorphVector aVector;
00398
00399
00400 aVector.push_back
00401 ( static_cast<Integer>( aPropertySlotPtr->isSetable() ) );
00402
00403
00404 aVector.push_back
00405 ( static_cast<Integer>( aPropertySlotPtr->isGetable() ) );
00406
00407
00408 aVector.push_back
00409 ( static_cast<Integer>( aPropertySlotPtr->isLoadable() ) );
00410
00411
00412 aVector.push_back
00413 ( static_cast<Integer>( aPropertySlotPtr->isSavable() ) );
00414
00415 return aVector;
00416 }
00417 else
00418 {
00419 return anObject.defaultGetPropertyAttributes( aPropertyName );
00420 }
00421 }
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439 static PropertySlotMapCref getPropertySlotMap()
00440 {
00441 return thePropertySlotMap;
00442 }
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489 private:
00490
00491 static PropertySlotMapConstIterator
00492 findPropertySlot( StringCref aPropertyName )
00493 {
00494 return thePropertySlotMap.find( aPropertyName );
00495 }
00496
00497 private:
00498
00499 static PropertySlotMap thePropertySlotMap;
00500
00501 static PolymorphAssocVector theInfoMap;
00502 };
00503
00504
00505
00506
00507
00508 template< class T > typename libecs::PropertyInterface< T>::PropertySlotMap
00509 libecs::PropertyInterface< T>::thePropertySlotMap;
00510 template< class T > typename libecs::PropertyInterface< T>::PolymorphAssocVector
00511 libecs::PropertyInterface< T>::theInfoMap;
00512
00513
00514
00515
00516 }
00517
00518 #endif
00519
00520
00521
00522
00523
00524
00525
00526