Mantid
Loading...
Searching...
No Matches
AlgorithmFactory.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2007 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7#pragma once
8
9//----------------------------------------------------------------------
10// Includes
11//----------------------------------------------------------------------
12#include "MantidAPI/DllConfig.h"
15#include <boost/optional.hpp>
16#include <memory>
17#include <sstream>
18#include <unordered_map>
19#include <unordered_set>
20#include <vector>
21
22namespace Mantid {
23namespace API {
24
28 std::string name;
29 int version;
30 std::string category;
31 std::string alias;
32};
33
34//----------------------------------------------------------------------
35// Forward declarations
36//----------------------------------------------------------------------
37class IAlgorithm;
38class Algorithm;
39
48class MANTID_API_DLL AlgorithmFactoryImpl final : public Kernel::DynamicFactory<Algorithm> {
49public:
52 // Unhide the base class version (to satisfy the intel compiler)
53 using Kernel::DynamicFactory<Algorithm>::create;
55 std::shared_ptr<Algorithm> create(const std::string &, const int &) const;
56
59 template <class C> std::pair<std::string, int> subscribe() {
60 std::unique_ptr<Kernel::AbstractInstantiator<Algorithm>> newI =
61 std::make_unique<Kernel::Instantiator<C, Algorithm>>();
62 return this->subscribe(std::move(newI));
63 }
64
73 template <class T>
74 std::pair<std::string, int> subscribe(std::unique_ptr<Kernel::AbstractInstantiator<T>> instantiator,
75 const SubscribeAction replaceExisting = ErrorIfExists) {
76 const auto tempAlg = instantiator->createInstance();
77 const int version = extractAlgVersion(tempAlg);
78 const std::string className = extractAlgName(tempAlg);
79 const std::string alias = extractAlgAlias(tempAlg);
80 typename VersionMap::const_iterator it = m_vmap.find(className);
81 if (!className.empty()) {
82 const std::string key = createName(className, version);
83 if (it == m_vmap.end()) {
84 m_vmap[className] = version;
85 } else {
86 if (version == it->second && replaceExisting == ErrorIfExists) {
87 std::ostringstream os;
88 os << "Cannot register algorithm " << className << " twice with the same version\n";
89 throw std::runtime_error(os.str());
90 }
91 if (version > it->second) {
92 m_vmap[className] = version;
93 }
94 }
95 Kernel::DynamicFactory<Algorithm>::subscribe(key, std::move(instantiator), replaceExisting);
96 } else {
97 throw std::invalid_argument("Cannot register empty algorithm name");
98 }
99
100 if (!alias.empty())
101 m_amap[alias] = className;
102
103 return std::make_pair(className, version);
104 }
106 void unsubscribe(const std::string &algorithmName, const int version);
108 bool exists(const std::string &algorithmName, const int version = -1);
109
111 const std::vector<std::string> getKeys() const override;
112 const std::vector<std::string> getKeys(bool includeHidden) const;
113
115 boost::optional<std::string> getRealNameFromAlias(const std::string &alias) const noexcept;
116
118 int highestVersion(const std::string &algorithmName) const;
119
121 const std::unordered_set<std::string> getCategories(bool includeHidden = false) const;
122
124 const std::map<std::string, bool> getCategoriesWithState() const;
125
127 std::vector<AlgorithmDescriptor> getDescriptors(bool includeHidden = false, bool includeAliases = false) const;
128
130 std::pair<std::string, int> decodeName(const std::string &mangledName) const;
131
132private:
134
136 const std::string extractAlgName(const std::shared_ptr<IAlgorithm> &alg) const;
138 int extractAlgVersion(const std::shared_ptr<IAlgorithm> &alg) const;
140 const std::string extractAlgAlias(const std::shared_ptr<IAlgorithm> &alg) const;
141
143 std::shared_ptr<Algorithm> createAlgorithm(const std::string &name, const int version) const;
144
150 std::string createName(const std::string &, const int &) const;
152 void fillHiddenCategories(std::unordered_set<std::string> *categorySet) const;
153
155 using VersionMap = std::map<std::string, int>;
159 using AliasMap = std::unordered_map<std::string, std::string>;
162};
163
165
169 const Poco::AutoPtr<Mantid::Kernel::DynamicFactory<Algorithm>::UpdateNotification> &;
170
171} // namespace API
172} // namespace Mantid
173
174namespace Mantid {
175namespace Kernel {
176EXTERN_MANTID_API template class MANTID_API_DLL Mantid::Kernel::SingletonHolder<Mantid::API::AlgorithmFactoryImpl>;
177}
178} // namespace Mantid
The AlgorithmFactory class is in charge of the creation of concrete instances of Algorithms.
VersionMap m_vmap
The map holding the registered class names and their highest versions.
std::unordered_map< std::string, std::string > AliasMap
A typedef for the map of algorithm aliases.
std::map< std::string, int > VersionMap
A typedef for the map of algorithm versions.
AlgorithmFactoryImpl & operator=(const AlgorithmFactoryImpl &)=delete
~AlgorithmFactoryImpl() override
Private Destructor.
AlgorithmFactoryImpl(const AlgorithmFactoryImpl &)=delete
AliasMap m_amap
The map holding the alias names of registered algorithms.
std::pair< std::string, int > subscribe(std::unique_ptr< Kernel::AbstractInstantiator< T > > instantiator, const SubscribeAction replaceExisting=ErrorIfExists)
Subscribes an algorithm using a custom instantiator.
std::pair< std::string, int > subscribe()
algorithm factory specific function to subscribe algorithms, calls the dynamic factory subscribe func...
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:85
IAlgorithm is the interface implemented by the Algorithm base class.
Definition: IAlgorithm.h:45
The base class for instantiators.
Definition: Instantiator.h:25
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...
SubscribeAction
Defines replacement behaviour.
Manage the lifetime of a class intended to be a singleton.
const Poco::AutoPtr< Mantid::Kernel::DynamicFactory< Algorithm >::UpdateNotification > & AlgorithmFactoryUpdateNotification_ptr
bool exists(::NeXus::File &file, const std::string &name)
Based on the current group in the file, does the named sub-entry exist?
std::unique_ptr< T > create(const P &parent, const IndexArg &indexArg, const HistArg &histArg)
This is the create() method that all the other create() methods call.
Helper class which provides the Collimation Length for SANS instruments.
Structure uniquely describing an algorithm with its name, category and version.
std::string name
Algorithm Name.
Policy class controlling creation of the singleton Implementation classes should mark their default c...