27 #ifndef PTLIB_FACTORY_H
28 #define PTLIB_FACTORY_H
42 #pragma warning(disable:4786)
138 return dynamic_cast<TheFactory&
>(
InternalGetFactory(
typeid(TheFactory).name(), CreateFactory<TheFactory>));
152 template <
class AbstractClass,
typename ParamType,
typename KeyType>
238 it->second.m_worker->DestroySingleton();
254 typename WorkerMap_T::iterator it =
m_workers.find(key);
256 return it->second.m_worker == worker;
267 typename WorkerMap_T::iterator it =
m_workers.find(key);
269 return it->second.m_worker->m_singletonInstance == instance;
272 m_workers.insert(make_pair(key, WorkerWrap(
PNEW WorkerBase(instance, autoDeleteInstance),
true)));
279 typename WorkerMap_T::iterator itOld =
m_workers.find(oldKey);
283 typename WorkerMap_T::iterator itNew =
m_workers.find(newKey);
285 return itNew->second.m_worker == itOld->second.m_worker;
287 m_workers.insert(make_pair(newKey, WorkerWrap(itOld->second.m_worker,
false)));
294 typename WorkerMap_T::iterator it =
m_workers.find(key);
296 if (it->second.m_autoDelete)
297 delete it->second.m_worker;
307 if (it->second == instance) {
318 for (
typename WorkerMap_T::iterator it =
m_workers.begin(); it !=
m_workers.end(); ++it) {
319 if (it->second.m_autoDelete)
320 delete it->second.m_worker;
336 return entry ==
m_workers.end() ? NULL : entry->second.m_worker->CreateInstance(param);
343 if (entry !=
m_workers.end() && !entry->second.m_worker->IsSingleton())
351 return entry !=
m_workers.end() && entry->second.m_worker->IsSingleton();
359 list.push_back(entry->first);
378 #define PFACTORY_STATICS(cls) \
379 static cls & GetFactory() { return PFactoryBase::GetFactoryAs<cls>(); } \
380 static bool Register(const Key_T & k, WorkerBase_T *w, bool a=false) { return GetFactory().InternalRegister(k, w, a); } \
381 static bool Register(const Key_T & k, Abstract_T * i, bool a = true) { return GetFactory().InternalRegister(k, i, a); } \
382 static bool RegisterAs(const Key_T & synonym, const Key_T & original){ return GetFactory().InternalRegisterAs(synonym, original); } \
383 static void Unregister(const Key_T & k) { GetFactory().InternalUnregister(k); } \
384 static void Unregister(WorkerBase_T * a) { GetFactory().InternalUnregister(a); } \
385 static void UnregisterAll() { GetFactory().InternalUnregisterAll(); } \
386 static bool IsRegistered(const Key_T & k) { return GetFactory().InternalIsRegistered(k); } \
387 static bool IsSingleton(const Key_T & k) { return GetFactory().InternalIsSingleton(k); } \
388 static typename Base_T::KeyList_T GetKeyList() { return GetFactory().InternalGetKeyList(); } \
389 static PMutex & GetMutex() { return GetFactory().m_mutex; } \
395 template <
class AbstractClass,
typename KeyType = PDefaultPFactoryKey>
407 template <
class ConcreteClass>
415 , m_key(new
Key_T(key))
417 PAssert(Register(key,
this),
"Factory Worker already registered");
434 return new ConcreteClass();
440 return GetFactory().InternalCreateInstance(key, key);
443 template <
class Derived_T>
446 Abstract_T * instance = GetFactory().InternalCreateInstance(key, key);
447 Derived_T * derived =
dynamic_cast<Derived_T *
>(instance);
451 GetFactory().InternalDestroy(key, instance);
460 template <
class AbstractClass,
typename ParamType,
typename KeyType = PDefaultPFactoryKey>
472 template <
class ConcreteClass>
480 , m_key(new
Key_T(key))
482 PAssert(Register(key,
this),
"Factory Worker already registered");
499 return new ConcreteClass(param);
505 return GetFactory().InternalCreateInstance(key, param);
508 template <
class Derived_T>
511 Abstract_T * instance = GetFactory().InternalCreateInstance(key, param);
512 Derived_T * derived =
dynamic_cast<Derived_T *
>(instance);
515 GetFactory().InternalDestroy(key, instance);
532 #define PFACTORY_CREATE(factory, ConcreteClass, ...) \
533 namespace PFactoryLoader { \
534 int ConcreteClass##_link(int const *) { return 0; } \
535 factory::Worker<ConcreteClass> ConcreteClass##_instance(__VA_ARGS__); \
538 #define PFACTORY_SYNONYM(factory, ConcreteClass, name, key) \
539 namespace PFactoryLoader { \
540 bool ConcreteClass##name##_synonym = factory::RegisterAs(key, ConcreteClass##_instance.GetKey()); \
543 #define PFACTORY_CREATE_SINGLETON(factory, ConcreteClass) \
544 PFACTORY_CREATE(factory, ConcreteClass, typeid(ConcreteClass).name(), true)
546 #define PFACTORY_GET_SINGLETON(factory, ConcreteClass) \
547 static ConcreteClass & GetInstance() { \
548 return *factory::CreateInstanceAs<ConcreteClass>(typeid(ConcreteClass).name()); \
557 #define PFACTORY_LOAD(ConcreteType) \
558 namespace PFactoryLoader { \
559 extern int ConcreteType##_link(int const *); \
560 static int const ConcreteType##_loader = ConcreteType##_link(&ConcreteType##_loader); \
564 #endif // PTLIB_FACTORY_H
static Abstract_T * CreateInstance(const Key_T &key, Param_T param)
Definition: pfactory.h:503
WorkerWrap(WorkerBase *worker, bool autoDelete)
Definition: pfactory.h:165
Definition: pfactory.h:163
This class waits for the semaphore on construction and automatically signals the semaphore on destruc...
Definition: psync.h:115
Base_T::Key_T Key_T
Definition: pfactory.h:403
Base_T::Key_T Key_T
Definition: pfactory.h:468
void InternalUnregister(WorkerBase *instance)
Definition: pfactory.h:303
Template class for generic factories of an abstract class.
Definition: pfactory.h:153
static PFactoryBase & InternalGetFactory(const std::string &className, PFactoryBase *(*createFactory)())
Definition: pfactory.h:122
virtual void DestroySingletons()=0
virtual Abstract_T * Create(Param_T) const
Definition: pfactory.h:219
void InternalUnregister(const Key_T &key)
Definition: pfactory.h:291
This class implements critical section mutexes using the most efficient mechanism available on the ho...
Definition: mutex.h:270
Base_T::Param_T Param_T
Definition: pfactory.h:402
virtual Abstract_T * Create(Param_T param) const
Definition: pfactory.h:497
WorkerBase(bool singleton=false)
Definition: pfactory.h:181
bool InternalIsRegistered(const Key_T &key)
Definition: pfactory.h:326
KeyList_T InternalGetKeyList()
Definition: pfactory.h:354
WorkerBase(Abstract_T *instance, bool delSingleton=true)
Definition: pfactory.h:187
PFactoryTemplate< AbstractClass, ParamType, KeyType > Base_T
Definition: pfactory.h:464
WorkerMap_T m_workers
Definition: pfactory.h:364
Base_T::WorkerBase WorkerBase_T
Definition: pfactory.h:400
virtual Abstract_T * Create(Param_T) const
Definition: pfactory.h:432
std::vector< Key_T > KeyList_T
Definition: pfactory.h:161
std::map< Key_T, WorkerWrap > WorkerMap_T
Definition: pfactory.h:169
static TheFactory & GetFactoryAs()
Definition: pfactory.h:136
std::string PDefaultPFactoryKey
this define the default class to be used for keys into PFactories
Definition: pfactory.h:375
Definition: pfactory.h:172
Definition: pfactory.h:473
virtual void DestroySingletons()
Definition: pfactory.h:235
Worker(const Key_T &key, bool singleton=false)
Definition: pfactory.h:413
Types m_type
Definition: pfactory.h:225
static Abstract_T * CreateInstance(const Key_T &key)
Definition: pfactory.h:438
Class for a factory to create concrete class instances which have a single parameter during construct...
Definition: pfactory.h:461
Definition: pfactory.h:178
virtual void DestroySingleton()
Definition: pfactory.h:208
void InternalDestroy(const Key_T &key, Abstract_T *instance)
Definition: pfactory.h:339
PFactoryTemplate< AbstractClass, const KeyType &, KeyType > Base_T
Definition: pfactory.h:399
Abstract_T * InternalCreateInstance(const Key_T &key, Param_T param)
Definition: pfactory.h:332
static FactoryMap & GetFactories()
Base_T::Param_T Param_T
Definition: pfactory.h:467
Definition: pfactory.h:176
AbstractClass Abstract_T
Definition: pfactory.h:156
bool InternalIsSingleton(const Key_T &key)
Definition: pfactory.h:347
#define PAssertNULL(ptr)
This macro is used to assert that a pointer must be non-null.
Definition: object.h:428
~Worker()
Definition: pfactory.h:420
PDECLARE_MUTEX(m_mutex,"PFactoryBase")
bool PBoolean
Definition: object.h:174
Definition: pfactory.h:408
Worker(const Key_T &key, bool singleton=false)
Definition: pfactory.h:478
virtual ~PFactoryBase()
Definition: pfactory.h:117
#define PMEMORY_IGNORE_ALLOCATIONS_FOR_SCOPE
Definition: object.h:1961
Base_T::Abstract_T Abstract_T
Definition: pfactory.h:466
PFactoryBase()
Definition: pfactory.h:114
Abstract_T * CreateInstance(Param_T param)
Definition: pfactory.h:198
const Key_T & GetKey() const
Definition: pfactory.h:429
Base_T::WorkerBase WorkerBase_T
Definition: pfactory.h:465
PFactoryTemplate()
Definition: pfactory.h:242
Definition: pfactory.h:177
void InternalUnregisterAll()
Definition: pfactory.h:315
static Derived_T * CreateInstanceAs(const Key_T &key, Param_T param)
Definition: pfactory.h:509
static PFactoryBase * CreateFactory()
Definition: pfactory.h:134
bool IsSingleton() const
Definition: pfactory.h:216
const Key_T & GetKey() const
Definition: pfactory.h:494
Class for a factory to create concrete class instances without parameters during construction.
Definition: pfactory.h:396
Base_T::Abstract_T Abstract_T
Definition: pfactory.h:401
ParamType Param_T
Definition: pfactory.h:159
Abstract_T * m_singletonInstance
Definition: pfactory.h:226
virtual ~WorkerBase()
Definition: pfactory.h:193
PBoolean InternalRegisterAs(const Key_T &newKey, const Key_T &oldKey)
Definition: pfactory.h:276
~Worker()
Definition: pfactory.h:485
#define PAssert(b, msg)
This macro is used to assert that a condition must be true.
Definition: object.h:400
bool InternalRegister(const Key_T &key, WorkerBase *worker, bool autoDeleteWorker)
Definition: pfactory.h:251
Base class for generic factories.
Definition: pfactory.h:111
bool m_autoDelete
Definition: pfactory.h:167
~PFactoryTemplate()
Definition: pfactory.h:245
WorkerMap_T::iterator WorkerIter_T
Definition: pfactory.h:170
PFACTORY_STATICS(PParamFactory)
bool InternalRegister(const Key_T &key, Abstract_T *instance, bool autoDeleteInstance)
Definition: pfactory.h:264
static Derived_T * CreateInstanceAs(const Key_T &key)
Definition: pfactory.h:444
PFACTORY_STATICS(PFactory)
Types
Definition: pfactory.h:175
#define PNEW
Macro for overriding system default new operator.
Definition: object.h:1896
KeyType Key_T
Definition: pfactory.h:160
WorkerBase * m_worker
Definition: pfactory.h:166