Mantid
Loading...
Searching...
No Matches
Object.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2017 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 <boost/python/borrowed.hpp>
12#include <boost/python/dict.hpp>
13#include <boost/python/list.hpp>
14#include <boost/python/object.hpp>
15
23namespace MantidQt {
24namespace Widgets {
25namespace Common {
26namespace Python {
27
28// Alias for boost python object/dict/list wrapper
29using Object = boost::python::object;
30using Dict = boost::python::dict;
31using List = boost::python::list;
32
33// Alias for handle wrapping a raw PyObject*
34template <typename T = PyObject> using Handle = boost::python::handle<T>;
35
36// Alias for type to convert a C++ object to a Python object
37template <typename T> using ToPythonValue = boost::python::to_python_value<T>;
38
39// Helper to forward to boost python
40inline ssize_t Len(const Python::Object &obj) { return boost::python::len(obj); }
41
42// Helper to create an Object from a new reference to a raw PyObject*
43inline Python::Object NewRef(PyObject *obj) {
44 if (!obj) {
46 }
48}
49
50// Helper to create an Object from a borrowed reference to a raw PyObject*
51inline Python::Object BorrowedRef(PyObject *obj) {
52 if (!obj) {
54 }
55 return Python::Object(Python::Handle<>(boost::python::borrowed(obj)));
56}
57
58// Alias for exception indicating Python error handler is set
59using ErrorAlreadySet = boost::python::error_already_set;
60
65public:
70 explicit InstanceHolder(Object obj) : m_instance(std::move(obj)) {}
71
79 InstanceHolder(Object obj, const char *attr) : m_instance(std::move(obj)) {
81 if (PyObject_HasAttrString(pyobj().ptr(), attr) == 0) {
82 throw std::invalid_argument(std::string(pyobj().ptr()->ob_type->tp_name) + " has no attribute " + attr);
83 }
84 }
85
90 m_instance = Python::Object(); // none
91 }
92
94 inline const Object &pyobj() const { return m_instance; }
95
96private:
98};
99
100} // namespace Python
101} // namespace Common
102} // namespace Widgets
103} // namespace MantidQt
double obj
the value of the quadratic function
Holds a Python instance of an object with a method to access it.
Definition: Object.h:64
InstanceHolder(Object obj, const char *attr)
Construct an InstanceHolder with an existing Python object.
Definition: Object.h:79
~InstanceHolder()
The destructor must hold the GIL to be able reduce the refcount of the object.
Definition: Object.h:88
const Object & pyobj() const
Return the held instance object.
Definition: Object.h:94
InstanceHolder(Object obj)
Construct an InstanceHolder with an existing Python object.
Definition: Object.h:70
Defines a structure for acquiring/releasing the Python GIL using the RAII pattern.
Exception type that captures the current Python error state as a generic C++ exception for any genera...
Definition: ErrorHandling.h:24
boost::python::error_already_set ErrorAlreadySet
Definition: Object.h:59
boost::python::dict Dict
Definition: Object.h:30
boost::python::list List
Definition: Object.h:31
Python::Object NewRef(PyObject *obj)
Definition: Object.h:43
boost::python::to_python_value< T > ToPythonValue
Definition: Object.h:37
Python::Object BorrowedRef(PyObject *obj)
Definition: Object.h:51
boost::python::object Object
Definition: Object.h:29
boost::python::handle< T > Handle
Definition: Object.h:34
ssize_t Len(const Python::Object &obj)
Definition: Object.h:40
The AlgorithmProgressDialogPresenter keeps track of the running algorithms and displays a progress ba...
STL namespace.