Mantid
Loading...
Searching...
No Matches
OptionalBool.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 +
14#include <boost/python/class.hpp>
15#include <boost/python/enum.hpp>
16#include <boost/python/make_constructor.hpp>
17
18using namespace boost::python;
19using namespace Mantid::Kernel;
20using namespace Mantid::PythonInterface;
21
23 boost::python::enum_<OptionalBool::Value>("OptionalBoolValue")
27 // Python 3 does not allow .True/.False. It gives a syntax error
28 // so use an underscore after the name
29 .value((OptionalBool::StrTrue + "_").c_str(), OptionalBool::True)
30 .value((OptionalBool::StrFalse + "_").c_str(), OptionalBool::False);
31}
32
33namespace {
34OptionalBool *createOptionalBool(OptionalBool::Value value) {
35 auto state = OptionalBool::Value(value);
36 return new OptionalBool(state);
37}
38
39class OptionalBoolPropertyValueHandler : public Registry::PropertyValueHandler {
40
41private:
42 OptionalBool fromPyObj(const boost::python::object &value) const {
43 OptionalBool target;
44 const extract<OptionalBool> asDirect(value);
45 const extract<OptionalBool::Value> asEnum(value);
46 const extract<bool> asBool(value);
47
48 if (asDirect.check()) {
49 target = asDirect();
50 } else if (asEnum.check()) {
51 target = OptionalBool(asEnum());
52 } else if (asBool.check()) {
53 target = OptionalBool(asBool());
54 } else {
55 throw std::invalid_argument("Unknown conversion to OptionalBool");
56 }
57 return target;
58 }
59
60public:
61 using HeldType = OptionalBool;
62
66 void set(Mantid::Kernel::IPropertyManager *alg, const std::string &name,
67 const boost::python::object &value) const override {
68
69 alg->setProperty<OptionalBool>(name, fromPyObj(value));
70 }
71
75 std::unique_ptr<Mantid::Kernel::Property> create(const std::string &name, const boost::python::object &value,
76 const boost::python::object &validator,
77 const unsigned int direction) const override {
78 using boost::python::extract;
79
80 auto optBool = fromPyObj(value);
81 if (isNone(validator)) {
82 return std::make_unique<PropertyWithValue<OptionalBool>>(name, optBool, direction);
83 } else {
84 const IValidator *propValidator = extract<IValidator *>(validator);
85 return std::make_unique<PropertyWithValue<OptionalBool>>(name, optBool, propValidator->clone(), direction);
86 }
87 }
88};
89} // namespace
90
92 // V3D class
93 class_<OptionalBool>("OptionalBool")
94 .def("__init__", make_constructor(&createOptionalBool, default_call_policies(), (arg("value"))))
95 .def("getValue", &OptionalBool::getValue, arg("self"));
96}
97
100 // ints & vectors
101 PropertyWithValueExporter<OptionalBool>::define("OptionalBoolPropertyWithValue");
102
103 Registry::TypeRegistry::subscribe<OptionalBoolPropertyValueHandler>();
104}
double value
The value of the point.
Definition: FitMW.cpp:51
void export_PropertyWithValueOptionalBool()
void export_OptionalBoolValue()
void export_OptionalBool()
Interface to PropertyManager.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
IValidator is the basic interface for all validators for properties.
Definition: IValidator.h:43
virtual IValidator_sptr clone() const =0
Make a copy of the present type of validator.
OptionalBool : Tri-state bool.
Definition: OptionalBool.h:25
static const std::string StrUnset
Definition: OptionalBool.h:30
static const std::string StrFalse
Definition: OptionalBool.h:31
static const std::string StrTrue
Definition: OptionalBool.h:32
std::unique_ptr< T > create(const P &parent, const IndexArg &indexArg, const HistArg &histArg)
This is the create() method that all the other create() methods call.
bool isNone(PyObject *ptr)
Definition: IsNone.h:26
A helper struct to export PropertyWithValue<> types to Python.
static void define(const char *pythonClassName)
This class provides a base-class objects that are able to take a python object and set it on an algor...