DynamicFactory.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // DynamicFactory.h
  3. //
  4. // $Id: //poco/1.4/Foundation/include/Poco/DynamicFactory.h#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Core
  8. // Module: DynamicFactory
  9. //
  10. // Definition of the DynamicFactory class.
  11. //
  12. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // SPDX-License-Identifier: BSL-1.0
  16. //
  17. #ifndef Foundation_DynamicFactory_INCLUDED
  18. #define Foundation_DynamicFactory_INCLUDED
  19. #include "Poco/Foundation.h"
  20. #include "Poco/Instantiator.h"
  21. #include "Poco/Exception.h"
  22. #include "Poco/Mutex.h"
  23. #include <map>
  24. #include <memory>
  25. namespace Poco {
  26. template <class Base>
  27. class DynamicFactory
  28. /// A factory that creates objects by class name.
  29. {
  30. public:
  31. typedef AbstractInstantiator<Base> AbstractFactory;
  32. DynamicFactory()
  33. /// Creates the DynamicFactory.
  34. {
  35. }
  36. ~DynamicFactory()
  37. /// Destroys the DynamicFactory and deletes the instantiators for
  38. /// all registered classes.
  39. {
  40. for (typename FactoryMap::iterator it = _map.begin(); it != _map.end(); ++it)
  41. {
  42. delete it->second;
  43. }
  44. }
  45. Base* createInstance(const std::string& className) const
  46. /// Creates a new instance of the class with the given name.
  47. /// The class must have been registered with registerClass.
  48. /// If the class name is unknown, a NotFoundException is thrown.
  49. {
  50. FastMutex::ScopedLock lock(_mutex);
  51. typename FactoryMap::const_iterator it = _map.find(className);
  52. if (it != _map.end())
  53. return it->second->createInstance();
  54. else
  55. throw NotFoundException(className);
  56. }
  57. template <class C>
  58. void registerClass(const std::string& className)
  59. /// Registers the instantiator for the given class with the DynamicFactory.
  60. /// The DynamicFactory takes ownership of the instantiator and deletes
  61. /// it when it's no longer used.
  62. /// If the class has already been registered, an ExistsException is thrown
  63. /// and the instantiator is deleted.
  64. {
  65. registerClass(className, new Instantiator<C, Base>);
  66. }
  67. void registerClass(const std::string& className, AbstractFactory* pAbstractFactory)
  68. /// Registers the instantiator for the given class with the DynamicFactory.
  69. /// The DynamicFactory takes ownership of the instantiator and deletes
  70. /// it when it's no longer used.
  71. /// If the class has already been registered, an ExistsException is thrown
  72. /// and the instantiator is deleted.
  73. {
  74. poco_check_ptr (pAbstractFactory);
  75. FastMutex::ScopedLock lock(_mutex);
  76. std::auto_ptr<AbstractFactory> ptr(pAbstractFactory);
  77. typename FactoryMap::iterator it = _map.find(className);
  78. if (it == _map.end())
  79. _map[className] = ptr.release();
  80. else
  81. throw ExistsException(className);
  82. }
  83. void unregisterClass(const std::string& className)
  84. /// Unregisters the given class and deletes the instantiator
  85. /// for the class.
  86. /// Throws a NotFoundException if the class has not been registered.
  87. {
  88. FastMutex::ScopedLock lock(_mutex);
  89. typename FactoryMap::iterator it = _map.find(className);
  90. if (it != _map.end())
  91. {
  92. delete it->second;
  93. _map.erase(it);
  94. }
  95. else throw NotFoundException(className);
  96. }
  97. bool isClass(const std::string& className) const
  98. /// Returns true iff the given class has been registered.
  99. {
  100. FastMutex::ScopedLock lock(_mutex);
  101. return _map.find(className) != _map.end();
  102. }
  103. private:
  104. DynamicFactory(const DynamicFactory&);
  105. DynamicFactory& operator = (const DynamicFactory&);
  106. typedef std::map<std::string, AbstractFactory*> FactoryMap;
  107. FactoryMap _map;
  108. mutable FastMutex _mutex;
  109. };
  110. } // namespace Poco
  111. #endif // Foundation_DynamicFactory_INCLUDED