![]() |
MAK RTIspy API Documentation for HLA 1516
|
00001 /********************************************************************* 00002 ** Copyright (c) 2006 MaK Technologies, Inc. 00003 ** All rights reserved. 00004 *********************************************************************/ 00005 /******************************************************************************* 00006 ** $RCSfile: pluggableFactory.h,v $ $Revision: 1.8 $ $State: Exp $ 00007 *******************************************************************************/ 00010 00011 #ifndef DtPluggableFactory_H_ 00012 #define DtPluggableFactory_H_ 00013 00014 #include <iostream> 00015 #include <vlutil/vlPrint.h> 00016 #include <vlutil/vlSmartPointers.h> 00017 00018 template< typename BaseType, typename DerivedType > 00019 class DtPluggableFactoryProduct; 00020 00023 #define DtREGISTER_WITH_FACTORY_BY_CLASSNAME( ProductClass, CreatableClass ) \ 00024 static MAKRti::DtString DtRegistered##CreatableClass##With##ProductClass##FactoryByName = \ 00025 DtPluggableFactory< MAKRti::DtString, ProductClass >::registerCreator( #CreatableClass, &CreatableClass::create ); 00026 #define DtREGISTER_WITH_FACTORY_BY_NAME( CreationKey, ProductClass, CreatableClass ) \ 00027 static MAKRti::DtString DtRegistered##CreatableClass##With##ProductClass##FactoryByKey##CreationKey## = \ 00028 DtPluggableFactory< MAKRti::DtString, ProductClass >::registerCreator( CreationKey, &CreatableClass::create ); 00029 #define DtREGISTER_TEMPLATE_WITH_FACTORY_BY_NAME( CreationKey, ProductClass, CreatableClass, TemplateParam ) \ 00030 static MAKRti::DtString DtRegistered##CreatableClass##With##ProductClass##FactoryByKey##CreationKey## = \ 00031 DtPluggableFactory< MAKRti::DtString, ProductClass >::registerCreator( CreationKey, &CreatableClass<TemplateParam>::create ); 00032 00035 #define DtREGISTER_WITH_FACTORY_BY_CLASSNAME_WITH_PARAM( ProductClass, CreatableClass, CreatorParam ) \ 00036 static MAKRti::DtString DtRegistered##CreatableClass##With##ProductClass##FactoryByNameWithParam = \ 00037 DtPluggableFactory< MAKRti::DtString, ProductClass, CreatorParam >::registerCreator( #CreatableClass, &CreatableClass::create ); 00038 #define DtREGISTER_WITH_FACTORY_BY_NAME_WITH_PARAM( CreationKey, ProductClass, CreatableClass, CreatorParam ) \ 00039 static MAKRti::DtString DtRegistered##CreatableClass##With##ProductClass##FactoryByKey##CreationKey##WithParam = \ 00040 DtPluggableFactory< MAKRti::DtString, ProductClass, CreatorParam >::registerCreator( CreationKey, &CreatableClass::create ); 00041 #define DtREGISTER_TEMPLATE_WITH_FACTORY_BY_NAME_WITH_PARAM( CreationKey, ProductClass, CreatableClass, TemplateParam, CreatorParam ) \ 00042 static MAKRti::DtString DtRegistered##CreatableClass##With##ProductClass##FactoryByKey##CreationKey##WithParam = \ 00043 DtPluggableFactory< MAKRti::DtString, ProductClass, CreatorParam >::registerCreator( CreationKey, &CreatableClass<TemplateParam>::create ); 00044 00045 00082 template< typename Key, typename Product, typename ProductCreatorParameter > 00083 class DtPluggableFactory 00084 { 00085 public: 00086 typedef DtBoost::shared_ptr<Product> DtFactoryProductSP; 00087 typedef DtFactoryProductSP (*DtCreatorFn)(); 00088 typedef DtFactoryProductSP (*DtParamCreatorFn)(ProductCreatorParameter); 00089 00090 typedef std::map< Key, DtCreatorFn > DtKeyToCreatorMap; 00091 typedef std::map< Key, DtParamCreatorFn > DtKeyToParamCreatorMap; 00092 00093 static bool isCreatable( const Key& key ); 00094 00096 static DtFactoryProductSP create( const Key& key ); 00097 00099 static DtFactoryProductSP create( const Key& key, ProductCreatorParameter parameter); 00100 00102 static MAKRti::DtString registerCreator( const Key& key, DtCreatorFn creator ); 00103 00105 static MAKRti::DtString registerCreator( const Key& key, DtParamCreatorFn creator ); 00106 00107 private: 00108 00110 DtPluggableFactory(); 00111 00112 static DtPluggableFactory< Key, Product, ProductCreatorParameter >* factory(); 00113 00114 bool productHasCreator( const Key& key ); 00115 00116 DtFactoryProductSP createProduct( const Key& key ); 00117 00118 DtFactoryProductSP createProduct( const Key& key, ProductCreatorParameter param ); 00119 00120 MAKRti::DtString registerProductCreator( const Key& key, DtCreatorFn creator ); 00121 00122 MAKRti::DtString registerProductCreator( const Key& key, DtParamCreatorFn creator ); 00123 00124 private: 00125 00126 DtKeyToCreatorMap theCreators; 00127 DtKeyToParamCreatorMap theParamCreators; 00128 }; 00129 00130 00131 template< typename Key, typename Product, typename ProductCreatorParameter > 00132 bool DtPluggableFactory<Key, Product, ProductCreatorParameter>::isCreatable( const Key& key ) 00133 { 00134 return factory()->productHasCreator( key ); 00135 } 00136 00137 template< typename Key, typename Product, typename ProductCreatorParameter > 00138 typename DtPluggableFactory<Key, Product, ProductCreatorParameter>::DtFactoryProductSP DtPluggableFactory<Key, Product, ProductCreatorParameter>::create( const Key& key ) 00139 { 00140 return factory()->createProduct( key ); 00141 } 00142 00143 template< typename Key, typename Product, typename ProductCreatorParameter > 00144 typename DtPluggableFactory<Key, Product, ProductCreatorParameter>::DtFactoryProductSP DtPluggableFactory<Key, Product, ProductCreatorParameter>::create( const Key& key, ProductCreatorParameter parameter) 00145 { 00146 return factory()->createProduct( key, parameter ); 00147 } 00148 00149 template< typename Key, typename Product, typename ProductCreatorParameter > 00150 MAKRti::DtString DtPluggableFactory<Key, Product, ProductCreatorParameter>::registerCreator( const Key& key, DtCreatorFn creator ) 00151 { 00152 return factory()->registerProductCreator( key, creator ); 00153 } 00154 00155 template< typename Key, typename Product, typename ProductCreatorParameter > 00156 MAKRti::DtString DtPluggableFactory<Key, Product, ProductCreatorParameter>::registerCreator( const Key& key, DtParamCreatorFn creator ) 00157 { 00158 return factory()->registerProductCreator( key, creator ); 00159 } 00160 00161 template< typename Key, typename Product, typename ProductCreatorParameter > 00162 DtPluggableFactory<Key, Product, ProductCreatorParameter>::DtPluggableFactory() {} 00163 00164 template< typename Key, typename Product, typename ProductCreatorParameter > 00165 DtPluggableFactory< Key, Product, ProductCreatorParameter >* DtPluggableFactory<Key, Product, ProductCreatorParameter>::factory() 00166 { 00169 static DtPluggableFactory< Key, Product, ProductCreatorParameter >* theFactory = 0; 00170 if ( !theFactory ) 00171 { 00174 theFactory = new DtPluggableFactory< Key, Product, ProductCreatorParameter >(); 00175 } 00176 return theFactory; 00177 } 00178 00179 template< typename Key, typename Product, typename ProductCreatorParameter > 00180 bool DtPluggableFactory<Key, Product, ProductCreatorParameter>::productHasCreator( const Key& key ) 00181 { 00182 return ( theCreators.find( key ) != theCreators.end() || 00183 theParamCreators.find( key ) != theParamCreators.end() ); 00184 } 00185 00186 template< typename Key, typename Product, typename ProductCreatorParameter > 00187 typename DtPluggableFactory<Key, Product, ProductCreatorParameter>::DtFactoryProductSP DtPluggableFactory<Key, Product, ProductCreatorParameter>::createProduct( const Key& key ) 00188 { 00189 typename DtKeyToCreatorMap::iterator pos = theCreators.find( key ); 00190 if ( pos == theCreators.end() ) { return DtFactoryProductSP(); } 00191 return pos->second(); 00192 } 00193 00194 template< typename Key, typename Product, typename ProductCreatorParameter > 00195 typename DtPluggableFactory<Key, Product, ProductCreatorParameter>::DtFactoryProductSP DtPluggableFactory<Key, Product, ProductCreatorParameter>::createProduct( const Key& key, ProductCreatorParameter param ) 00196 { 00197 typename DtKeyToParamCreatorMap::iterator pos = theParamCreators.find( key ); 00198 if ( pos == theParamCreators.end() ) { return DtFactoryProductSP(); } 00199 return pos->second(param); 00200 } 00201 00202 template< typename Key, typename Product, typename ProductCreatorParameter > 00203 MAKRti::DtString DtPluggableFactory<Key, Product, ProductCreatorParameter>::registerProductCreator( const Key& key, DtCreatorFn creator ) 00204 { 00205 #ifdef _DEBUG 00206 00207 00208 00209 00210 if ( productHasCreator( key )) 00211 { 00212 DtVerbose << "WARNING: Attempted to register Factory Key: " << key << " for multiple creators this is often an error." << std::endl; 00213 } 00214 #endif 00215 00216 theCreators[ key ] = creator; 00217 return key; 00218 } 00219 00220 00221 template< typename Key, typename Product, typename ProductCreatorParameter > 00222 MAKRti::DtString DtPluggableFactory<Key, Product, ProductCreatorParameter>::registerProductCreator( const Key& key, DtParamCreatorFn creator ) 00223 { 00224 #ifdef _DEBUG 00225 00226 00227 00228 00229 if ( productHasCreator( key )) 00230 { 00231 MAKRti::DtVerbose << "WARNING: Attempted to register Factory Key: " << 00232 key << " for multiple creators this is often an error." << std::endl; 00233 } 00234 #endif 00235 00236 theParamCreators[ key ] = creator; 00237 return key; 00238 } 00239 00240 00241 #endif //! DtPluggableFactory_H_ 00242 00243