31 #ifndef PTLIB_FACTORY_H 32 #define PTLIB_FACTORY_H 45 #pragma warning(disable:4786) 122 class FactoryMap :
public std::map<std::string, PFactoryBase *>
143 template <
class AbstractClass,
typename KeyType = PDefaultPFactoryKey>
162 : m_type(singleton ? DynamicSingleton : NonSingleton)
163 , m_singletonInstance(NULL)
167 : m_type(delSingleton ? DynamicSingleton : StaticSingleton)
168 , m_singletonInstance(instance)
178 if (m_type == NonSingleton)
181 if (m_singletonInstance == NULL)
182 m_singletonInstance = Create(key);
183 return m_singletonInstance;
186 virtual Abstract_T *
Create(
const Key_T & )
const 188 PAssert(this->m_type == StaticSingleton,
"Incorrect factory worker descendant");
189 return this->m_singletonInstance;
194 if (m_type == DynamicSingleton) {
195 delete m_singletonInstance;
196 m_singletonInstance = NULL;
205 template <
class ConcreteClass>
209 Worker(
const Key_T & key,
bool singleton =
false)
217 virtual Abstract_T *
Create(
const Key_T & )
const 219 return new ConcreteClass;
228 return GetInstance().Register_Internal(key, worker);
231 static bool Register(
const Key_T & key, Abstract_T * instance,
bool autoDeleteInstance =
true)
234 if (GetInstance().Register_Internal(key, worker))
242 return GetInstance().RegisterAs_Internal(newKey, oldKey);
247 GetInstance().Unregister_Internal(key);
252 GetInstance().UnregisterAll_Internal();
257 return GetInstance().IsRegistered_Internal(key);
262 return GetInstance().CreateInstance_Internal(key);
265 template <
class Derived_T>
268 return dynamic_cast<Derived_T *
>(GetInstance().CreateInstance_Internal(key));
273 return GetInstance().IsSingleton_Internal(key);
278 return GetInstance().GetKeyList_Internal();
283 return GetInstance().m_keyMap;
288 return GetInstance().m_mutex;
293 for (
typename KeyMap_T::const_iterator it = m_keyMap.begin(); it != m_keyMap.end(); ++it)
294 it->second->DestroySingleton();
308 std::string className =
typeid(
PFactory).name();
311 FactoryMap::const_iterator entry = factories.find(className);
312 if (entry != factories.end()) {
313 PAssert(entry->second != NULL,
"Factory map returned NULL for existing key");
322 factories[className] = factory;
330 if (m_keyMap.find(key) != m_keyMap.end())
339 if (m_keyMap.find(oldKey) == m_keyMap.end())
341 m_keyMap[newKey] = m_keyMap[oldKey];
362 return m_keyMap.find(key) != m_keyMap.end();
368 typename KeyMap_T::const_iterator entry = m_keyMap.find(key);
369 if (entry != m_keyMap.end())
370 return entry->second->CreateInstance(key);
377 if (m_keyMap.find(key) == m_keyMap.end())
379 return m_keyMap[key]->IsSingleton();
386 typename KeyMap_T::const_iterator entry;
387 for (entry = m_keyMap.begin(); entry != m_keyMap.end(); ++entry)
388 list.push_back(entry->first);
411 #define PFACTORY_CREATE(factory, ConcreteClass, ...) \ 412 namespace PFactoryLoader { \ 413 int ConcreteClass##_link() { return 0; } \ 414 factory::Worker<ConcreteClass> ConcreteClass##_instance(__VA_ARGS__); \ 417 #define PFACTORY_CREATE_SINGLETON(factory, ConcreteClass) \ 418 PFACTORY_CREATE(factory, ConcreteClass, typeid(ConcreteClass).name(), true) 420 #define PFACTORY_GET_SINGLETON(factory, ConcreteClass) \ 421 static ConcreteClass & GetInstance() { \ 422 return *factory::CreateInstanceAs<ConcreteClass>(typeid(ConcreteClass).name()); \ 431 #define PFACTORY_LOAD(ConcreteType) \ 432 namespace PFactoryLoader { \ 433 extern int ConcreteType##_link(); \ 434 int const ConcreteType##_loader = ConcreteType##_link(); \ 438 #endif // PTLIB_FACTORY_H static PBoolean RegisterAs(const Key_T &newKey, const Key_T &oldKey)
Definition: pfactory.h:240
Types
Definition: pfactory.h:153
This class waits for the semaphore on construction and automatically signals the semaphore on destruc...
Definition: psync.h:86
std::map< Key_T, WorkerBase * > KeyMap_T
Definition: pfactory.h:223
PFactory()
Definition: pfactory.h:298
WorkerBase(bool singleton=false)
Definition: pfactory.h:161
static KeyMap_T & GetKeyMap()
Definition: pfactory.h:281
Definition: pfactory.h:122
Definition: pfactory.h:155
static PMutex & GetMutex()
Definition: pfactory.h:286
virtual void DestroySingletons()=0
bool IsRegistered_Internal(const Key_T &key)
Definition: pfactory.h:359
void UnregisterAll_Internal()
Definition: pfactory.h:352
static void Unregister(const Key_T &key)
Definition: pfactory.h:245
Abstract_T * CreateInstance_Internal(const Key_T &key)
Definition: pfactory.h:365
AbstractClass Abstract_T
Definition: pfactory.h:148
Abstract_T * m_singletonInstance
Definition: pfactory.h:159
bool Register_Internal(const Key_T &key, WorkerBase *worker)
Definition: pfactory.h:327
KeyMap_T m_keyMap
Definition: pfactory.h:392
PMutex m_mutex
Definition: pfactory.h:133
std::string PDefaultPFactoryKey
These templates implement an Abstract Factory that allows creation of a class "factory" that can be u...
Definition: pfactory.h:101
Worker(const Key_T &key, bool singleton=false)
Definition: pfactory.h:209
~PFactory()
Definition: pfactory.h:301
BOOL PBoolean
Definition: object.h:102
static Abstract_T * CreateInstance(const Key_T &key)
Definition: pfactory.h:260
bool IsSingleton_Internal(const Key_T &key)
Definition: pfactory.h:374
KeyList_T GetKeyList_Internal()
Definition: pfactory.h:382
virtual void DestroySingletons()
Definition: pfactory.h:291
static void UnregisterAll()
Definition: pfactory.h:250
static FactoryMap & GetFactories()
WorkerBase(Abstract_T *instance, bool delSingleton=true)
Definition: pfactory.h:166
#define PAssertNULL(ptr)
This macro is used to assert that a pointer must be non-null.
Definition: object.h:220
Definition: pfactory.h:206
virtual ~PFactoryBase()
Definition: pfactory.h:117
#define PMEMORY_IGNORE_ALLOCATIONS_FOR_SCOPE
Definition: object.h:953
static bool Register(const Key_T &key, WorkerBase *worker)
Definition: pfactory.h:226
PFactoryBase()
Definition: pfactory.h:114
FactoryMap()
Definition: pfactory.h:125
static bool IsRegistered(const Key_T &key)
Definition: pfactory.h:255
KeyType Key_T
Definition: pfactory.h:147
Template class for generic factories of an abstract class.
Definition: pfactory.h:144
Abstract_T * CreateInstance(const Key_T &key)
Definition: pfactory.h:176
virtual Abstract_T * Create(const Key_T &) const
Definition: pfactory.h:186
static KeyList_T GetKeyList()
Definition: pfactory.h:276
#define PAssert(b, msg)
This macro is used to assert that a condition must be true.
Definition: object.h:192
static PBoolean IsSingleton(const Key_T &key)
Definition: pfactory.h:271
bool IsSingleton() const
Definition: pfactory.h:200
Base class for generic factories.
Definition: pfactory.h:111
Definition: pfactory.h:154
static bool Register(const Key_T &key, Abstract_T *instance, bool autoDeleteInstance=true)
Definition: pfactory.h:231
static PFactory & GetInstance()
Definition: pfactory.h:306
Definition: pfactory.h:150
void Unregister_Internal(const Key_T &key)
Definition: pfactory.h:345
virtual ~WorkerBase()
Definition: pfactory.h:171
std::vector< Key_T > KeyList_T
Definition: pfactory.h:224
static PMutex & GetFactoriesMutex()
virtual void DestroySingleton()
Definition: pfactory.h:192
static Derived_T * CreateInstanceAs(const Key_T &key)
Definition: pfactory.h:266
#define PNEW
Macro for overriding system default new operator.
Definition: object.h:890
virtual Abstract_T * Create(const Key_T &) const
Definition: pfactory.h:217
PBoolean RegisterAs_Internal(const Key_T &newKey, const Key_T &oldKey)
Definition: pfactory.h:336