1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
|
////////////////////////////////////////////////////////////////////////////////
// The Loki Library
// Copyright (c) 2005 by Peter Kuemmel
//
// Code covered by the MIT License
// The authors make no representations about the suitability of this software
// for any purpose. It is provided "as is" without express or implied warranty.
////////////////////////////////////////////////////////////////////////////////
// $Id: Factory.cpp 823 2007-05-08 10:48:40Z lfittl $
//#define CLASS_LEVEL_THERADING
#define USE_SEQUENCE
#include <iostream>
#include <string>
#include <loki/Factory.h>
#include <loki/Functor.h>
#ifdef LOKI_DISABLE_TYPELIST_MACROS
#define USE_WQUENCE
#endif
#ifdef USE_SEQUENCE
#include <loki/Sequence.h>
#endif
using namespace Loki;
using std::cout;
using std::endl;
////////////////////////////////////////////
// Object to create: Product
// Constructor with 0 and 2 arguments
////////////////////////////////////////////
class AbstractProduct{};
class Product : public AbstractProduct
{
public:
Product(){}
Product( int, int ){}
};
///////////////////////////////////////////////////////////
// Factory for creation a Product object without parameters
///////////////////////////////////////////////////////////
typedef SingletonHolder
<
Factory< AbstractProduct, int >,
CreateUsingNew,
Loki::LongevityLifetime::DieAsSmallObjectChild
>
PFactoryNull;
/////////////////////////////////////////////////////////////
// Factory for creation a Product object with 2 parameters
/////////////////////////////////////////////////////////////
typedef SingletonHolder
<
#ifndef USE_SEQUENCE
Factory< AbstractProduct, std::string, LOKI_TYPELIST_2( int, int ) >,
#else
Factory< AbstractProduct, std::string, Seq< int, int > >,
#endif
CreateUsingNew,
Loki::LongevityLifetime::DieAsSmallObjectChild
>
PFactory;
////////////////////////////////////////////////////
// Creator functions with different names
////////////////////////////////////////////////////
Product* createProductNull()
{
cout << "createProductNull()" << endl;
return new Product;
}
Product* createProductParm( int a, int b )
{
cout << "createProductParm( int a, int b ) " << endl;
return new Product( a, b );
}
///////////////////////////////////////////////////
// Overloaded creator functions
///////////////////////////////////////////////////
Product* createProductOver()
{
cout << "createProductOver()" << endl;
return new Product;
}
Product* createProductOver( int a, int b )
{
cout << "createProductOver( int a, int b )" << endl;
return new Product( a, b );
}
///////////////////////////////////////////////////
// Creator functions are polymorphic member functions
///////////////////////////////////////////////////
class AbstractCreator{
public:
virtual ~AbstractCreator() {}
virtual AbstractProduct* create() = 0;
virtual AbstractProduct* createParm( int, int ) = 0;
};
class Creator : public AbstractCreator{
public:
Creator(){}
AbstractProduct* create()
{
cout << "Creator::create()" << endl;
return new Product;
}
AbstractProduct* createParm( int a, int b )
{
cout << "Creator::create( int a, int b )" << endl;
return new Product( a,b );
}
};
///////////////////////////////////////////////////////////////
// Creator functions are member functions of a template class
///////////////////////////////////////////////////////////////
template< class T>
class CreatorT{
public:
CreatorT(){}
T* create()
{
cout << "CreatorT<T>::create()" << endl;
return new T;
}
T* createParm( int a, int b )
{
cout << "CreatorT<T>::create( int a, int b )" << endl;
return new T( a,b );
}
};
///////////////////////////////////////////////////////////////
// get creator functions on runntime
///////////////////////////////////////////////////////////////
#ifndef USE_SEQUENCE
typedef Functor<Product*,LOKI_TYPELIST_2(int,int)> CreateFunctor;
#else
typedef Functor<Product*,Seq<int,int> > CreateFunctor;
#endif
typedef
SingletonHolder
<
#ifndef USE_SEQUENCE
Factory< AbstractProduct, int,LOKI_TYPELIST_3(CreateFunctor,int,int) >,
#else
Factory< AbstractProduct, int,Seq<CreateFunctor,int,int> >,
#endif
CreateUsingNew,
Loki::LongevityLifetime::DieAsSmallObjectChild
>
PFactoryFunctorParm;
Product* createProductRuntime(CreateFunctor func, int a, int b)
{
Product *p = func( a, b );
cout << " called by createProductRuntime(CreateFunctor func, int a, int b) " << endl;
return p;
}
///////////////////////////////////////
// Register creator functions
// No additional typdefs are necessary!
//////////////////////////////////////
Creator creator;
AbstractCreator* c = &creator;
CreatorT<Product> cT;
bool reg()
{
bool const ok1 = PFactoryNull::Instance().Register( 1, createProductNull );
bool const ok2 = PFactoryNull::Instance().Register( 2, static_cast<Product*(*)()>(createProductOver) );
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( "One", createProductParm );
bool const ok6 = PFactory::Instance().Register( "Two", static_cast<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;
}
////////////////////////////////////////////////////////////////////////////////////
//
// 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;
cout << endl << "creator function is a simple function:" << endl;
p= PFactoryNull::Instance().CreateObject( 1 );
delete p;
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( "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( "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( "Four", 64,64 );
delete p;
CreateFunctor func1(createProductParm);
CreateFunctor func2(&cT, &CreatorT<Product>::createParm);
cout << endl << "creator function is a functor parameter" << endl;
p= PFactoryFunctorParm::Instance().CreateObject( 1, func1, 64,64 );
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;
#if defined(__BORLANDC__) || defined(_MSC_VER)
system("PAUSE");
#endif
return EXIT_SUCCESS;
}
|