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
11#include "MantidPythonInterface/api/DllConfig.h"
15
16#include <boost/python/register_ptr_to_python.hpp>
17
19
20// Specialization for shared_ptr<Workspace> and derived types.
21template <typename T>
22struct PYTHON_API_DLL
23 TypedPropertyValueHandler<std::shared_ptr<T>,
24 typename std::enable_if<std::is_base_of<API::Workspace, T>::value>::type>
25 : public PropertyValueHandler {
27 using HeldType = std::shared_ptr<T>;
28
30 using PointeeType = T;
32 using PropertyValueType = std::shared_ptr<T>;
33
40 void set(Kernel::IPropertyManager *alg, const std::string &name, const boost::python::object &value) const override {
41 if (value == boost::python::object())
42 alg->setProperty<HeldType>(name, std::shared_ptr<T>(nullptr));
43 else
44 alg->setProperty<HeldType>(name, std::dynamic_pointer_cast<T>(ExtractSharedPtr<API::Workspace>(value)()));
45 }
46
47 GNU_DIAG_OFF("maybe-uninitialized")
59 std::unique_ptr<Kernel::Property> create(const std::string &name, const boost::python::object &defaultValue,
60 const boost::python::object &validator,
61 const unsigned int direction) const override {
62 using boost::python::extract;
64 using Kernel::Property;
66 const PropertyValueType valueInC = extract<PropertyValueType>(defaultValue)();
67 std::unique_ptr<Property> valueProp;
68 if (isNone(validator)) {
69 valueProp = std::make_unique<PropertyWithValue<PropertyValueType>>(name, valueInC, direction);
70 } else {
71 const IValidator *propValidator = extract<IValidator *>(validator);
72 valueProp =
73 std::make_unique<PropertyWithValue<PropertyValueType>>(name, valueInC, propValidator->clone(), direction);
74 }
75 return valueProp;
76 }
77 GNU_DIAG_ON("maybe-uninitialized")
78};
79
88template <typename IType> struct RegisterWorkspacePtrToPython {
89 using IType_sptr = std::shared_ptr<IType>;
90 using IType_wptr = std::weak_ptr<IType>;
93 using namespace boost::python;
94 using namespace Registry;
95
96 register_ptr_to_python<IType_sptr>();
97 register_ptr_to_python<IType_wptr>();
98 // properties can only ever store pointers to these
99 TypeRegistry::subscribe<TypedPropertyValueHandler<IType_sptr>>();
100 }
101};
102} // namespace Mantid::PythonInterface::Registry
std::string name
Definition Run.cpp:60
double value
The value of the point.
Definition FitMW.cpp:51
#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 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:37
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:95
bool isNone(const 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...
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...