protect private data, add std::vector<IdType> RegisteredIds()

git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@356 7ec92016-0320-0410-acc4-a06ded1c099a
This commit is contained in:
syntheticpp 2005-11-12 16:52:36 +00:00
parent d45e0a1b08
commit 735cde8666
2 changed files with 85 additions and 68 deletions

View file

@ -656,39 +656,6 @@ template <typename AP, typename Id, typename P1 >
///
/// Create functions can have up to 15 parameters.
///
/// \code
/// template
/// <
/// class AbstractProduct,
/// typename IdentifierType,
/// typename CreatorParmTList = NullType,
/// template<typename, class> class FactoryErrorPolicy = ExceptionFactoryError
/// >
/// class Factory : public FactoryErrorPolicy<IdentifierType, AbstractProduct>
/// {
///
/// public:
/// typedef Functor<AbstractProduct*, CreatorParmTList> 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 <class PtrObj, typename CreaFn>
/// 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 <typename AP, typename Id, typename P1 >
>
class Factory : public FactoryErrorPolicy<IdentifierType, AbstractProduct>
{
public:
typedef Functor<AbstractProduct*, CreatorParmTList> ProductCreator;
typedef FactoryImpl< AbstractProduct, IdentifierType, CreatorParmTList > Impl;
private:
typedef AssocVector<IdentifierType, Functor<AbstractProduct*, CreatorParmTList> > 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 <typename AP, typename Id, typename P1 >
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 <typename AP, typename Id, typename P1 >
return associations_.erase(id) != 0;
}
std::vector<IdentifierType> RegisteredIds()
{
std::vector<IdentifierType> 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 <typename AP, typename Id, typename P1 >
return this->OnUnknownType(id);
}
private:
IdToProductMap associations_;
};
@ -1037,6 +1001,9 @@ template <typename AP, typename Id, typename P1 >
#endif // FACTORY_INC_
// $Log$
// Revision 1.11 2005/11/12 16:52:36 syntheticpp
// protect private data, add std::vector<IdType> RegisteredIds()
//
// Revision 1.10 2005/11/03 12:43:35 syntheticpp
// more doxygen documentation, modules added
//

View file

@ -14,6 +14,7 @@
#define USE_SEQUENCE
#include <iostream>
#include <string>
#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<Product> 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<Product>::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<Product>::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<Product>::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 <crtdbg.h>
#include <cassert>
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<std::string> ids = PFactory::Instance().RegisteredIds();
for(std::vector<std::string>::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<IdType> 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
//