00001 // 00002 // The Austria library is copyright (c) Gianni Mariani 2004. 00003 // 00004 // Grant Of License. Grants to LICENSEE the non-exclusive right to use the Austria 00005 // library subject to the terms of the LGPL. 00006 // 00007 // A copy of the license is available in this directory or one may be found at this URL: 00008 // http://www.gnu.org/copyleft/lesser.txt 00009 // 00015 #ifndef x_at_factory_h_x 00016 #define x_at_factory_h_x 1 00017 00018 #include "at_exports.h" 00019 00020 #include "at_source_locator.h" 00021 #include "at_status_report.h" 00022 00023 #include "at_types.h" 00024 #include "at_lifetime.h" 00025 00026 #include <map> 00027 #include <sstream> 00028 00029 // Austria namespace 00030 namespace at 00031 { 00032 00163 // ======== DKy ==================================================== 00177 class AUSTRIA_EXPORT DKy 00178 { 00179 public: 00180 00190 inline DKy( const char * i_value ) 00191 : m_value( i_value ) 00192 { 00193 } 00194 00199 inline operator const char * () const 00200 { 00201 return m_value; 00202 } 00203 00208 inline operator AT_String () const 00209 { 00210 return AT_String( m_value ); 00211 } 00212 00218 bool operator==( const DKy i_value ) const; 00219 00225 bool operator<( const DKy i_value ) const; 00226 00232 bool operator<=( const DKy i_value ) const; 00233 00239 bool operator>( const DKy i_value ) const; 00240 00246 bool operator>=( const DKy i_value ) const; 00247 00248 private: 00249 00250 const char * m_value; // contains a simple pointer 00251 00252 }; 00253 00254 // 00255 // ============================================================================ 00256 // ============================================================================ 00257 // 00258 // The following classes define templates for passing parameters to 00259 // constructors. 00260 // 00261 // ============================================================================ 00262 // ============================================================================ 00263 // 00264 00265 00266 // ======== FactoryTraits ========================================== 00272 class AUSTRIA_EXPORT FactoryTraits 00273 { 00274 public: 00275 00281 enum FactoryErrorCodes 00282 { 00288 FactoryNotFound, 00289 00295 FactoryCreateException, 00296 00302 FactoryFailure 00303 }; 00304 00311 enum FactoryCreateReportingOption 00312 { 00313 00320 AllwaysReturn, 00321 00327 ThrowOnError, 00328 00334 AssertOnError 00335 }; 00336 00337 00338 }; 00339 00340 00341 // ======== Wrapper_Basic ========================================== 00348 class AUSTRIA_EXPORT Wrapper_Basic 00349 { 00350 public: 00351 00352 // ======== ReportError =========================================== 00361 void ReportError( 00362 FactoryTraits::FactoryErrorCodes i_code, 00363 const AT_String & i_msg 00364 ); 00365 00366 // ======== ReportFactoryNotFound ================================= 00371 void ReportFactoryNotFound(); 00372 00373 // ======== ReportCreateException ================================= 00380 void ReportCreateException( const AT_String & i_keystr ); 00381 00382 00383 // ======== ReportFactoryFailed =================================== 00390 void ReportFactoryFailed( const AT_String & i_keystr ); 00391 00392 00393 // ======== Wrapper_Basic ====================================== 00398 inline Wrapper_Basic( 00399 StatusReport * io_srep, 00400 FactoryTraits::FactoryCreateReportingOption i_create_report_option 00401 ) 00402 : m_srep( io_srep ), 00403 m_create_report_option( i_create_report_option ) 00404 { 00405 } 00406 00410 StatusReport * m_srep; 00411 00416 FactoryTraits::FactoryCreateReportingOption m_create_report_option; 00417 00418 00419 }; 00420 00421 00422 // ======== KeyToStr =============================================== 00430 template< typename w_KeyType > 00431 AT_String KeyToStr( const w_KeyType & i_key ) 00432 { 00433 std::ostringstream l_ostream; 00434 00435 l_ostream << i_key; 00436 00437 return l_ostream.str(); 00438 00439 } // end KeyToStr 00440 00441 00442 00443 // ======== FactoryRegistryEntry =================================== 00453 class AUSTRIA_EXPORT FactoryRegistryEntry 00454 { 00455 public: 00456 00468 virtual ~FactoryRegistryEntry() 00469 { 00470 } 00471 00472 00473 // ======== RemoveAndDelete ======================================= 00482 virtual void RemoveAndDelete() = 0; 00483 00484 }; 00485 00486 00487 // ======== FactoryEntryLocator_Base =============================== 00493 class AUSTRIA_EXPORT FactoryEntryLocator_Base 00494 { 00495 public: 00496 00497 FactoryEntryLocator_Base() 00498 : m_registry_entry( 0 ) 00499 { 00500 } 00501 00502 // ======== ~FactoryEntryLocator_Base =========================== 00508 virtual ~FactoryEntryLocator_Base() 00509 { 00510 FactoryRegistryEntry * l_registry_entry( m_registry_entry ); 00511 00512 if ( l_registry_entry != 0 ) 00513 { 00514 m_registry_entry = 0; 00515 00516 // this may cause FactoryEntryLocator_Base::RemoveEntry 00517 // to be called but that's ok since m_registry_entry will 00518 // be 0 and hence will not do anything. 00519 l_registry_entry->RemoveAndDelete(); 00520 } 00521 00522 } // end ~FactoryEntryLocator_Base 00523 00524 00525 // ======== RemoveEntry =========================================== 00535 void RemoveEntry() 00536 { 00537 00538 FactoryRegistryEntry * l_registry_entry( m_registry_entry ); 00539 00540 if ( l_registry_entry != 0 ) 00541 { 00542 // we can get called recusively. Setting m_registry_entry 00543 // breaks the cycle. 00544 m_registry_entry = 0; 00545 00546 delete l_registry_entry; 00547 } 00548 00549 return; 00550 00551 } // end RemoveEntry 00552 00558 FactoryRegistryEntry * m_registry_entry; 00559 00560 }; 00561 00562 00563 00564 // ======== Factory_Base ================================================ 00571 template< 00572 typename w_KeyType 00573 > 00574 class Factory_Base 00575 : public SourceLocator_Basic, 00576 public FactoryEntryLocator_Base 00577 { 00578 public: 00579 00580 Factory_Base( 00581 w_KeyType i_key, 00582 const char * i_filename, 00583 int i_lineno, 00584 const char * i_description 00585 ) 00586 : SourceLocator_Basic( i_filename, i_lineno ), 00587 m_description( i_description ), 00588 m_factory_key( i_key ) 00589 { 00590 } 00591 00592 00593 // ======== Size ================================================== 00601 virtual int Size() = 0; 00602 00603 00604 // ======== GetKey ================================================ 00610 inline const w_KeyType & GetKey() const 00611 { 00612 return m_factory_key; 00613 } // end GetKey 00614 00618 const char * m_description; 00619 00623 w_KeyType m_factory_key; 00624 00625 }; 00626 00627 00628 // ======== Creator0P =============================================== 00634 template< 00635 typename w_InterfaceType, 00636 typename w_KeyType 00637 > 00638 class Creator0P 00639 : public Factory_Base< w_KeyType > 00640 { 00641 public: 00642 00643 00644 // ======== Creator0P =========================================== 00651 inline Creator0P( 00652 w_KeyType i_key, 00653 const char * i_filename, 00654 int i_lineno, 00655 const char * i_description 00656 ) 00657 : Factory_Base< w_KeyType >( 00658 i_key, 00659 i_filename, 00660 i_lineno, 00661 i_description 00662 ) 00663 { 00664 } // end Creator0P 00665 00666 00667 // ======== Create ================================================ 00675 virtual w_InterfaceType * Create() = 0; 00676 00677 // ======== Create ================================================ 00686 virtual w_InterfaceType * Create( void * i_location ) = 0; 00687 00688 00689 // ======== Destroy =============================================== 00700 virtual void Destroy( w_InterfaceType * i_ptr ) = 0; 00701 00702 00703 // ======== CreateWrapper ========================================= 00710 struct CreateWrapper 00711 : public Wrapper_Basic 00712 { 00713 00720 CreateWrapper( 00721 Creator0P * i_creator, 00722 StatusReport * o_srep = 0, 00723 FactoryTraits::FactoryCreateReportingOption i_option = FactoryTraits::AllwaysReturn 00724 ) 00725 : Wrapper_Basic( o_srep, i_option ), 00726 m_creator( i_creator ) 00727 { 00728 } 00729 00735 w_InterfaceType * operator() () 00736 { 00737 if ( m_creator == 0 ) 00738 { 00739 ReportFactoryNotFound(); 00740 return 0; 00741 } 00742 00743 w_InterfaceType * l_interface; 00744 00745 try 00746 { 00747 l_interface = m_creator->Create(); 00748 } 00749 catch ( ... ) 00750 { 00751 ReportCreateException( KeyToStr( m_creator->GetKey() ) ); 00752 00753 if ( m_create_report_option == FactoryTraits::ThrowOnError ) 00754 { 00755 throw; // this should re-throw the same exception that was 00756 // caught. 00757 } 00758 00759 return 0; // finished. 00760 } 00761 00762 if ( l_interface == 0 ) 00763 { 00764 ReportFactoryFailed( KeyToStr( m_creator->GetKey() ) ); 00765 } 00766 00767 return l_interface; 00768 } 00769 00774 Creator0P * m_creator; 00775 00776 }; // end CreateWrapper 00777 00778 // 00779 // Member variables for Creator0P 00780 // 00781 00782 }; 00783 00784 00785 // ======== CreatorImpl0P =========================================== 00791 template< 00792 typename w_ImplementorType, 00793 typename w_InterfaceType, 00794 typename w_KeyType 00795 > 00796 class CreatorImpl0P 00797 : public Creator0P< w_InterfaceType, w_KeyType > 00798 { 00799 public: 00800 00801 typedef Creator0P< w_InterfaceType, w_KeyType > t_CreatorType; 00802 00803 CreatorImpl0P( 00804 w_KeyType i_key, 00805 const char * i_filename, 00806 int i_lineno, 00807 const char * i_description 00808 ) 00809 : Creator0P< w_InterfaceType, w_KeyType >( 00810 i_key, 00811 i_filename, 00812 i_lineno, 00813 i_description 00814 ) 00815 { 00816 } 00817 00818 virtual w_InterfaceType * Create() 00819 { 00820 return new w_ImplementorType(); 00821 } 00822 00823 virtual int Size() 00824 { 00825 return sizeof( w_ImplementorType ); 00826 } 00827 00828 virtual w_InterfaceType * Create( void * i_location ) 00829 { 00830 return new ( i_location ) w_ImplementorType; 00831 } 00832 00833 virtual void Destroy( w_InterfaceType * i_ptr ) 00834 { 00835 w_ImplementorType * l_impl = static_cast< w_ImplementorType * >( i_ptr ); 00836 00837 l_impl->~w_ImplementorType(); 00838 } 00839 00840 00841 }; 00842 00843 00844 // ======== Creator1P =============================================== 00850 template< 00851 typename w_InterfaceType, 00852 typename w_KeyType, 00853 typename w_arg_1 00854 > 00855 class Creator1P 00856 : public Factory_Base< w_KeyType > 00857 { 00858 public: 00859 00860 00861 // ======== Creator1P =========================================== 00868 inline Creator1P( 00869 w_KeyType i_key, 00870 const char * i_filename, 00871 int i_lineno, 00872 const char * i_description 00873 ) 00874 : Factory_Base< w_KeyType >( 00875 i_key, 00876 i_filename, 00877 i_lineno, 00878 i_description 00879 ) 00880 { 00881 } // end Creator1P 00882 00883 00884 // ======== Create ================================================ 00892 virtual w_InterfaceType * Create( w_arg_1 i_arg_1 ) = 0; 00893 00894 // ======== Destroy =============================================== 00905 virtual void Destroy( w_InterfaceType * i_ptr ) = 0; 00906 00907 // ======== Create ================================================ 00916 virtual w_InterfaceType * Create( w_arg_1 i_arg_1, void * i_location ) = 0; 00917 00918 00919 // ======== CreateWrapper ========================================= 00926 struct CreateWrapper 00927 : public Wrapper_Basic 00928 { 00929 00936 CreateWrapper( 00937 Creator1P * i_creator, 00938 StatusReport * o_srep = 0, 00939 FactoryTraits::FactoryCreateReportingOption i_option = FactoryTraits::AllwaysReturn 00940 ) 00941 : Wrapper_Basic( o_srep, i_option ), 00942 m_creator( i_creator ) 00943 { 00944 } 00945 00951 w_InterfaceType * operator() ( w_arg_1 i_arg_1 ) 00952 { 00953 if ( m_creator == 0 ) 00954 { 00955 ReportFactoryNotFound(); 00956 return 0; 00957 } 00958 00959 w_InterfaceType * l_interface; 00960 00961 try 00962 { 00963 l_interface = m_creator->Create( i_arg_1 ); 00964 } 00965 catch ( ... ) 00966 { 00967 ReportCreateException( KeyToStr( m_creator->GetKey() ) ); 00968 00969 if ( m_create_report_option == FactoryTraits::ThrowOnError ) 00970 { 00971 throw; // this should re-throw the same exception that was 00972 // caught. 00973 } 00974 00975 return 0; // finished. 00976 } 00977 00978 if ( l_interface == 0 ) 00979 { 00980 ReportFactoryFailed( KeyToStr( m_creator->GetKey() ) ); 00981 } 00982 00983 return l_interface; 00984 } 00985 00990 Creator1P * m_creator; 00991 00992 }; // end CreateWrapper 00993 00994 // 00995 // Member variables for Creator1P 00996 // 00997 00998 }; 00999 01000 01001 // ======== CreatorImpl1P =========================================== 01007 template< 01008 typename w_ImplementorType, 01009 typename w_InterfaceType, 01010 typename w_KeyType, 01011 typename w_arg_1 01012 > 01013 class CreatorImpl1P 01014 : public Creator1P< w_InterfaceType, w_KeyType, w_arg_1 > 01015 { 01016 public: 01017 01018 typedef Creator1P< w_InterfaceType, w_KeyType, w_arg_1 > t_CreatorType; 01019 01020 CreatorImpl1P( 01021 w_KeyType i_key, 01022 const char * i_filename, 01023 int i_lineno, 01024 const char * i_description 01025 ) 01026 : Creator1P< w_InterfaceType, w_KeyType, w_arg_1 >( 01027 i_key, 01028 i_filename, 01029 i_lineno, 01030 i_description 01031 ) 01032 { 01033 } 01034 01035 virtual w_InterfaceType * Create( w_arg_1 i_arg_1 ) 01036 { 01037 return new w_ImplementorType( i_arg_1 ); 01038 } 01039 01040 virtual int Size() 01041 { 01042 return sizeof( w_ImplementorType ); 01043 } 01044 01045 virtual w_InterfaceType * Create( w_arg_1 i_arg_1, void * i_location ) 01046 { 01047 return new ( i_location ) w_ImplementorType( i_arg_1 ); 01048 } 01049 01050 virtual void Destroy( w_InterfaceType * i_ptr ) 01051 { 01052 w_ImplementorType * l_impl = static_cast< w_ImplementorType * >( i_ptr ); 01053 01054 l_impl->~w_ImplementorType(); 01055 } 01056 01057 }; 01058 01059 01060 01061 // ======== Creator2P =============================================== 01067 template< 01068 typename w_InterfaceType, 01069 typename w_KeyType, 01070 typename w_arg_1, 01071 typename w_arg_2 01072 > 01073 class Creator2P 01074 : public Factory_Base< w_KeyType > 01075 { 01076 public: 01077 01078 01079 // ======== Creator2P =========================================== 01086 inline Creator2P( 01087 w_KeyType i_key, 01088 const char * i_filename, 01089 int i_lineno, 01090 const char * i_description 01091 ) 01092 : Factory_Base< w_KeyType >( 01093 i_key, 01094 i_filename, 01095 i_lineno, 01096 i_description 01097 ) 01098 { 01099 } // end Creator2P 01100 01101 01102 // ======== Create ================================================ 01110 virtual w_InterfaceType * Create( w_arg_1 i_arg_1, w_arg_2 i_arg_2 ) = 0; 01111 01112 01113 // ======== Destroy =============================================== 01124 virtual void Destroy( w_InterfaceType * i_ptr ) = 0; 01125 01126 01127 // ======== Create ================================================ 01136 virtual w_InterfaceType * Create( w_arg_1 i_arg_1, w_arg_2 i_arg_2, void * i_location ) = 0; 01137 01138 01139 // ======== CreateWrapper ========================================= 01146 struct CreateWrapper 01147 : public Wrapper_Basic 01148 { 01149 01156 CreateWrapper( 01157 Creator2P * i_creator, 01158 StatusReport * o_srep = 0, 01159 FactoryTraits::FactoryCreateReportingOption i_option = FactoryTraits::AllwaysReturn 01160 ) 01161 : Wrapper_Basic( o_srep, i_option ), 01162 m_creator( i_creator ) 01163 { 01164 } 01165 01171 w_InterfaceType * operator() ( w_arg_1 i_arg_1, w_arg_2 i_arg_2 ) 01172 { 01173 if ( m_creator == 0 ) 01174 { 01175 ReportFactoryNotFound(); 01176 return 0; 01177 } 01178 01179 w_InterfaceType * l_interface; 01180 01181 try 01182 { 01183 l_interface = m_creator->Create( i_arg_1, i_arg_2 ); 01184 } 01185 catch ( ... ) 01186 { 01187 ReportCreateException( KeyToStr( m_creator->GetKey() ) ); 01188 01189 if ( m_create_report_option == FactoryTraits::ThrowOnError ) 01190 { 01191 throw; // this should re-throw the same exception that was 01192 // caught. 01193 } 01194 01195 return 0; // finished. 01196 } 01197 01198 if ( l_interface == 0 ) 01199 { 01200 ReportFactoryFailed( KeyToStr( m_creator->GetKey() ) ); 01201 } 01202 01203 return l_interface; 01204 } 01205 01210 Creator2P * m_creator; 01211 01212 }; // end CreateWrapper 01213 01214 // 01215 // Member variables for Creator2P 01216 // 01217 01218 }; 01219 01220 01221 // ======== CreatorImpl2P =========================================== 01227 template< 01228 typename w_ImplementorType, 01229 typename w_InterfaceType, 01230 typename w_KeyType, 01231 typename w_arg_1, 01232 typename w_arg_2 01233 > 01234 class CreatorImpl2P 01235 : public Creator2P< w_InterfaceType, w_KeyType, w_arg_1, w_arg_2 > 01236 { 01237 public: 01238 01239 typedef Creator2P< w_InterfaceType, w_KeyType, w_arg_1, w_arg_2 > t_CreatorType; 01240 01241 CreatorImpl2P( 01242 w_KeyType i_key, 01243 const char * i_filename, 01244 int i_lineno, 01245 const char * i_description 01246 ) 01247 : Creator2P< w_InterfaceType, w_KeyType, w_arg_1, w_arg_2 >( 01248 i_key, 01249 i_filename, 01250 i_lineno, 01251 i_description 01252 ) 01253 { 01254 } 01255 01256 virtual w_InterfaceType * Create( w_arg_1 i_arg_1, w_arg_2 i_arg_2 ) 01257 { 01258 return new w_ImplementorType( i_arg_1, i_arg_2 ); 01259 } 01260 01261 virtual int Size() 01262 { 01263 return sizeof( w_ImplementorType ); 01264 } 01265 01266 virtual w_InterfaceType * Create( w_arg_1 i_arg_1, w_arg_2 i_arg_2, void * i_location ) 01267 { 01268 return new ( i_location ) w_ImplementorType( i_arg_1, i_arg_2 ); 01269 } 01270 01271 virtual void Destroy( w_InterfaceType * i_ptr ) 01272 { 01273 w_ImplementorType * l_impl = static_cast< w_ImplementorType * >( i_ptr ); 01274 01275 l_impl->~w_ImplementorType(); 01276 } 01277 01278 01279 }; 01280 01281 // ======== Creator3P =============================================== 01287 template< 01288 typename w_InterfaceType, 01289 typename w_KeyType, 01290 typename w_arg_1, 01291 typename w_arg_2, 01292 typename w_arg_3 01293 > 01294 class Creator3P 01295 : public Factory_Base< w_KeyType > 01296 { 01297 public: 01298 01299 01300 // ======== Creator3P =========================================== 01307 inline Creator3P( 01308 w_KeyType i_key, 01309 const char * i_filename, 01310 int i_lineno, 01311 const char * i_description 01312 ) 01313 : Factory_Base< w_KeyType >( 01314 i_key, 01315 i_filename, 01316 i_lineno, 01317 i_description 01318 ) 01319 { 01320 } // end Creator3P 01321 01322 01323 // ======== Create ================================================ 01331 virtual w_InterfaceType * Create( w_arg_1 i_arg_1, w_arg_2 i_arg_2, w_arg_3 i_arg_3 ) = 0; 01332 01333 01334 // ======== Destroy =============================================== 01345 virtual void Destroy( w_InterfaceType * i_ptr ) = 0; 01346 01347 01348 // ======== Create ================================================ 01357 virtual w_InterfaceType * Create( 01358 w_arg_1 i_arg_1, w_arg_2 i_arg_2, w_arg_3 i_arg_3, void * i_location 01359 ) = 0; 01360 01361 01362 // ======== CreateWrapper ========================================= 01369 struct CreateWrapper 01370 : public Wrapper_Basic 01371 { 01372 01379 CreateWrapper( 01380 Creator3P * i_creator, 01381 StatusReport * o_srep = 0, 01382 FactoryTraits::FactoryCreateReportingOption i_option = FactoryTraits::AllwaysReturn 01383 ) 01384 : Wrapper_Basic( o_srep, i_option ), 01385 m_creator( i_creator ) 01386 { 01387 } 01388 01394 w_InterfaceType * operator() ( w_arg_1 i_arg_1, w_arg_2 i_arg_2, w_arg_3 i_arg_3 ) 01395 { 01396 if ( m_creator == 0 ) 01397 { 01398 ReportFactoryNotFound(); 01399 return 0; 01400 } 01401 01402 w_InterfaceType * l_interface; 01403 01404 try 01405 { 01406 l_interface = m_creator->Create( i_arg_1, i_arg_2, i_arg_3 ); 01407 } 01408 catch ( ... ) 01409 { 01410 ReportCreateException( KeyToStr( m_creator->GetKey() ) ); 01411 01412 if ( m_create_report_option == FactoryTraits::ThrowOnError ) 01413 { 01414 throw; // this should re-throw the same exception that was 01415 // caught. 01416 } 01417 01418 return 0; // finished. 01419 } 01420 01421 if ( l_interface == 0 ) 01422 { 01423 ReportFactoryFailed( KeyToStr( m_creator->GetKey() ) ); 01424 } 01425 01426 return l_interface; 01427 } 01428 01433 Creator3P * m_creator; 01434 01435 }; // end CreateWrapper 01436 01437 // 01438 // Member variables for Creator3P 01439 // 01440 01441 }; 01442 01443 01444 // ======== CreatorImpl3P =========================================== 01450 template< 01451 typename w_ImplementorType, 01452 typename w_InterfaceType, 01453 typename w_KeyType, 01454 typename w_arg_1, 01455 typename w_arg_2, 01456 typename w_arg_3 01457 > 01458 class CreatorImpl3P 01459 : public Creator3P< w_InterfaceType, w_KeyType, w_arg_1, w_arg_2, w_arg_3 > 01460 { 01461 public: 01462 01463 typedef Creator3P< w_InterfaceType, w_KeyType, w_arg_1, w_arg_2, w_arg_3 > t_CreatorType; 01464 01465 CreatorImpl3P( 01466 w_KeyType i_key, 01467 const char * i_filename, 01468 int i_lineno, 01469 const char * i_description 01470 ) 01471 : Creator3P< w_InterfaceType, w_KeyType, w_arg_1, w_arg_2, w_arg_3 >( 01472 i_key, 01473 i_filename, 01474 i_lineno, 01475 i_description 01476 ) 01477 { 01478 } 01479 01480 virtual w_InterfaceType * Create( w_arg_1 i_arg_1, w_arg_2 i_arg_2, w_arg_3 i_arg_3 ) 01481 { 01482 return new w_ImplementorType( i_arg_1, i_arg_2, i_arg_3 ); 01483 } 01484 01485 virtual int Size() 01486 { 01487 return sizeof( w_ImplementorType ); 01488 } 01489 01490 virtual w_InterfaceType * Create( w_arg_1 i_arg_1, w_arg_2 i_arg_2, w_arg_3 i_arg_3, void * i_location ) 01491 { 01492 return new ( i_location ) w_ImplementorType( i_arg_1, i_arg_2, i_arg_3 ); 01493 } 01494 01495 virtual void Destroy( w_InterfaceType * i_ptr ) 01496 { 01497 w_ImplementorType * l_impl = static_cast< w_ImplementorType * >( i_ptr ); 01498 01499 l_impl->~w_ImplementorType(); 01500 } 01501 01502 01503 }; 01504 01505 01506 // 01507 // ============================================================================ 01508 // ============================================================================ 01509 // 01510 // The basic factory class. 01511 // 01512 // ============================================================================ 01513 // ============================================================================ 01514 // 01515 01516 01517 01518 // ======== FactoryRegisterBase ======================================= 01532 class AUSTRIA_EXPORT FactoryRegisterBase 01533 : public PtrTarget_Basic 01534 { 01535 public: 01536 01537 // ======== Key =================================================== 01544 virtual const DKy * Key() = 0; 01545 01546 01547 // ======== Version =============================================== 01556 virtual int Version() = 0; 01557 01558 }; 01559 01560 01561 // ======== FactoryRegisterRegistry =================================== 01567 class AUSTRIA_EXPORT FactoryRegisterRegistry 01568 : public PtrTarget_Basic 01569 { 01570 public: 01571 01572 typedef PtrDelegate<FactoryRegisterBase *> ( * t_creator )(); 01573 01574 // ======== FindOrCreate ========================================== 01584 virtual PtrDelegate<FactoryRegisterBase *> FindOrCreate( 01585 const DKy & i_key, 01586 t_creator i_create_meth 01587 ) = 0; 01588 01589 // ======== Shutdown ============================================== 01594 virtual void Shutdown() = 0; 01595 01596 // ======== Get =================================================== 01604 static PtrView<FactoryRegisterRegistry *> Get(); 01605 }; 01606 01607 01608 // ======== FactoryRegister ======================================== 01615 template< 01616 typename w_InterfaceType, 01617 typename w_KeyType = DKy, 01618 typename w_CreatorType = Creator0P< w_InterfaceType, w_KeyType > 01619 > 01620 class FactoryRegister 01621 : public FactoryRegisterBase 01622 { 01623 private: 01624 01625 FactoryRegister( const FactoryRegister & ); 01626 01627 FactoryRegister() 01628 : m_register_key( ( typeid( FactoryRegister ) ).name() ) 01629 { 01630 } 01631 01632 const DKy m_register_key; 01633 01634 // ======== Create ================================================ 01642 static PtrDelegate<FactoryRegisterBase *> Create() 01643 { 01644 return new FactoryRegister(); 01645 } 01646 01647 01648 // ======== FactoryRegisterVersion ================================ 01655 static int FactoryRegisterVersion() 01656 { 01657 return sizeof( FactoryRegister ); 01658 } 01659 01660 // ======== Key =================================================== 01667 virtual const DKy * Key() 01668 { 01669 return & m_register_key; 01670 } 01671 01672 01673 // ======== Version =============================================== 01680 virtual int Version() 01681 { 01682 return FactoryRegisterVersion(); 01683 } 01684 01685 public: 01686 01692 struct t_MapEntry 01693 { 01700 ~t_MapEntry() 01701 { 01702 if ( m_factory != 0 ) 01703 { 01704 m_factory->RemoveEntry(); 01705 } 01706 } 01707 01712 t_MapEntry( const t_MapEntry & i_map_entry ) 01713 : m_factory( i_map_entry.Transfer() ) 01714 { 01715 } 01716 01717 t_MapEntry( w_CreatorType * m_factory = 0 ) 01718 : m_factory( m_factory ) 01719 { 01720 } 01721 01722 // ======== Transfer ========================================== 01728 w_CreatorType * Transfer() const 01729 { 01730 w_CreatorType * l_factory( m_factory ); 01731 01732 m_factory = 0; 01733 01734 return l_factory; 01735 01736 } // end Transfer 01737 01741 mutable w_CreatorType * m_factory; 01742 01743 }; 01744 01750 struct t_EntryCompare 01751 { 01752 bool operator()( const w_KeyType * s1, const w_KeyType * s2 ) const 01753 { 01754 return (*s1) < (*s2); 01755 } 01756 }; 01757 01763 typedef std::map< const w_KeyType *, t_MapEntry, t_EntryCompare > t_MapType; 01764 typedef typename t_MapType::iterator t_MapIterator; 01765 01766 01767 // ======== t_FactoryRegistryEntry ================================ 01773 class t_FactoryRegistryEntry 01774 : public FactoryRegistryEntry 01775 { 01776 public: 01777 01778 // described in FactoryRegistryEntry 01779 // 01780 virtual void RemoveAndDelete() 01781 { 01782 m_pmap->erase( m_location ); 01783 delete this; 01784 } 01785 01786 // t_FactoryRegistryEntry constructor 01787 // 01788 t_FactoryRegistryEntry( 01789 t_MapType * i_pmap, 01790 t_MapIterator i_location 01791 ) 01792 : m_pmap( i_pmap ), 01793 m_location( i_location ) 01794 { 01795 } 01796 01801 t_MapType * m_pmap; 01802 01808 t_MapIterator m_location; 01809 01810 }; 01811 01812 // ======== t_RegistrySingleton =================================== 01820 class t_RegistrySingleton 01821 { 01822 public: 01823 01824 // ======== Get =============================================== 01831 FactoryRegister & Get() 01832 { 01833 PtrView<FactoryRegister *> l_register = m_register; 01834 01835 if ( ! l_register ) 01836 { 01837 PtrView<FactoryRegisterRegistry *> l_frr = FactoryRegisterRegistry::Get(); 01838 01839 Ptr<FactoryRegisterBase *> l_val = l_frr->FindOrCreate( 01840 DKy( typeid( FactoryRegister ).name() ), 01841 & FactoryRegister::Create 01842 ); 01843 01844 // nasty down cast - right here !!! 01845 // Since we know that the pointer is being delegated, we must 01846 // make sure that the reference count is not increased. In this 01847 // case, the reference count of the pointer is not incremented at 01848 // all. 01849 m_register = PtrDelegate<FactoryRegister *>( 01850 static_cast<FactoryRegister *>( l_val.Transfer() ), 01851 false 01852 ); 01853 01854 l_register = m_register; 01855 01856 } 01857 01858 return * l_register; 01859 01860 } // end Get 01861 01862 Ptr<FactoryRegister * > m_register; 01863 01864 }; 01865 01866 01867 // ======== Get =================================================== 01881 static FactoryRegister & Get(); 01882 01883 // ======== Find ================================================== 01888 w_CreatorType * Find( const w_KeyType & i_key ) 01889 { 01890 t_MapIterator l_location = m_map.find( & i_key ); 01891 01892 if ( l_location == m_map.end() ) 01893 { 01894 return 0; 01895 } 01896 01897 return ( * l_location ).second.m_factory; 01898 } 01899 01900 01901 // ======== RegisterInsert ======================================== 01909 void RegisterInsert( w_CreatorType * i_factory ) 01910 { 01911 01912 // 01913 // attempt to insert the factory into the map 01914 // 01915 01916 std::pair<t_MapIterator, bool> l_insert_result = 01917 m_map.insert( typename t_MapType::value_type( & i_factory->GetKey(), t_MapEntry( i_factory ) ) ); 01918 01919 // 01920 // If the map already has an entry of this type - return, lack of a 01921 // m_registry_entry indicates failure. 01922 // 01923 if ( ! l_insert_result.second ) 01924 { 01925 return; 01926 } 01927 01928 // 01929 // create the registry entry - this allows correct clean-up 01930 i_factory->m_registry_entry = new t_FactoryRegistryEntry( 01931 & m_map, 01932 l_insert_result.first 01933 ); 01934 } 01935 01936 01937 // ======== Create ============================================== 01946 static typename w_CreatorType::CreateWrapper Create( 01947 const w_KeyType & i_key, 01948 StatusReport * o_srep = 0, 01949 FactoryTraits::FactoryCreateReportingOption i_option = FactoryTraits::AllwaysReturn 01950 ) 01951 { 01952 return typename w_CreatorType::CreateWrapper( 01953 Get().Find( i_key ), 01954 o_srep, 01955 i_option 01956 ); 01957 } // end Initiate 01958 01959 01964 t_MapType m_map; 01965 01966 }; 01967 01968 // ===================================================================== 01969 // 01970 // 01971 01972 #ifndef AT_Factory_Home 01973 #define AT_Factory_Home 1 01974 #endif 01975 01976 #if AT_Factory_Home 01977 01978 // ======== FactoryRegister<w_InterfaceType, w_KeyType, w_CreatorType>::Get() ========== 01988 template< 01989 typename w_InterfaceType, 01990 typename w_KeyType, 01991 typename w_CreatorType 01992 > 01993 FactoryRegister<w_InterfaceType, w_KeyType, w_CreatorType> 01994 & FactoryRegister<w_InterfaceType, w_KeyType, w_CreatorType>::Get() 01995 { 01996 static t_RegistrySingleton s_reg; 01997 01998 return s_reg.Get(); 01999 02000 } // end FactoryRegister<w_InterfaceType, w_KeyType, w_CreatorType>::Get() 02001 02002 #endif 02003 02004 // 02005 // 02006 // ===================================================================== 02007 02008 // ======== FactoryImpl0P =========================================== 02014 template< 02015 typename w_ImplementorType, 02016 typename w_InterfaceType, 02017 typename w_KeyType = DKy 02018 > 02019 class FactoryImpl0P 02020 : public CreatorImpl0P< w_ImplementorType, w_InterfaceType, w_KeyType > 02021 { 02022 public: 02023 02024 typedef typename FactoryImpl0P::t_CreatorType t_RegistryCreatorType; 02025 02026 FactoryImpl0P( 02027 w_KeyType i_key, 02028 const char * i_filename, 02029 int i_lineno, 02030 const char * i_description 02031 ) 02032 : CreatorImpl0P< w_ImplementorType, w_InterfaceType, w_KeyType >( 02033 i_key, 02034 i_filename, 02035 i_lineno, 02036 i_description 02037 ) 02038 { 02039 // 02040 // Insert this factory into the factory register. 02041 // 02042 FactoryRegister< w_InterfaceType, w_KeyType, t_RegistryCreatorType >::Get().RegisterInsert( 02043 this 02044 ); 02045 } 02046 02047 }; 02048 02049 // ======== FactoryImpl1P =========================================== 02055 template< 02056 typename w_ImplementorType, 02057 typename w_InterfaceType, 02058 typename w_KeyType, 02059 typename w_arg_1 02060 > 02061 class FactoryImpl1P 02062 : public CreatorImpl1P< w_ImplementorType, w_InterfaceType, w_KeyType, w_arg_1 > 02063 { 02064 public: 02065 02066 typedef typename FactoryImpl1P::t_CreatorType t_RegistryCreatorType; 02067 02068 FactoryImpl1P( 02069 w_KeyType i_key, 02070 const char * i_filename, 02071 int i_lineno, 02072 const char * i_description 02073 ) 02074 : CreatorImpl1P< w_ImplementorType, w_InterfaceType, w_KeyType, w_arg_1 >( 02075 i_key, 02076 i_filename, 02077 i_lineno, 02078 i_description 02079 ) 02080 { 02081 // 02082 // Insert this factory into the factory register. 02083 // 02084 FactoryRegister< w_InterfaceType, w_KeyType, t_RegistryCreatorType >::Get().RegisterInsert( 02085 this 02086 ); 02087 } 02088 02089 }; 02090 02091 // ======== FactoryImpl2P =========================================== 02097 template< 02098 typename w_ImplementorType, 02099 typename w_InterfaceType, 02100 typename w_KeyType, 02101 typename w_arg_1, 02102 typename w_arg_2 02103 > 02104 class FactoryImpl2P 02105 : public CreatorImpl2P< w_ImplementorType, w_InterfaceType, w_KeyType, w_arg_1, w_arg_2 > 02106 { 02107 public: 02108 02109 typedef typename FactoryImpl2P::t_CreatorType t_RegistryCreatorType; 02110 02111 FactoryImpl2P( 02112 w_KeyType i_key, 02113 const char * i_filename, 02114 int i_lineno, 02115 const char * i_description 02116 ) 02117 : CreatorImpl2P< w_ImplementorType, w_InterfaceType, w_KeyType, w_arg_1, w_arg_2 >( 02118 i_key, 02119 i_filename, 02120 i_lineno, 02121 i_description 02122 ) 02123 { 02124 // 02125 // Insert this factory into the factory register. 02126 // 02127 FactoryRegister< w_InterfaceType, w_KeyType, t_RegistryCreatorType >::Get().RegisterInsert( 02128 this 02129 ); 02130 } 02131 02132 }; 02133 02134 // ======== FactoryImpl3P =========================================== 02140 template< 02141 typename w_ImplementorType, 02142 typename w_InterfaceType, 02143 typename w_KeyType, 02144 typename w_arg_1, 02145 typename w_arg_2, 02146 typename w_arg_3 02147 > 02148 class FactoryImpl3P 02149 : public CreatorImpl3P< w_ImplementorType, w_InterfaceType, w_KeyType, w_arg_1, w_arg_2, w_arg_3 > 02150 { 02151 public: 02152 02153 typedef typename FactoryImpl3P::t_CreatorType t_RegistryCreatorType; 02154 02155 FactoryImpl3P( 02156 w_KeyType i_key, 02157 const char * i_filename, 02158 int i_lineno, 02159 const char * i_description 02160 ) 02161 : CreatorImpl3P< w_ImplementorType, w_InterfaceType, w_KeyType, w_arg_1, w_arg_2, w_arg_3 >( 02162 i_key, 02163 i_filename, 02164 i_lineno, 02165 i_description 02166 ) 02167 { 02168 // 02169 // Insert this factory into the factory register. 02170 // 02171 FactoryRegister< w_InterfaceType, w_KeyType, t_RegistryCreatorType >::Get().RegisterInsert( 02172 this 02173 ); 02174 } 02175 02176 }; 02177 02178 // ======== AT_FactorySymbolRef?P ===================================== 02184 #define AT_FactorySymbolRef(x_Name,x_NumParam) x_g_f ## x_Name ## _ ## x_NumParam ## P_ref 02185 02186 // ======== AT_MakeFactory?P ========================================== 02192 #define AT_MakeFactory0P(x_Key, x_Impl, x_Interface, x_KeyType) \ 02193 FactoryImpl0P<x_Impl, x_Interface, x_KeyType> \ 02194 x_g_f ## x_Impl ## _0P( x_Key, __FILE__, __LINE__, # x_Interface " " # x_Impl " " # x_Key ); \ 02195 extern "C" { void * AT_FactorySymbolRef(x_Impl,0) = static_cast<void *>( & x_g_f ## x_Impl ## _0P ); }; 02196 02197 #define AT_MakeFactory1P(x_Key, x_Impl, x_Interface, x_KeyType, x_arg1) \ 02198 FactoryImpl1P<x_Impl, x_Interface, x_KeyType, x_arg1> \ 02199 x_g_f ## x_Impl ## _1P( x_Key, __FILE__, __LINE__, # x_Interface " " # x_Impl " " # x_Key ); \ 02200 extern "C" { void * AT_FactorySymbolRef(x_Impl,1) = static_cast<void *>( & x_g_f ## x_Impl ## _1P ); }; 02201 02202 #define AT_MakeFactory2P(x_Key, x_Impl, x_Interface, x_KeyType, x_arg1, x_arg2) \ 02203 FactoryImpl2P<x_Impl, x_Interface, x_KeyType, x_arg1, x_arg2> \ 02204 x_g_f ## x_Impl ## _2P( x_Key, __FILE__, __LINE__, # x_Interface " " # x_Impl " " # x_Key ); \ 02205 extern "C" { void * AT_FactorySymbolRef(x_Impl,2) = static_cast<void *>( & x_g_f ## x_Impl ## _2P ); }; 02206 02207 #define AT_MakeFactory3P(x_Key, x_Impl, x_Interface, x_KeyType, x_arg1, x_arg2, x_arg3) \ 02208 FactoryImpl3P<x_Impl, x_Interface, x_KeyType, x_arg1, x_arg2, x_arg3> \ 02209 x_g_f ## x_Impl ## _3P( x_Key, __FILE__, __LINE__, # x_Interface " " # x_Impl " " # x_Key ); \ 02210 extern "C" { void * AT_FactorySymbolRef(x_Impl,3) = static_cast<void *>( & x_g_f ## x_Impl ## _3P ); }; 02211 02212 02213 // ======== AT_MakeNamedFactory?P ============================= 02220 #define AT_MakeNamedFactory0P(x_Name, x_Key, x_Impl, x_Interface, x_KeyType) \ 02221 FactoryImpl0P<x_Impl, x_Interface, x_KeyType> \ 02222 x_g_f ## x_Name ## _0P( x_Key, __FILE__, __LINE__, # x_Interface " " # x_Impl " " # x_Key " " # x_Name ); \ 02223 extern "C" { void * AT_FactorySymbolRef(x_Name,0) = static_cast<void *>( & x_g_f ## x_Name ## _0P ); }; 02224 02225 #define AT_MakeNamedFactory1P(x_Name, x_Key, x_Impl, x_Interface, x_KeyType, x_arg1) \ 02226 FactoryImpl1P<x_Impl, x_Interface, x_KeyType, x_arg1> \ 02227 x_g_f ## x_Name ## _1P( x_Key, __FILE__, __LINE__, # x_Interface " " # x_Impl " " # x_Key " " # x_Name ); \ 02228 extern "C" { void * AT_FactorySymbolRef(x_Name,1) = static_cast<void *>( & x_g_f ## x_Name ## _1P ); }; 02229 02230 #define AT_MakeNamedFactory2P(x_Name, x_Key, x_Impl, x_Interface, x_KeyType, x_arg1, x_arg2) \ 02231 FactoryImpl2P<x_Impl, x_Interface, x_KeyType, x_arg1, x_arg2> \ 02232 x_g_f ## x_Name ## _2P( x_Key, __FILE__, __LINE__, # x_Interface " " # x_Impl " " # x_Key " " # x_Name ); \ 02233 extern "C" { void * AT_FactorySymbolRef(x_Name,2) = static_cast<void *>( & x_g_f ## x_Name ## _2P ); }; 02234 02235 #define AT_MakeNamedFactory3P(x_Name, x_Key, x_Impl, x_Interface, x_KeyType, x_arg1, x_arg2, x_arg3) \ 02236 FactoryImpl3P<x_Impl, x_Interface, x_KeyType, x_arg1, x_arg2, x_arg3> \ 02237 x_g_f ## x_Name ## _3P( x_Key, __FILE__, __LINE__, # x_Interface " " # x_Impl " " # x_Key " " # x_Name ); \ 02238 extern "C" { void * AT_FactorySymbolRef(x_Name,3) = static_cast<void *>( & x_g_f ## x_Name ## _3P ); }; 02239 02240 02241 // ======== AT_MakeCUnitLink ================================== 02249 #define AT_MakeCUnitLink_Name(name) x_AT_CUnit_##name 02250 #define AT_MakeCUnitLink(name) int AT_MakeCUnitLink_Name(name)[1]; 02251 #define AT_MakeCUnitLinkExtern(name) extern int AT_MakeCUnitLink_Name(name)[]; 02252 02253 02254 // ======== AT_HardLinkExtern ========================================= 02260 #define AT_HardLinkExtern(x_Name,x_NumParam) \ 02261 extern "C" { extern void * AT_FactorySymbolRef(x_Name,x_NumParam); }; 02262 02263 02264 // ======== AT_HardLinkArrayElement =================================== 02270 #define AT_HardLinkArrayElement(x_Name,x_NumParam) \ 02271 & AT_FactorySymbolRef(x_Name,x_NumParam), 02272 02273 // end of GenericFactories ( Doxygen group ) 02275 02276 }; // namespace 02277 #endif // x_at_factory_h_x 02278 02279 02280
Generated for Austria by
and
MakeXS at Sun Oct 24 17:35:34 PDT 2004