From 735cde86661d4bd56430f6e4e7fd4cfcfa0dd98a Mon Sep 17 00:00:00 2001 From: syntheticpp Date: Sat, 12 Nov 2005 16:52:36 +0000 Subject: [PATCH] protect private data, add std::vector RegisteredIds() git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@356 7ec92016-0320-0410-acc4-a06ded1c099a --- include/loki/Factory.h | 67 ++++++++----------------------- test/Factory/Factory.cpp | 86 +++++++++++++++++++++++++++++++--------- 2 files changed, 85 insertions(+), 68 deletions(-) diff --git a/include/loki/Factory.h b/include/loki/Factory.h index 67c1466..ddddf49 100644 --- a/include/loki/Factory.h +++ b/include/loki/Factory.h @@ -656,39 +656,6 @@ template /// /// Create functions can have up to 15 parameters. /// -/// \code -/// template -/// < -/// class AbstractProduct, -/// typename IdentifierType, -/// typename CreatorParmTList = NullType, -/// template class FactoryErrorPolicy = ExceptionFactoryError -/// > -/// class Factory : public FactoryErrorPolicy -/// { -/// -/// public: -/// typedef Functor ProductCreator; -/// typedef FactoryImpl< AbstractProduct, IdentifierType, CreatorParmTList > Impl; -/// -/// typedef typename Impl::Parm1Parm1; -/// // ... up to 15 Parameters -/// -/// typedef typename IdToProductMap::iterator iterator; -/// iterator begin(); -/// iterator end(); -/// -/// bool Register(const IdentifierType& id, ProductCreator creator); -/// bool Unregister(const IdentifierType& id); -/// -/// template -/// bool Register(const IdentifierType& id, const PtrObj& p, CreaFn fn); -/// -/// AbstractProduct* CreateObject(const IdentifierType& id); -/// AbstractProduct* CreateObject(const IdentifierType& id, Parm1 p1); -/// // ... up to 15 Parameters -/// }; -/// \endcode //////////////////////////////////////////////////////////////////////////////// template < @@ -699,16 +666,16 @@ template > class Factory : public FactoryErrorPolicy { - - - public: typedef Functor ProductCreator; typedef FactoryImpl< AbstractProduct, IdentifierType, CreatorParmTList > Impl; - private: typedef AssocVector > IdToProductMap; + typedef typename IdToProductMap::iterator iterator; + + IdToProductMap associations_; public: + typedef typename Impl::Parm1 Parm1; typedef typename Impl::Parm2 Parm2; typedef typename Impl::Parm3 Parm3; @@ -734,17 +701,6 @@ template associations_.erase(associations_.begin(), associations_.end()); } - typedef typename IdToProductMap::iterator iterator; - - iterator begin() - { - return associations_.begin(); - } - iterator end() - { - return associations_.end(); - } - bool Register(const IdentifierType& id, ProductCreator creator) { return associations_.insert( @@ -765,6 +721,16 @@ template return associations_.erase(id) != 0; } + std::vector RegisteredIds() + { + std::vector ids; + for(iterator it = associations_.begin(); it != associations_.end();++it) + { + ids.push_back(it->first); + } + return ids; + } + AbstractProduct* CreateObject(const IdentifierType& id) { typename IdToProductMap::iterator i = associations_.find(id); @@ -922,8 +888,6 @@ template return this->OnUnknownType(id); } - private: - IdToProductMap associations_; }; @@ -1037,6 +1001,9 @@ template #endif // FACTORY_INC_ // $Log$ +// Revision 1.11 2005/11/12 16:52:36 syntheticpp +// protect private data, add std::vector RegisteredIds() +// // Revision 1.10 2005/11/03 12:43:35 syntheticpp // more doxygen documentation, modules added // diff --git a/test/Factory/Factory.cpp b/test/Factory/Factory.cpp index 6ee4549..6ca9b35 100755 --- a/test/Factory/Factory.cpp +++ b/test/Factory/Factory.cpp @@ -14,6 +14,7 @@ #define USE_SEQUENCE #include +#include #include "loki/Factory.h" #include "loki/Functor.h" @@ -64,9 +65,9 @@ PFactoryNull; typedef SingletonHolder < #ifndef USE_SEQUENCE - Factory< AbstractProduct, int, LOKI_TYPELIST_2( int, int ) >, +Factory< AbstractProduct, std::string, LOKI_TYPELIST_2( int, int ) >, #else - Factory< AbstractProduct, int, Seq< int, int > >, +Factory< AbstractProduct, std::string, Seq< int, int > >, #endif CreateUsingNew, Loki::LongevityLifetime::DieAsSmallObjectChild @@ -180,15 +181,13 @@ Product* createProductRuntime(CreateFunctor func, int a, int b) } - - - /////////////////////////////////////// // Register creator functions // No additional typdefs are necessary! ////////////////////////////////////// -AbstractCreator* c = new Creator; +Creator creator; +AbstractCreator* c = &creator; CreatorT cT; bool reg() @@ -198,44 +197,79 @@ bool reg() bool const ok3 = PFactoryNull::Instance().Register( 3, c, &AbstractCreator::create ); bool const ok4 = PFactoryNull::Instance().Register( 4, &cT, &CreatorT::create ); - bool const ok5 = PFactory::Instance().Register( 1, createProductParm ); - bool const ok6 = PFactory::Instance().Register( 2, (Product*(*)(int,int))createProductOver ); - bool const ok7 = PFactory::Instance().Register( 3, c, &AbstractCreator::createParm ); - bool const ok8 = PFactory::Instance().Register( 4, &cT, &CreatorT::createParm ); + bool const ok5 = PFactory::Instance().Register( "One", createProductParm ); + bool const ok6 = PFactory::Instance().Register( "Two", (Product*(*)(int,int))createProductOver ); + bool const ok7 = PFactory::Instance().Register( "Three", c, &AbstractCreator::createParm ); + bool const ok8 = PFactory::Instance().Register( "Four", &cT, &CreatorT::createParm ); bool const ok9 = PFactoryFunctorParm::Instance().Register( 1, createProductRuntime ); return ok1 && ok2 && ok3 && ok4 && ok5 && ok6 && ok7 && ok8 && ok9; } - -int main(int argc, char *argv[]) + + +//////////////////////////////////////////////////////////////////////////////////// +// +// detect memory leaks on MSVC Ide +// +//////////////////////////////////////////////////////////////////////////////////// + +//#define MSVC_DETECT_MEMORY_LEAKS +#ifdef MSVC_DETECT_MEMORY_LEAKS + +#include +#include + +void heap_debug() { + int tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG ); + + // Turn on leak-checking bit + tmpFlag |= _CRTDBG_LEAK_CHECK_DF; + + //tmpFlag |= _CRTDBG_CHECK_MasterLWMasterYS_DF; + + // Turn off CRT block checking bit + tmpFlag &= ~_CRTDBG_CHECK_CRT_DF; + + // Set flag to the new value + _CrtSetDbgFlag( tmpFlag ); +} +#else +void heap_debug() +{} +#endif + +int main() +{ + heap_debug(); + reg(); - AbstractProduct* p; + AbstractProduct* p; cout << endl << "creator function is a simple function:" << endl; p= PFactoryNull::Instance().CreateObject( 1 ); delete p; - p= PFactory::Instance().CreateObject( 1, 64,64 ); + p= PFactory::Instance().CreateObject( "One", 64,64 ); delete p; cout << endl << "creator function is a overloaded function:" << endl; p= PFactoryNull::Instance().CreateObject( 2 ); delete p; - p= PFactory::Instance().CreateObject( 2, 64,64 ); + p= PFactory::Instance().CreateObject( "Two", 64,64 ); delete p; cout << endl << "creator function is a member function:" << endl; p= PFactoryNull::Instance().CreateObject( 3 ); delete p; - p= PFactory::Instance().CreateObject( 3, 64,64 ); + p= PFactory::Instance().CreateObject( "Three", 64,64 ); delete p; cout << endl << "creator function is a template member function" << endl; p= PFactoryNull::Instance().CreateObject( 4 ); delete p; - p= PFactory::Instance().CreateObject( 4, 64,64 ); + p= PFactory::Instance().CreateObject( "Four", 64,64 ); delete p; CreateFunctor func1(createProductParm); @@ -245,14 +279,30 @@ int main(int argc, char *argv[]) delete p; p= PFactoryFunctorParm::Instance().CreateObject( 1, func2, 64,64 ); delete p; - + + + cout << endl; + cout << "Registered ids: \n"; + + std::vector ids = PFactory::Instance().RegisteredIds(); + + for(std::vector::iterator it=ids.begin(); it!=ids.end(); ++it) + cout << *it << "\n"; + + cout << endl; cout << endl; + system("PAUSE"); + return EXIT_SUCCESS; } + // $Log$ +// Revision 1.9 2005/11/12 16:52:36 syntheticpp +// protect private data, add std::vector RegisteredIds() +// // Revision 1.8 2005/11/07 12:06:43 syntheticpp // change lifetime policy DieOrder to a msvc7.1 compilable version. Make this the default lifetime for SmallObject //