13#include "MantidKernel/DllConfig.h"
19#include <Poco/Notification.h>
20#include <Poco/NotificationCenter.h>
64template <
class Base,
class Comparator = CaseInsensitiveStringComparator>
class DynamicFactory {
99 virtual std::shared_ptr<Base>
create(
const std::string &className)
const {
100 auto it =
_map.find(className);
101 if (it !=
_map.end())
102 return it->second->createInstance();
117 auto it =
_map.find(className);
118 if (it !=
_map.end())
119 return it->second->createUnwrappedInstance();
130 template <
class C>
void subscribe(
const std::string &className) {
145 void subscribe(
const std::string &className, std::unique_ptr<AbstractFactory> pAbstractFactory,
147 if (className.empty()) {
148 throw std::invalid_argument(
"Cannot register empty class name");
151 auto it =
_map.find(className);
153 _map[className] = std::move(pAbstractFactory);
156 throw std::runtime_error(className +
" is already registered.\n");
165 auto it =
_map.find(className);
166 if (!className.empty() && it !=
_map.end()) {
177 bool exists(
const std::string &className)
const {
return _map.find(className) !=
_map.end(); }
181 virtual const std::vector<std::string>
getKeys()
const {
182 std::vector<std::string> names;
183 names.reserve(
_map.size());
185 _map.cbegin(),
_map.cend(), std::back_inserter(names),
186 [](
const std::pair<
const std::string, std::unique_ptr<AbstractFactory>> &mapPair) { return mapPair.first; });
210 using FactoryMap = std::map<std::string, std::unique_ptr<AbstractFactory>, Comparator>;
Base class for dynamic factory notifications.
~DynamicFactoryNotification() override
A notification that the factory has been updated.
~DynamicFactoryUpdateNotification() override
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.