Mantid
Loading...
Searching...
No Matches
FunctionFactory.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"
16#include <vector>
17
18#include <mutex>
19
20namespace Mantid {
21
22namespace API {
23
24//----------------------------------------------------------------------
25// More forward declarations
26//----------------------------------------------------------------------
27class IFunction;
28class CompositeFunction;
29class Expression;
30class MultiDomainFunction;
31
43class MANTID_API_DLL FunctionFactoryImpl final : public Kernel::DynamicFactory<IFunction> {
44public:
51 std::shared_ptr<IFunction> createFunction(const std::string &type) const;
52
54 std::shared_ptr<IFunction> createInitialized(const std::string &input) const;
55
58 std::shared_ptr<MultiDomainFunction> createInitializedMultiDomainFunction(const std::string &input,
59 size_t domainNumber) const;
60
62 template <typename FunctionType> std::vector<std::string> getFunctionNames() const;
64 std::vector<std::string> getFunctionNamesGUI() const;
65 // Unhide the base class version (to satisfy the intel compiler)
66 using Kernel::DynamicFactory<IFunction>::subscribe;
67 void subscribe(const std::string &className, std::unique_ptr<AbstractFactory> pAbstractFactory,
69
70 void unsubscribe(const std::string &className);
71
72private:
74
78 ~FunctionFactoryImpl() override = default;
80 using Kernel::DynamicFactory<IFunction>::create;
81 using Kernel::DynamicFactory<IFunction>::createUnwrapped;
82
84 std::shared_ptr<IFunction> createSimple(const Expression &expr,
85 std::map<std::string, std::string> &parentAttributes) const;
86 std::shared_ptr<IPeakFunction> createPeakFunction(const Expression &expr, const std::string &name) const;
88 std::shared_ptr<CompositeFunction> createComposite(const Expression &expr,
89 std::map<std::string, std::string> &parentAttributes) const;
90
92 void inputError(const std::string &str = "") const;
94 void addConstraints(const std::shared_ptr<IFunction> &fun, const Expression &expr) const;
96 void addConstraint(const std::shared_ptr<IFunction> &fun, const Expression &expr) const;
98 void addConstraint(const std::shared_ptr<IFunction> &fun, const Expression &constraint_expr,
99 const Expression &penalty_expr) const;
101 void addTies(const std::shared_ptr<IFunction> &fun, const Expression &expr) const;
103 void addTie(const std::shared_ptr<IFunction> &fun, const Expression &expr) const;
104
105 mutable std::map<std::string, std::vector<std::string>> m_cachedFunctionNames;
106 mutable std::mutex m_mutex;
107};
108
114template <typename FunctionType> std::vector<std::string> FunctionFactoryImpl::getFunctionNames() const {
115 std::lock_guard<std::mutex> _lock(m_mutex);
116
117 const std::string soughtType(typeid(FunctionType).name());
118 if (m_cachedFunctionNames.find(soughtType) != m_cachedFunctionNames.end()) {
119 return m_cachedFunctionNames[soughtType];
120 }
121
122 // Create the entry in the cache and work with it directly
123 std::vector<std::string> &typeNames = m_cachedFunctionNames[soughtType];
124 const std::vector<std::string> names = this->getKeys();
125 std::copy_if(names.cbegin(), names.cend(), std::back_inserter(typeNames), [this](const std::string &name) {
126 std::shared_ptr<IFunction> func = this->createFunction(name);
127 return std::dynamic_pointer_cast<FunctionType>(func);
128 });
129 return typeNames;
130}
131
133
135using FunctionFactoryUpdateNotification = FunctionFactoryImpl::UpdateNotification;
137using FunctionFactoryUpdateNotification_ptr = const Poco::AutoPtr<FunctionFactoryUpdateNotification> &;
138} // namespace API
139} // namespace Mantid
140
141namespace Mantid {
142namespace Kernel {
143EXTERN_MANTID_API template class MANTID_API_DLL Mantid::Kernel::SingletonHolder<Mantid::API::FunctionFactoryImpl>;
144}
145} // namespace Mantid
146
151#define DECLARE_FUNCTION(classname) \
152 namespace { \
153 Mantid::Kernel::RegistrationHelper \
154 register_function_##classname(((Mantid::API::FunctionFactory::Instance().subscribe<classname>(#classname)), 0)); \
155 }
This class represents an expression made up of names, binary operators and brackets.
Definition: Expression.h:36
The FunctionFactory class is in charge of the creation of concrete instances of fitting functions.
~FunctionFactoryImpl() override=default
Private Destructor.
FunctionFactoryImpl(const FunctionFactoryImpl &)=delete
std::shared_ptr< IPeakFunction > createPeakFunction(const Expression &expr, const std::string &name) const
std::vector< std::string > getFunctionNames() const
Query available functions based on the template type.
std::map< std::string, std::vector< std::string > > m_cachedFunctionNames
FunctionFactoryImpl & operator=(const FunctionFactoryImpl &)=delete
This is an interface to a fitting function - a semi-abstarct class.
Definition: IFunction.h:163
The dynamic factory is a base dynamic factory for serving up objects in response to requests from oth...
virtual const std::vector< std::string > getKeys() const
Returns the keys in the map.
SubscribeAction
Defines replacement behaviour.
Manage the lifetime of a class intended to be a singleton.
const Poco::AutoPtr< FunctionFactoryUpdateNotification > & FunctionFactoryUpdateNotification_ptr
Convenient typedef for an UpdateNotification AutoPtr.
FunctionFactoryImpl::UpdateNotification FunctionFactoryUpdateNotification
Convenient typedef for an UpdateNotification.
Helper class which provides the Collimation Length for SANS instruments.
Policy class controlling creation of the singleton Implementation classes should mark their default c...