Mantid
Loading...
Searching...
No Matches
Property.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 <array>
15
16#include <boost/python/class.hpp>
17#include <boost/python/copy_const_reference.hpp>
18#include <boost/python/def.hpp>
19#include <boost/python/dict.hpp>
20#include <boost/python/enum.hpp>
21#include <boost/python/implicit.hpp>
22#include <boost/python/list.hpp>
23#include <boost/python/make_function.hpp>
24#include <boost/python/overloads.hpp>
25#include <boost/python/register_ptr_to_python.hpp>
26#include <boost/python/return_value_policy.hpp>
27
32using namespace boost::python;
33
34namespace {
35
36//
37// gcc 7 with std=c++17 has an issue attaching the EMPTY_*
38// functions with add_static_property when attempting to cast
39// the function pointer to a "const volatile void *":
40//
41// arg_to_python.hpp:211:66: error: invalid conversion from 'double (*)()
42// noexcept'
43// to 'const volatile void*' [-fpermissive]
44//
45// The noexcept specification appears to prevent the cast in the
46// boost python layer. These functions provide a pass through without the
47// noexcept specifier.
48
49constexpr inline double emptyDouble() { return Mantid::EMPTY_DBL(); }
50
51constexpr inline int emptyInt() { return Mantid::EMPTY_INT(); }
52
53constexpr inline long emptyLong() { return Mantid::EMPTY_LONG(); }
54
60PyObject *unitAsUnicode(const Property &self) {
61 static constexpr std::array<const char *, 2> codecs{"utf-8", "windows-1252"};
62 const auto &unitsBytes = self.units();
63 for (const auto &encoding : codecs) {
64 auto unitsPy = PyUnicode_Decode(unitsBytes.c_str(), unitsBytes.size(), encoding, "strict");
65 if (unitsPy)
66 return unitsPy;
67 // encoding failed. Try next
68 PyErr_Clear();
69 }
70 // All attempts failed, create an appropriate error string
71 const std::string allCodecsStr = []() {
72 auto it = codecs.begin();
73 std::string result = *it++;
74 for (; it != codecs.end(); ++it) {
75 result.append(",");
76 result.append(*it);
77 }
78 return result;
79 }();
80 const std::string helpMessage = std::string("Can't decode units string. Tried codecs=")
81 .append(allCodecsStr)
82 .append("\nTo try other codecs use Property.unitsAsBytes to retrieve the "
83 "original bytes object and use .decode().");
84 PyErr_SetString(PyExc_RuntimeError, helpMessage.c_str());
85 throw error_already_set();
86}
87
93PyObject *unitsAsBytes(const Property &self) { return PyBytes_FromString(self.units().c_str()); }
94
95} // namespace
96
98GNU_DIAG_OFF("unused-local-typedef")
99// Ignore -Wconversion warnings coming from boost::python
100// Seen with GCC 7.1.1 and Boost 1.63.0
101GNU_DIAG_OFF("conversion")
102// cppcheck-suppress unknownMacro
103BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(valueAsPrettyStrOverloader, valueAsPrettyStr, 0, 2)
104GNU_DIAG_ON("conversion")
105GNU_DIAG_ON("unused-local-typedef")
106
108 register_ptr_to_python<Property *>();
109
110 // vector of properties
111 std_vector_exporter<Property *>::wrap("std_vector_property");
112
113 // Direction
114 enum_<Direction::Type>("Direction")
115 .value("Input", Direction::Input)
116 .value("Output", Direction::Output)
117 .value("InOut", Direction::InOut)
118 .value("None", Direction::None);
119
120 // Add properties as that's what old version had
121 class_<Property, boost::noncopyable>("Property", no_init)
122 .add_property("name", make_function(&Property::name, return_value_policy<copy_const_reference>()),
123 "The name of the property")
124
125 .add_property("isValid", make_function(&Property::isValid),
126 "An empty string if the property is valid, otherwise it "
127 "contains an error message.")
128
129 .add_property("isDefault", make_function(&Property::isDefault), "Is the property set at the default value")
130
131 .add_property("getDefault", make_function(&Property::getDefault), "Get the default value as a string")
132
133 .add_property("direction", &Property::direction, "Input, Output, InOut or Unknown. See the Direction class")
134
135 .add_property("documentation",
136 make_function(&Property::documentation, return_value_policy<copy_const_reference>()),
137 "The property's doc string")
138
139 .def("setDocumentation", &Property::setDocumentation, (arg("doc")), "Setting documentation for property")
140
141 .add_property("type", make_function(&Property::type), "Returns a string identifier for the type")
142
143 .add_property("units", &unitAsUnicode, &Property::setUnits, "The units attached to this property")
144
145 .add_property("unitsAsBytes", &unitsAsBytes,
146 "The units attached to this property as a encoded bytes object. It "
147 "is assumed the caller knows the correct endcoding used.")
148
149 .add_property("value", &Property::value, &Property::setValue,
150 "The value of the property. For a generic property, equivalent to valueAsStr.")
151
152 .add_property("valueAsStr", &Property::value, &Property::setValue,
153 "The value of the property as a string. "
154 "For some property types, e.g. Workspaces, it is useful to "
155 "be able to refer to the string value directly")
156
157 .def("valueAsPrettyStr", &Property::valueAsPrettyStr,
158 valueAsPrettyStrOverloader((arg("maxLength") = 0, arg("collapseLists") = true),
159 "The value of the property as a formatted string. "
160 "If maxLength is defined then the output may not contain the "
161 "full "
162 "contents of the property. The maxLength and collapseLists "
163 "arguments "
164 "do not work for all property types"))
165
166 .add_property("allowedValues", &Property::allowedValues, "A list of allowed values")
167
168 .add_property("getGroup", make_function(&Property::getGroup, return_value_policy<copy_const_reference>()),
169 "Return the 'group' of the property, that is, the header "
170 "in the algorithm's list of properties.")
171
172 .add_property("settings",
173 make_function(static_cast<IPropertySettings *(Property::*)()>(&Property::getSettings),
174 return_value_policy<return_by_value>()),
175 "Return the object managing this property's settings")
176
177 .add_static_property("EMPTY_DBL", emptyDouble)
178 .add_static_property("EMPTY_INT", emptyInt)
179 .add_static_property("EMPTY_LONG", emptyLong)
180
181 .def("setAutoTrim", &Property::setAutoTrim, (arg("setting")), "Setting automatic trimming of whitespaces.")
182 .def("getAutoTrim", &Property::autoTrim, "Gets the setting of automatic trimming of whitespaces.")
183 .def("setDisableReplaceWSButton", &Property::setDisableReplaceWSButton, (arg("disable")),
184 "Disable the creation of the Replace Workspace button.")
185
186 .add_property("isDynamicDefault", make_function(&Property::isDynamicDefault),
187 "A flag indicating that the property's value has been set programmatically:"
188 " for example, if the property's default value depends on an upstream property")
189 .def("setIsDynamicDefault", &Property::setIsDynamicDefault, (arg("flag")),
190 "Set or clear the flag indicating that the property's value has been set programmatically.");
191}
#define GET_POINTER_SPECIALIZATION(TYPE)
Definition GetPointer.h:17
void export_Property()
Definition Property.cpp:107
#define GNU_DIAG_ON(x)
#define GNU_DIAG_OFF(x)
This is a collection of macros for turning compiler warnings off in a controlled manner.
Interface for modifiers to Property's that specify if they should be enabled or visible in a GUI.
Base class for properties.
Definition Property.h:94
bool isDynamicDefault() const
Returns a flag indicating that the property's value has been set programmatically,...
Definition Property.cpp:380
virtual const std::string & units() const
Returns the units of the property, if any, as a string.
Definition Property.cpp:191
const IPropertySettings * getSettings() const
Definition Property.cpp:109
void setDocumentation(const std::string &documentation)
Sets the user level description of the property.
Definition Property.cpp:146
void setIsDynamicDefault(const bool &flag)
Set or clear the flag indicating whether or not the property's value has been set programmatically.
Definition Property.cpp:385
virtual std::string valueAsPrettyStr(const size_t maxLength=0, const bool collapseLists=true) const
Returns the value of the property as a pretty printed string.
Definition Property.cpp:135
void setDisableReplaceWSButton(const bool &disable)
Sets the property to disable the creation of the "Replace Workspace" button.
Definition Property.cpp:374
bool autoTrim() const
Returns if the property is set to automatically trim string unput values of whitespace.
Definition Property.cpp:355
unsigned int direction() const
returns the direction of the property
Definition Property.h:177
const std::string & documentation() const
Get the property's documentation string.
Definition Property.cpp:76
virtual std::string setValue(const std::string &)=0
Set the value of the property via a string.
virtual void setUnits(const std::string &unit)
Sets the units of the property, as a string.
Definition Property.cpp:198
virtual std::string isValid() const
Overridden function that checks whether the property, if not overriden returns "".
Definition Property.cpp:93
virtual bool isDefault() const =0
Overriden function that returns if property has the same value that it was initialised with,...
const std::string & name() const
Get the property's name.
Definition Property.cpp:61
void setAutoTrim(const bool &setting)
Sets if the property is set to automatically trim string unput values of whitespace.
Definition Property.cpp:362
const std::string & getGroup()
Definition Property.h:199
virtual std::vector< std::string > allowedValues() const
Returns the set of valid values for this property, if such a set exists.
Definition Property.cpp:152
const std::string type() const
Returns the type of the property as a string.
Definition Property.cpp:87
virtual std::string getDefault() const =0
Get the default value for the property which is the value the property was initialised with.
virtual std::string value() const =0
Returns the value of the property as a string.
constexpr int EMPTY_INT() noexcept
Returns what we consider an "empty" integer within a property.
Definition EmptyValues.h:24
constexpr long EMPTY_LONG() noexcept
Returns what we consider an "empty" long within a property.
Definition EmptyValues.h:30
constexpr double EMPTY_DBL() noexcept
Returns what we consider an "empty" double within a property.
Definition EmptyValues.h:42
Describes the direction (within an algorithm) of a Property.
Definition Property.h:50
@ InOut
Both an input & output workspace.
Definition Property.h:55
@ Input
An input workspace.
Definition Property.h:53
@ Output
An output workspace.
Definition Property.h:54
A struct to help export std::vector types.
static void wrap(std::string const &python_name)
a python wrapper