Mantid
Loading...
Searching...
No Matches
PythonObjectProperty.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 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 +
9
13
14#ifndef Q_MOC_RUN
15#include <boost/python/bases.hpp>
16#include <boost/python/class.hpp>
17#include <boost/python/return_by_value.hpp>
18#include <boost/python/return_value_policy.hpp>
19#endif
20
21#include <boost/python/default_call_policies.hpp>
22#include <boost/python/make_constructor.hpp>
23
31using namespace boost::python;
32
33namespace {
34
35PythonObjectProperty *createPythonObjectProperty(std::string const &name, boost::python::object const &value,
36 IValidator_sptr const &validator, unsigned int const direction) {
37 return new PythonObjectProperty(name, value, validator, direction);
38}
39} // namespace
40
42 // export base class
43 using BaseValueType = boost::python::object;
44 PropertyWithValueExporter<BaseValueType>::define("PythonObjectPropertyWithValue");
45
46 // leaf class type
47 using BaseClassType = PythonObjectProperty::BaseClass;
48 class_<PythonObjectProperty, bases<BaseClassType>, boost::noncopyable>("PythonObjectProperty", no_init)
49 // name and direction
50 .def(init<const std::string &, const unsigned int>(
51 (arg("self"), arg("name"), arg("direction") = Direction::Input), "Construct a PythonObjectProperty"))
52
53 // name, validator, and direction
54 .def(init<const std::string &, IValidator_sptr, const unsigned int>(
55 (arg("self"), arg("name"), arg("validator"), arg("direction") = Direction::Input),
56 "Construct a PythonObjectProperty with a validator"))
57
58 // name, default, and direction
59 .def(init<const std::string &, const boost::python::object &, const unsigned int>(
60 (arg("self"), arg("name"), arg("defaultValue"), arg("direction") = Direction::Input),
61 "Construct a PythonObjectProperty with a default value"))
62
63 // name, default, validator, and direction
64 .def(init<const std::string &, const boost::python::object &, IValidator_sptr, const unsigned int>(
65 (arg("self"), arg("name"), arg("defaultValue"), arg("validator") = std::make_shared<NullValidator>(),
66 arg("direction") = Direction::Input),
67 "Construct a PythonObjectProperty with a default value and validator"))
68
69 .def("__init__",
70 make_constructor(&createPythonObjectProperty, default_call_policies(),
71 (arg("name"), arg("value"), arg("validator") = IValidator_sptr(new NullValidator),
72 arg("direction") = Direction::Input)))
73
74 .add_property("value", make_function(&PythonObjectProperty::operator(),
75 return_value_policy<boost::python::return_by_value>()))
76 .def("setValue", static_cast<std::string (PythonObjectProperty::*)(boost::python::object const &)>(
78
79 // type handler for alg.setProperty calls
80 Registry::TypeRegistry::subscribe(typeid(BaseValueType), new Registry::PythonObjectTypeHandler);
81}
std::string name
Definition Run.cpp:60
double value
The value of the point.
Definition FitMW.cpp:51
NullValidator is a validator that doesn't.
The concrete, templated class for properties.
std::string setValue(PythonObject const &obj)
Set the property value.
Mantid::Kernel::PropertyWithValue< ValueType > BaseClass
void export_PythonObjectProperty()
std::shared_ptr< IValidator > IValidator_sptr
A shared_ptr to an IValidator.
Definition IValidator.h:26
Describes the direction (within an algorithm) of a Property.
Definition Property.h:50
@ Input
An input workspace.
Definition Property.h:53
A helper struct to export PropertyWithValue<> types to Python.
A specialisation of PropertyValueHandler to handle passing a python object directly to a PythonObject...