13#include "MantidKernel/DllConfig.h"
19#include <Poco/Notification.h>
20#include <Poco/NotificationCenter.h>
47template <
class Base,
class Comparator = CaseInsensitiveStringComparator>
class DynamicFactory {
89 virtual std::shared_ptr<Base>
create(
const std::string &className)
const {
90 auto it =
_map.find(className);
92 return it->second->createInstance();
107 auto it =
_map.find(className);
108 if (it !=
_map.end())
109 return it->second->createUnwrappedInstance();
120 template <
class C>
void subscribe(
const std::string &className) {
135 void subscribe(
const std::string &className, std::unique_ptr<AbstractFactory> pAbstractFactory,
137 if (className.empty()) {
138 throw std::invalid_argument(
"Cannot register empty class name");
141 auto it =
_map.find(className);
143 _map[className] = std::move(pAbstractFactory);
146 throw std::runtime_error(className +
" is already registered.\n");
155 auto it =
_map.find(className);
156 if (!className.empty() && it !=
_map.end()) {
167 bool exists(
const std::string &className)
const {
return _map.find(className) !=
_map.end(); }
171 virtual const std::vector<std::string>
getKeys()
const {
172 std::vector<std::string> names;
173 names.reserve(
_map.size());
175 _map.cbegin(),
_map.cend(), std::back_inserter(names),
176 [](
const std::pair<
const std::string, std::unique_ptr<AbstractFactory>> &mapPair) { return mapPair.first; });
200 using FactoryMap = std::map<std::string, std::unique_ptr<AbstractFactory>, Comparator>;
Base class for dynamic factory notifications.
A notification that the factory has been updated.
The dynamic factory is a base dynamic factory for serving up objects in response to requests from oth...
FactoryMap _map
The map holding the registered class names and their instantiators.
virtual Base * createUnwrapped(const std::string &className) const
Creates a new instance of the class with the given name, which is not wrapped in a boost shared_ptr.
std::map< std::string, std::unique_ptr< AbstractFactory >, Comparator > FactoryMap
A typedef for the map of registered classes.
DynamicFactory(const DynamicFactory &)=delete
void subscribe(const std::string &className)
Registers the instantiator for the given class with the DynamicFactory.
DynamicFactory & operator=(const DynamicFactory &)=delete
void unsubscribe(const std::string &className)
Unregisters the given class and deletes the instantiator for the class.
virtual std::shared_ptr< Base > create(const std::string &className) const
Creates a new instance of the class with the given name.
DynamicFactory()
Protected constructor for base class.
virtual const std::vector< std::string > getKeys() const
Returns the keys in the map.
void sendUpdateNotificationIfEnabled()
Send an update notification if they are enabled.
NotificationStatus
Defines the whether notifications are dispatched.
virtual ~DynamicFactory()=default
Destroys the DynamicFactory and deletes the instantiators for all registered classes.
NotificationStatus m_notifyStatus
Flag marking whether we should dispatch notifications.
SubscribeAction
Defines replacement behaviour.
void sendUpdateNotification()
Send an update notification.
bool exists(const std::string &className) const
Returns true if the given class is currently registered.
void subscribe(const std::string &className, std::unique_ptr< AbstractFactory > pAbstractFactory, SubscribeAction replace=ErrorIfExists)
Registers the instantiator for the given class with the DynamicFactory.
void enableNotifications()
Enable notifications.
Poco::NotificationCenter notificationCenter
Sends notifications to observers.
void disableNotifications()
Disable notifications.
Exception for when an item is not found in a collection.
The instantiator is a generic class for creating objects of the template type.
std::less< std::string > CaseSensitiveStringComparator
Helper class which provides the Collimation Length for SANS instruments.