15#include "MantidPythonInterface/kernel/DllConfig.h"
19#define PY_ARRAY_UNIQUE_SYMBOL KERNEL_ARRAY_API
20#define NO_IMPORT_ARRAY
21#include <numpy/arrayobject.h>
23#include <boost/python/extract.hpp>
27template <
typename HeldType>
struct StdVectorExtractor {
28 static std::vector<HeldType> extract(
const boost::python::object &
value) {
29 return boost::python::extract<std::vector<HeldType>>(
value.ptr());
32template <>
struct StdVectorExtractor<bool> {
33 static std::vector<bool> extract(
const boost::python::object & ) {
34 throw std::runtime_error(
"Unable to supported extracting std::vector<bool> from python object");
46template <
typename ContainerType>
48 const boost::python::object &
value)
const {
50 using DestElementType =
typename ContainerType::value_type;
53 const std::string tpName =
value.ptr()->ob_type->tp_name;
54 if (tpName.starts_with(
"std_vector")) {
61 }
else if (PySequence_Check(
value.ptr())) {
65 DestElementType scalar = boost::python::extract<DestElementType>(
value.ptr());
81template <
typename ContainerType>
82std::unique_ptr<Kernel::Property>
84 const boost::python::object &validator,
const unsigned int direction)
const {
85 using DestElementType =
typename ContainerType::value_type;
86 using boost::python::extract;
90 ContainerType valueInC;
92 const std::string tpName = defaultValue.ptr()->ob_type->tp_name;
93 if (tpName.starts_with(
"std_vector")) {
94 valueInC = StdVectorExtractor<DestElementType>::extract(defaultValue);
95 }
else if (PySequence_Check(defaultValue.ptr())) {
99 DestElementType scalar = boost::python::extract<DestElementType>(defaultValue.ptr());
100 valueInC = std::vector<DestElementType>(1, scalar);
103 std::unique_ptr<Kernel::Property> valueProp;
105 valueProp = std::make_unique<PropertyWithValue<ContainerType>>(
name, valueInC, direction);
107 const IValidator *propValidator = extract<IValidator *>(validator);
108 valueProp = std::make_unique<PropertyWithValue<ContainerType>>(
name, valueInC, propValidator->
clone(), direction);
117#define INSTANTIATE(ElementType) template struct PYTHON_KERNEL_DLL SequenceTypeHandler<std::vector<ElementType>>;
double value
The value of the point.
#define INSTANTIATE(TYPE)
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.
virtual IValidator_sptr clone() const =0
Make a copy of the present type of validator.
The concrete, templated class for properties.
Thin object wrapper around a numpy array.
static bool check(const boost::python::object &obj)
Check if a python object points to an array type object.
bool isNone(const PyObject *ptr)
Converter taking an input numpy array and converting it to a std::vector.
Converts a Python sequence type to a C++ std::vector, where the element type is defined by the templa...
std::unique_ptr< Kernel::Property > create(const std::string &name, const boost::python::object &defaultValue, const boost::python::object &validator, const unsigned int direction) const override
Call to create a name property where the value is some container type.
void set(Kernel::IPropertyManager *alg, const std::string &name, const boost::python::object &value) const override
Call to set a named property where the value is some container type.