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 <memory>
16#include <optional>
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 auto nameAndVersion = std::make_pair(className, version);
100
101 if (!alias.empty())
102 m_amap[alias] = nameAndVersion;
103
104 return nameAndVersion;
105 }
107 void unsubscribe(const std::string &algorithmName, const int version);
109 bool exists(const std::string &algorithmName, const int version = -1);
110
112 const std::vector<std::string> getKeys() const override;
113 const std::vector<std::string> getKeys(bool includeHidden) const;
114
116 std::optional<std::pair<std::string, int>> getRealNameFromAlias(const std::string &alias) const noexcept;
117
119 int highestVersion(const std::string &algorithmName) const;
120
122 const std::unordered_set<std::string> getCategories(bool includeHidden = false) const;
123
125 const std::map<std::string, bool> getCategoriesWithState() const;
126
128 std::vector<AlgorithmDescriptor> getDescriptors(bool includeHidden = false, bool includeAliases = false) const;
129
131 std::pair<std::string, int> decodeName(const std::string &mangledName) const;
132
133private:
135
137 const std::string extractAlgName(const std::shared_ptr<IAlgorithm> &alg) const;
139 int extractAlgVersion(const std::shared_ptr<IAlgorithm> &alg) const;
141 const std::string extractAlgAlias(const std::shared_ptr<IAlgorithm> &alg) const;
142
144 std::shared_ptr<Algorithm> createAlgorithm(const std::string &name, const int version) const;
145
151 std::string createName(const std::string &, const int &) const;
153 void fillHiddenCategories(std::unordered_set<std::string> *categorySet) const;
154
156 using VersionMap = std::map<std::string, int>;
160 using AliasMap = std::unordered_map<std::string, std::pair<std::string, int>>;
163};
164
166
170 const Poco::AutoPtr<Mantid::Kernel::DynamicFactory<Algorithm>::UpdateNotification> &;
171
172} // namespace API
173} // namespace Mantid
174
175namespace Mantid {
176namespace Kernel {
177EXTERN_MANTID_API template class MANTID_API_DLL Mantid::Kernel::SingletonHolder<Mantid::API::AlgorithmFactoryImpl>;
178}
179} // namespace Mantid
std::string name
Definition Run.cpp:60
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::pair< std::string, int > > 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:76
IAlgorithm is the interface implemented by the Algorithm base class.
Definition IAlgorithm.h:45
The base class for instantiators.
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
Mantid::Kernel::SingletonHolder< AlgorithmFactoryImpl > AlgorithmFactory
bool exists(Nexus::File &file, const std::string &name)
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...