16#include <boost/python/class.hpp>
17#include <boost/python/def.hpp>
18#include <boost/python/dict.hpp>
19#include <boost/python/list.hpp>
20#include <boost/python/overloads.hpp>
26#include <frameobject.h>
50 const auto keys = self->
getKeys(includeHidden);
52 for (
const auto &key : keys) {
53 std::pair<std::string, int> algInfo;
59 object name(handle<>(to_python_value<const std::string &>()(algInfo.first)));
60 object ver(handle<>(to_python_value<const int &>()(algInfo.second)));
64 if (inventory.has_key(
name)) {
65 versions = inventory.get(
name);
68 inventory[
name] = versions;
70 versions.attr(
"append")(ver);
80list getDescriptors(
AlgorithmFactoryImpl const *
const self,
bool includeHidden =
false,
bool includeAlias =
false) {
81 const auto descriptors = self->
getDescriptors(includeHidden, includeAlias);
83 for (
const auto &descr : descriptors) {
84 pyDescriptors.append(boost::python::object(descr));
100 dict pythonCategories;
101 for (
auto &it : categories) {
102 object categoryName(handle<>(to_python_value<const std::string &>()(it.first)));
103 pythonCategories[categoryName] = it.second;
106 return pythonCategories;
114std::recursive_mutex PYALG_REGISTER_MUTEX;
126 std::lock_guard<std::recursive_mutex> lock(PYALG_REGISTER_MUTEX);
128 static auto *
const pyAlgClass = (PyObject *)converter::registered<Algorithm>::converters.to_python_target_type();
130 PyObject *classObject(
nullptr);
131 if (PyObject_IsInstance(
obj.ptr(), pyAlgClass)) {
132 classObject = PyObject_GetAttrString(
obj.ptr(),
"__class__");
133 }
else if (PyObject_IsSubclass(
obj.ptr(), pyAlgClass)) {
134 classObject =
obj.ptr();
137 throw std::invalid_argument(
"Cannot register an algorithm that does not derive from Algorithm.");
139 boost::python::object classType(handle<>(borrowed(classObject)));
141 std::unique_ptr<Mantid::Kernel::AbstractInstantiator<Algorithm>> temp =
142 std::make_unique<PythonObjectInstantiator<Algorithm>>(classType);
143 auto descr = self.subscribe(std::move(temp), AlgorithmFactoryImpl::OverwriteCurrent);
147 FileLoaderRegistry::Instance().unsubscribe(descr.first, descr.second);
156BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(existsOverloader, exists, 1, 2)
157BOOST_PYTHON_FUNCTION_OVERLOADS(getDescriptors_overloads, getDescriptors, 1, 3)
168 class_<AlgorithmDescriptor>(
"AlgorithmDescriptor")
174 class_<AlgorithmFactoryImpl, boost::noncopyable>(
"AlgorithmFactoryImpl", no_init)
176 existsOverloader((arg(
"name"), arg(
"version") = -1),
"Returns true if the given algorithm exists with "
177 "an option to specify the version"))
179 .def(
"getRegisteredAlgorithms", &getRegisteredAlgorithms, (arg(
"self"), arg(
"include_hidden")),
180 "Returns a Python dictionary of currently registered algorithms")
182 "Returns the highest version of the named algorithm. Throws "
183 "ValueError if no algorithm can be found")
184 .def(
"subscribe", &subscribe, (arg(
"self"), arg(
"object")),
185 "Register a Python class derived from "
186 "PythonAlgorithm into the factory")
187 .def(
"getDescriptors", &getDescriptors,
188 getDescriptors_overloads((arg(
"self"), arg(
"include_hidden") =
false, arg(
"include_alias") =
false),
189 "Return a list of descriptors of registered algorithms. Each "
190 "descriptor is a list: [name, version, category, alias]."))
191 .def(
"getCategoriesandState", &getCategoriesandState,
192 "Return the categories of the algorithms. This includes those within "
193 "the Factory itself and any cleanly constructed algorithms stored "
196 "Returns the highest version of the named algorithm. Throws "
197 "ValueError if no algorithm can be found")
198 .def(
"enableNotifications", &AlgorithmFactoryImpl::enableNotifications)
199 .def(
"disableNotifications", &AlgorithmFactoryImpl::disableNotifications)
201 .def(
"Instance", &AlgorithmFactory::Instance, return_value_policy<reference_existing_object>(),
202 "Returns a reference to the AlgorithmFactory singleton")
203 .staticmethod(
"Instance");
#define GET_POINTER_SPECIALIZATION(TYPE)
void export_AlgorithmFactory()
double obj
the value of the quadratic function
#define GNU_DIAG_OFF(x)
This is a collection of macros for turning compiler warnings off in a controlled manner.
The AlgorithmFactory class is in charge of the creation of concrete instances of Algorithms.
bool exists(const std::string &algorithmName, const int version=-1)
Does an algorithm of the given name and version exist.
int highestVersion(const std::string &algorithmName) const
Returns the highest version of the algorithm currently registered.
std::vector< AlgorithmDescriptor > getDescriptors(bool includeHidden=false, bool includeAliases=false) const
Returns algorithm descriptors.
void unsubscribe(const std::string &algorithmName, const int version)
Unsubscribe the given algorithm.
std::pair< std::string, int > decodeName(const std::string &mangledName) const
unmangles the names used as keys into the name and version
const std::vector< std::string > getKeys() const override
Get the algorithm names and version - mangled use decodeName to separate.
const std::map< std::string, bool > getCategoriesWithState() const
Get the algorithm categories.
Defines a structure for releasing the Python GIL using the RAII pattern.
RAII handler to temporarily remove and reinstall a Python trace function.
std::string category
category
std::string name
Algorithm Name.