Mantid
Loading...
Searching...
No Matches
Sip.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 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 <QtGlobal>
12#include <sip.h>
13#include <stdexcept>
14
15namespace MantidQt {
16namespace Widgets {
17namespace Common {
18namespace Python {
19
20namespace Detail {
21EXPORT_OPT_MANTIDQT_COMMON const sipAPIDef *sipAPI();
22} // namespace Detail
23
28template <typename T> T *extract(const Object &obj) {
29 const auto sipapi = Detail::sipAPI();
30 if (!PyObject_TypeCheck(obj.ptr(), sipapi->api_wrapper_type)) {
31 throw std::runtime_error("extract() - Object is not a sip-wrapped type.");
32 }
33 // transfer ownership from python to C++
34 sipapi->api_transfer_to(obj.ptr(), 0);
35 // reinterpret to sipWrapper
36 auto wrapper = reinterpret_cast<sipSimpleWrapper *>(obj.ptr());
37 // PyQt6's sip ABI (>=13) provides api_get_address.
38 return static_cast<T *>(sipapi->api_get_address(wrapper));
39}
40
46template <typename T> Object wrap(const T &obj, char const *nameOfType) {
47 const auto sipapi = Detail::sipAPI();
48 const auto type = sipapi->api_find_type(nameOfType);
49 if (!type) {
50 return Python::Object();
51 }
52 auto pyObj = sipapi->api_convert_from_type(obj, type, nullptr);
53 return NewRef(pyObj);
54}
55
56} // namespace Python
57} // namespace Common
58} // namespace Widgets
59
60} // namespace MantidQt
#define EXPORT_OPT_MANTIDQT_COMMON
Definition DllOption.h:15
double obj
the value of the quadratic function
EXPORT_OPT_MANTIDQT_COMMON const sipAPIDef * sipAPI()
Definition Sip.cpp:15
Python::Object NewRef(PyObject *obj)
Definition Object.h:43
Object wrap(const T &obj, char const *nameOfType)
Convert a C++ object of type T to a Python object.
Definition Sip.h:46
boost::python::object Object
Definition Object.h:29
T * extract(const Object &obj)
Extract a C++ object of type T from the Python object.
Definition Sip.h:28
The AlgorithmProgressDialogPresenter keeps track of the running algorithms and displays a progress ba...