19#include <boost/python/class.hpp>
20#include <boost/python/def.hpp>
26#include <frameobject.h>
56 bool const isClassFactoryAware = PyObject_HasAttrString(m_classObject.ptr(),
"_factory_use");
58 if (isClassFactoryAware) {
59 m_classObject.attr(
"_factory_use")();
61 object instance = m_classObject();
62 if (isClassFactoryAware) {
63 m_classObject.attr(
"_factory_free")();
65 auto instancePtr = extract<std::shared_ptr<IFunction>>(instance)();
66 auto *deleter = std::get_deleter<converter::shared_ptr_deleter, IFunction>(instancePtr);
85 PyObject *registered = PyList_New(0);
86 for (
const auto &name : names) {
87 PyObject *
value = to_python_value<const std::string &>()(name);
88 if (PyList_Append(registered,
value))
89 throw std::runtime_error(
"Failed to insert value into PyList");
104 PyObject *registered = PyList_New(0);
105 for (
const auto &name : names) {
108 PyObject *bkg_function = to_python_value<const std::string &>()(name);
109 if (PyList_Append(registered, bkg_function))
110 throw std::runtime_error(
"Failed to insert value into PyList");
126 PyObject *registered = PyList_New(0);
127 for (
const auto &name : names) {
130 PyObject *peak_function = to_python_value<const std::string &>()(name);
131 if (PyList_Append(registered, peak_function))
132 throw std::runtime_error(
"Failed to insert value into PyList");
148 auto peakFun = std::dynamic_pointer_cast<Mantid::API::IPeakFunction>(fun);
150 throw std::invalid_argument(name +
" is not a PeakFunction");
166 const std::string &name) {
168 auto composite = std::dynamic_pointer_cast<Mantid::API::CompositeFunction>(fun);
172 std::string error_message = name +
" is not a composite function.";
173 throw std::invalid_argument(error_message);
179std::recursive_mutex FUNCTION_REGISTER_MUTEX;
187 std::lock_guard<std::recursive_mutex> lock(FUNCTION_REGISTER_MUTEX);
188 static auto *baseClass =
189 const_cast<PyTypeObject *
>(converter::registered<IFunction>::converters.to_python_target_type());
194 const bool isSubClass = (PyObject_IsSubclass(classObject,
reinterpret_cast<PyObject *
>(baseClass)) == 1);
195 if (PyErr_Occurred() || !isSubClass) {
196 throw std::invalid_argument(std::string(
"subscribe(): Unexpected type. "
197 "Expected a class derived from "
198 "IFunction1D or IPeakFunction, "
200 classObject->ob_type->tp_name);
204 auto creator = std::make_unique<PythonObjectInstantiator<IFunction>>(object(handle<>(borrowed(classObject))));
208 auto func = creator->createInstance();
219 class_<FunctionFactoryImpl, boost::noncopyable>(
"FunctionFactoryImpl", no_init)
220 .def(
"getFunctionNames", &getFunctionNames, arg(
"self"),
"Returns a list of the currently available functions")
221 .def(
"createCompositeFunction", &createCompositeFunction, (arg(
"self"), arg(
"name")),
222 "Return a pointer to the requested function")
224 "Return a pointer to the requested function")
226 "Return a pointer to the requested function")
228 (arg(
"self"), arg(
"init_expr"), arg(
"domain_number")),
"Return a pointer to the requested function")
229 .def(
"subscribe", &subscribe, (arg(
"self"), arg(
"object")),
230 "Register a Python class derived from IFunction into the factory")
232 "Remove a type from the factory")
233 .def(
"Instance", &FunctionFactory::Instance, return_value_policy<reference_existing_object>(),
234 "Returns a reference to the FunctionFactory singleton")
235 .def(
"getBackgroundFunctionNames", &getBackgroundFunctionNames, arg(
"self"),
236 "Returns a list of the currently available background functions")
237 .def(
"getPeakFunctionNames", &getPeakFunctionNames, arg(
"self"),
238 "Returns a list of the currently available peak functions")
239 .def(
"createPeakFunction", &createPeakFunction, (arg(
"self"), arg(
"name")),
"Return pointer to peak function.")
240 .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.
Manage the lifetime of a class intended to be a singleton.
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.
RAII handler to temporarily remove and reinstall a Python trace function.
std::shared_ptr< IPeakFunction > IPeakFunction_sptr
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.