20#include <boost/python/class.hpp>
21#include <boost/python/def.hpp>
27#include <frameobject.h>
58 bool const isClassFactoryAware = PyObject_HasAttrString(m_classObject.ptr(),
"_factory_use");
60 if (isClassFactoryAware) {
61 m_classObject.attr(
"_factory_use")();
63 object instance = m_classObject();
64 if (isClassFactoryAware) {
65 m_classObject.attr(
"_factory_free")();
67 auto instancePtr = extract<std::shared_ptr<IFunction>>(instance)();
68 auto *deleter = std::get_deleter<converter::shared_ptr_deleter, IFunction>(instancePtr);
85 std::vector<std::string> names;
92 PyObject *registered = PyList_New(0);
93 for (
const auto &
name : names) {
94 PyObject *
value = to_python_value<const std::string &>()(
name);
95 if (PyList_Append(registered,
value))
96 throw std::runtime_error(
"Failed to insert value into PyList");
111 PyObject *registered = PyList_New(0);
112 for (
const auto &
name : names) {
115 PyObject *bkg_function = to_python_value<const std::string &>()(
name);
116 if (PyList_Append(registered, bkg_function))
117 throw std::runtime_error(
"Failed to insert value into PyList");
133 PyObject *registered = PyList_New(0);
134 for (
const auto &
name : names) {
137 PyObject *peak_function = to_python_value<const std::string &>()(
name);
138 if (PyList_Append(registered, peak_function))
139 throw std::runtime_error(
"Failed to insert value into PyList");
155 auto peakFun = std::dynamic_pointer_cast<Mantid::API::IPeakFunction>(fun);
157 throw std::invalid_argument(
name +
" is not a PeakFunction");
173 const std::string &
name) {
175 auto composite = std::dynamic_pointer_cast<Mantid::API::CompositeFunction>(fun);
179 std::string error_message =
name +
" is not a composite function.";
180 throw std::invalid_argument(error_message);
186std::recursive_mutex FUNCTION_REGISTER_MUTEX;
194 std::lock_guard<std::recursive_mutex> lock(FUNCTION_REGISTER_MUTEX);
195 static auto *baseClass =
196 const_cast<PyTypeObject *
>(converter::registered<IFunction>::converters.to_python_target_type());
201 const bool isSubClass = (PyObject_IsSubclass(classObject,
reinterpret_cast<PyObject *
>(baseClass)) == 1);
202 if (PyErr_Occurred() || !isSubClass) {
203 throw std::invalid_argument(std::string(
"subscribe(): Unexpected type. "
204 "Expected a class derived from "
205 "IFunction1D or IPeakFunction, "
207 classObject->ob_type->tp_name);
211 auto creator = std::make_unique<PythonObjectInstantiator<IFunction>>(object(handle<>(borrowed(classObject))));
215 auto func = creator->createInstance();
226 class_<FunctionFactoryImpl, boost::noncopyable>(
"FunctionFactoryImpl", no_init)
227 .def(
"getFunctionNames", &getFunctionNames, arg(
"self"),
"Returns a list of the currently available functions")
228 .def(
"createCompositeFunction", &createCompositeFunction, (arg(
"self"), arg(
"name")),
229 "Return a pointer to the requested function")
231 "Return a pointer to the requested function")
233 "Return a pointer to the requested function")
235 (arg(
"self"), arg(
"init_expr"), arg(
"domain_number")),
"Return a pointer to the requested function")
236 .def(
"subscribe", &subscribe, (arg(
"self"), arg(
"object")),
237 "Register a Python class derived from IFunction into the factory")
239 "Remove a type from the factory")
240 .def(
"Instance", &FunctionFactory::Instance, return_value_policy<reference_existing_object>(),
241 "Returns a reference to the FunctionFactory singleton")
242 .def(
"getBackgroundFunctionNames", &getBackgroundFunctionNames, arg(
"self"),
243 "Returns a list of the currently available background functions")
244 .def(
"getPeakFunctionNames", &getPeakFunctionNames, arg(
"self"),
245 "Returns a list of the currently available peak functions")
246 .def(
"createPeakFunction", &createPeakFunction, (arg(
"self"), arg(
"name")),
"Return pointer to peak function.")
247 .staticmethod(
"Instance");
double value
The value of the point.
#define GET_POINTER_SPECIALIZATION(TYPE)
void export_FunctionFactory()
The FunctionFactory class is in charge of the creation of concrete instances of fitting functions.
std::shared_ptr< IFunction > createFunction(const std::string &type) const
Creates an instance of a function.
std::shared_ptr< MultiDomainFunction > createInitializedMultiDomainFunction(const std::string &input, size_t domainNumber) const
Creates an instnce of an inizialised multidomain function where each domain has the same function.
std::vector< std::string > getFunctionNames() const
Query available functions based on the template type.
void unsubscribe(const std::string &className)
void subscribe(const std::string &className, std::unique_ptr< AbstractFactory > pAbstractFactory, Kernel::DynamicFactory< IFunction >::SubscribeAction replace=ErrorIfExists)
std::shared_ptr< IFunction > createInitialized(const std::string &input) const
Creates an instance of a function.
An interface to a background function.
This is an interface to a fitting function - a semi-abstarct class.
An interface to a peak function, which extend the interface of IFunctionWithLocation by adding method...
A composite function defined on a CompositeDomain.
Defines a structure for acquiring/releasing the Python GIL using the RAII pattern.
std::shared_ptr< Base > createInstance() const override
Creates an instance of the object as shared_ptr to the Base type.
Defines a structure for releasing the Python GIL using the RAII pattern.
RAII handler to temporarily remove and reinstall a Python trace function.
std::shared_ptr< IPeakFunction > IPeakFunction_sptr
Mantid::Kernel::SingletonHolder< FunctionFactoryImpl > FunctionFactory
std::shared_ptr< CompositeFunction > CompositeFunction_sptr
shared pointer to the composite function base class
Special shared_ptr::deleter object that locks the GIL while deleting the underlying Python object.