Mantid
Loading...
Searching...
No Matches
PropertyWithValueExporter.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2011 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 +
7#pragma once
8
11
12#ifndef Q_MOC_RUN
13#include <boost/python/bases.hpp>
14#include <boost/python/class.hpp>
15#include <boost/python/return_by_value.hpp>
16#include <boost/python/return_value_policy.hpp>
17#endif
18
19namespace {
20
21// Call the dtype helper function
22template <typename HeldType> std::string dtype(Mantid::Kernel::PropertyWithValue<HeldType> &self) {
23 // Check for the special case of a string
24 if constexpr (std::is_same<HeldType, std::string>::value) {
25 std::stringstream ss;
26 std::string val = self.value();
27 ss << "S" << val.size();
28 std::string ret_val = ss.str();
29 return ret_val;
30 }
31
33}
34
35// getter and setter helper functions for `value` Python property export.
36template <typename T> static T const &get_value(Mantid::Kernel::PropertyWithValue<T> const &self) {
37 return self(); // forwards to operator()()
38}
39
40template <typename T> static void set_value(Mantid::Kernel::PropertyWithValue<T> &self, T const &v) {
41 self = v; // forwards to operator=
42}
43
44} // namespace
45
46namespace Mantid {
47namespace PythonInterface {
48
52template <typename HeldType, typename ValueReturnPolicy = boost::python::return_by_value>
54 static void define(const char *pythonClassName) {
55 using namespace boost::python;
56 using namespace Mantid::Kernel;
57
58 class_<PropertyWithValue<HeldType>, bases<Property>, boost::noncopyable>(
59 pythonClassName, init<std::string, HeldType, unsigned int>(
60 (arg("self"), arg("name"), arg("value"), arg("direction") = Direction::Input)))
61 .add_property("value", make_function(&get_value<HeldType>, return_value_policy<ValueReturnPolicy>()),
62 &set_value<HeldType>)
63 .def("dtype", &dtype<HeldType>, arg("self"));
64 }
65};
66} // namespace PythonInterface
67} // namespace Mantid
The concrete, templated class for properties.
std::string value() const override
Get the value of the property as a string.
std::string dtype(const Container< HeldType > &)
Helper class which provides the Collimation Length for SANS instruments.
@ Input
An input workspace.
Definition Property.h:53
A helper struct to export PropertyWithValue<> types to Python.