Mantid
Loading...
Searching...
No Matches
RegisterWorkspacePtrToPython.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2014 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
13
14#include <boost/python/register_ptr_to_python.hpp>
15
17
18// Specialization for shared_ptr<Workspace> and derived types.
19template <typename T>
21 typename std::enable_if<std::is_base_of<API::Workspace, T>::value>::type>
22 : public PropertyValueHandler {
24 using HeldType = std::shared_ptr<T>;
25
27 using PointeeType = T;
29 using PropertyValueType = std::shared_ptr<T>;
30
37 void set(Kernel::IPropertyManager *alg, const std::string &name, const boost::python::object &value) const override {
38 if (value == boost::python::object())
39 alg->setProperty<HeldType>(name, std::shared_ptr<T>(nullptr));
40 else
41 alg->setProperty<HeldType>(name, std::dynamic_pointer_cast<T>(ExtractSharedPtr<API::Workspace>(value)()));
42 }
43
55 std::unique_ptr<Kernel::Property> create(const std::string &name, const boost::python::object &defaultValue,
56 const boost::python::object &validator,
57 const unsigned int direction) const override {
58 using boost::python::extract;
60 using Kernel::Property;
62 const PropertyValueType valueInC = extract<PropertyValueType>(defaultValue)();
63 std::unique_ptr<Property> valueProp;
64 if (isNone(validator)) {
65 valueProp = std::make_unique<PropertyWithValue<PropertyValueType>>(name, valueInC, direction);
66 } else {
67 const IValidator *propValidator = extract<IValidator *>(validator);
68 valueProp =
69 std::make_unique<PropertyWithValue<PropertyValueType>>(name, valueInC, propValidator->clone(), direction);
70 }
71 return valueProp;
72 }
73};
74
83template <typename IType> struct DLLExport RegisterWorkspacePtrToPython {
84 using IType_sptr = std::shared_ptr<IType>;
85 using IType_wptr = std::weak_ptr<IType>;
88 using namespace boost::python;
89 using namespace Registry;
90
91 register_ptr_to_python<IType_sptr>();
92 register_ptr_to_python<IType_wptr>();
93 // properties can only ever store pointers to these
94 TypeRegistry::subscribe<TypedPropertyValueHandler<IType_sptr>>();
95 }
96};
97} // namespace Mantid::PythonInterface::Registry
double value
The value of the point.
Definition: FitMW.cpp:51
#define DLLExport
Definitions of the DLLImport compiler directives for MSVC.
Definition: System.h:53
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.
The concrete, templated class for properties.
Base class for properties.
Definition: Property.h:94
bool isNone(PyObject *ptr)
Definition: IsNone.h:26
STL namespace.
This class provides a base-class objects that are able to take a python object and set it on an algor...
Encapsulates the registration required for an interface type T that sits on top of a Kernel::DataItem...
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
Create a PropertyWithValue from the given python object value.
void set(Kernel::IPropertyManager *alg, const std::string &name, const boost::python::object &value) const override
Set function to handle Python -> C++ calls and get the correct type.
This class provides a templated class object that is able to take a python object and perform operati...