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.h does not define SIP_API_MAJOR_NR, but its sip ABI (>=13) provides
38// api_get_address, so select that path for Qt6 explicitly.
39#if (SIP_API_MAJOR_NR == 8 && SIP_API_MINOR_NR >= 1) || SIP_API_MAJOR_NR > 8 || QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
40 return static_cast<T *>(sipapi->api_get_address(wrapper));
41#elif SIP_API_MAJOR_NR == 8
42 return static_cast<T *>(wrapper->data);
43#else
44 return static_cast<T *>(wrapper->u.cppPtr);
45#endif
46}
47
53template <typename T> Object wrap(const T &obj, char const *nameOfType) {
54 const auto sipapi = Detail::sipAPI();
55 const auto type = sipapi->api_find_type(nameOfType);
56 if (!type) {
57 return Python::Object();
58 }
59 auto pyObj = sipapi->api_convert_from_type(obj, type, nullptr);
60 return NewRef(pyObj);
61}
62
63} // namespace Python
64} // namespace Common
65} // namespace Widgets
66
67} // 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:53
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...