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
14
15#include <boost/python/register_ptr_to_python.hpp>
16
18
19// Specialization for shared_ptr<Workspace> and derived types.
20template <typename T>
22 typename std::enable_if<std::is_base_of<API::Workspace, T>::value>::type>
23 : public PropertyValueHandler {
25 using HeldType = std::shared_ptr<T>;
26
28 using PointeeType = T;
30 using PropertyValueType = std::shared_ptr<T>;
31
38 void set(Kernel::IPropertyManager *alg, const std::string &name, const boost::python::object &value) const override {
39 if (value == boost::python::object())
40 alg->setProperty<HeldType>(name, std::shared_ptr<T>(nullptr));
41 else
42 alg->setProperty<HeldType>(name, std::dynamic_pointer_cast<T>(ExtractSharedPtr<API::Workspace>(value)()));
43 }
44
45 GNU_DIAG_OFF("maybe-uninitialized")
57 std::unique_ptr<Kernel::Property> create(const std::string &name, const boost::python::object &defaultValue,
58 const boost::python::object &validator,
59 const unsigned int direction) const override {
60 using boost::python::extract;
62 using Kernel::Property;
64 const PropertyValueType valueInC = extract<PropertyValueType>(defaultValue)();
65 std::unique_ptr<Property> valueProp;
66 if (isNone(validator)) {
67 valueProp = std::make_unique<PropertyWithValue<PropertyValueType>>(name, valueInC, direction);
68 } else {
69 const IValidator *propValidator = extract<IValidator *>(validator);
70 valueProp =
71 std::make_unique<PropertyWithValue<PropertyValueType>>(name, valueInC, propValidator->clone(), direction);
72 }
73 return valueProp;
74 }
75 GNU_DIAG_ON("maybe-uninitialized")
76};
77
86template <typename IType> struct DLLExport RegisterWorkspacePtrToPython {
87 using IType_sptr = std::shared_ptr<IType>;
88 using IType_wptr = std::weak_ptr<IType>;
91 using namespace boost::python;
92 using namespace Registry;
93
94 register_ptr_to_python<IType_sptr>();
95 register_ptr_to_python<IType_wptr>();
96 // properties can only ever store pointers to these
97 TypeRegistry::subscribe<TypedPropertyValueHandler<IType_sptr>>();
98 }
99};
100} // namespace Mantid::PythonInterface::Registry
std::string name
Definition Run.cpp:60
double value
The value of the point.
Definition FitMW.cpp:51
#define DLLExport
Definitions of the DLLImport compiler directives for MSVC.
Definition System.h:37
#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: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(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...