ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
casters.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <QSharedPointer>
11 #include <QString>
12 #include <QVariant>
13 
14 #include <CVLog.h>
15 #include <ecvColorScale.h>
16 
17 // clang-format off
18 #undef slots
19 #include <pybind11/pybind11.h>
20 #include <pybind11/native_enum.h>
21 #include <Python.h>
22 // clang-format on
23 
24 PYBIND11_DECLARE_HOLDER_TYPE(T, QSharedPointer<T>);
25 PYBIND11_MAKE_OPAQUE(QSharedPointer<ccColorScale>);
26 
27 namespace pybind11
28 {
29 namespace detail
30 {
31 template <> struct type_caster<QString>
32 {
33  PYBIND11_TYPE_CASTER(QString, _("QString"));
34 
35  bool load(handle src, bool)
36  {
37  PyObject *source = src.ptr();
38 
39  const char *str = PyUnicode_AsUTF8(source);
40  if (!str)
41  {
42  return false;
43  }
44 
45  value = str;
46  return true;
47  }
48 
49  static handle cast(const QString &src, return_value_policy policy, handle parent)
50  {
51  const QByteArray byteArray = src.toUtf8();
52  // TODO find how to use this error parameter.
53  const char *errors = nullptr;
54  PyObject *obj = PyUnicode_DecodeUTF8(byteArray.constData(), byteArray.size(), errors);
55  return obj;
56  }
57 };
58 
59 template <> struct type_caster<QByteArray>
60 {
61  PYBIND11_TYPE_CASTER(QByteArray, _("QByteArray"));
62 
63  bool load(handle src, bool)
64  {
65  if (!src || !isinstance<pybind11::bytes>(src))
66  {
67  return false;
68  }
69 
70  char *buffer;
71  ssize_t length;
72  if (PyBytes_AsStringAndSize(src.ptr(), &buffer, &length) == -1)
73  {
74  std::runtime_error("Unable to extract bytes contents!");
75  }
76  value = QByteArray(buffer, length);
77  return true;
78  }
79 
80  static handle cast(const QByteArray &src, return_value_policy /* policy */, handle /* handle */)
81  {
82  return PyBytes_FromStringAndSize(src.constData(), src.size());
83  }
84 };
85 
86 template <> struct type_caster<QVariant>
87 {
88  PYBIND11_TYPE_CASTER(QVariant, _("QVariant"));
89 
90  bool load(handle src, bool)
91  {
92  if (!src)
93  {
94  return false;
95  }
96  if (src.is_none())
97  {
98  return true;
99  }
100 
101  if (isinstance<pybind11::int_>(src))
102  {
103  value = src.cast<int>();
104  }
105  else if (isinstance<pybind11::str>(src))
106  {
107  value = QString::fromStdString(src.cast<std::string>());
108  }
109  else if (isinstance<pybind11::float_>(src))
110  {
111  value = src.cast<double>();
112  }
113  else
114  {
115  return false;
116  }
117  return true;
118  }
119 
120  static handle cast(const QVariant &src, return_value_policy policy, handle handle)
121  {
122  pybind11::object h = none();
123  switch (src.type())
124  {
125  case QVariant::Invalid:
126  break;
127  case QVariant::Bool:
128  h = pybind11::cast(src.toBool(), policy, handle);
129  break;
130  case QVariant::Int:
131  h = pybind11::cast(src.toInt(), policy, handle);
132  break;
133  case QVariant::UInt:
134  h = pybind11::cast(src.toUInt(), policy, handle);
135  break;
136  case QVariant::LongLong:
137  h = pybind11::cast(src.toLongLong(), policy, handle);
138  break;
139  case QVariant::ULongLong:
140  h = pybind11::cast(src.toULongLong(), policy, handle);
141  break;
142  case QVariant::Double:
143  h = pybind11::cast(src.toDouble(), policy, handle);
144  break;
145  case QVariant::Char:
146  h = pybind11::cast(src.toChar(), policy, handle);
147  break;
148  case QVariant::String:
149  h = pybind11::cast(src.toString(), policy, handle);
150  break;
151  case QVariant::Map:
152  Q_FALLTHROUGH();
153  case QVariant::List:
154  Q_FALLTHROUGH();
156  Q_FALLTHROUGH();
157  case QVariant::ByteArray:
158  h = pybind11::cast(src.toByteArray(), policy, handle);
159  break;
160  case QVariant::BitArray:
161  Q_FALLTHROUGH();
162  case QVariant::Date:
163  Q_FALLTHROUGH();
164  case QVariant::Time:
165  Q_FALLTHROUGH();
166  case QVariant::DateTime:
167  Q_FALLTHROUGH();
168  case QVariant::Url:
169  Q_FALLTHROUGH();
170  case QVariant::Locale:
171  Q_FALLTHROUGH();
172  case QVariant::Rect:
173  Q_FALLTHROUGH();
174  case QVariant::RectF:
175  Q_FALLTHROUGH();
176  case QVariant::Size:
177  Q_FALLTHROUGH();
178  case QVariant::SizeF:
179  Q_FALLTHROUGH();
180  case QVariant::Line:
181  Q_FALLTHROUGH();
182  case QVariant::LineF:
183  Q_FALLTHROUGH();
184  case QVariant::Point:
185  Q_FALLTHROUGH();
186  case QVariant::PointF:
187  Q_FALLTHROUGH();
188 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
189  case QVariant::RegularExpression:
190  Q_FALLTHROUGH();
191 #else
192  case QVariant::RegExp:
193  Q_FALLTHROUGH();
194 #endif
195  case QVariant::Hash:
196  Q_FALLTHROUGH();
197  case QVariant::EasingCurve:
198  Q_FALLTHROUGH();
199  case QVariant::Uuid:
200  Q_FALLTHROUGH();
201  case QVariant::ModelIndex:
202  Q_FALLTHROUGH();
203  case QVariant::PersistentModelIndex:
204  Q_FALLTHROUGH();
205  case QVariant::LastCoreType:
206  Q_FALLTHROUGH();
207  case QVariant::Font:
208  Q_FALLTHROUGH();
209  case QVariant::Pixmap:
210  Q_FALLTHROUGH();
211  case QVariant::Brush:
212  Q_FALLTHROUGH();
213  case QVariant::Color:
214  Q_FALLTHROUGH();
215  case QVariant::Palette:
216  Q_FALLTHROUGH();
217  case QVariant::Image:
218  Q_FALLTHROUGH();
219  case QVariant::Polygon:
220  Q_FALLTHROUGH();
221  case QVariant::Region:
222  Q_FALLTHROUGH();
223  case QVariant::Bitmap:
224  Q_FALLTHROUGH();
225  case QVariant::Cursor:
226  Q_FALLTHROUGH();
227  case QVariant::KeySequence:
228  Q_FALLTHROUGH();
229  case QVariant::Pen:
230  Q_FALLTHROUGH();
231  case QVariant::TextLength:
232  Q_FALLTHROUGH();
233  case QVariant::TextFormat:
234 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
235  Q_FALLTHROUGH();
236  case QVariant::Matrix:
237 #endif
238  Q_FALLTHROUGH();
239  case QVariant::Transform:
240  Q_FALLTHROUGH();
241  case QVariant::Matrix4x4:
242  Q_FALLTHROUGH();
243  case QVariant::Vector2D:
244  Q_FALLTHROUGH();
245  case QVariant::Vector3D:
246  Q_FALLTHROUGH();
247  case QVariant::Vector4D:
248  Q_FALLTHROUGH();
249  case QVariant::Quaternion:
250  Q_FALLTHROUGH();
251  case QVariant::Icon:
252  Q_FALLTHROUGH();
253  case QVariant::LastGuiType:
254  Q_FALLTHROUGH();
255  case QVariant::SizePolicy:
256  Q_FALLTHROUGH();
257  case QVariant::UserType:
258  Q_FALLTHROUGH();
259 #if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
260  case QVariant::PolygonF:
261  Q_FALLTHROUGH();
262 #endif
263  case QVariant::LastType:
264  throw std::runtime_error("Cannot convert this QVariant to python object");
265  }
266  return h.release();
267  }
268 };
269 
270 } // namespace detail
271 } // namespace pybind11
double Time(void)
Definition: MyTime.h:38
PYBIND11_MAKE_OPAQUE(QSharedPointer< ccColorScale >)
PYBIND11_DECLARE_HOLDER_TYPE(T, QSharedPointer< T >)
__host__ __device__ float length(float2 v)
Definition: cutil_math.h:1162
void Transform(benchmark::State &state, const core::Device &device)
Definition: PointCloud.cpp:127
std::vector< std::string > StringList
Definition: Common.h:193
unsigned Bool
Definition: sqlite3.c:20710
struct DateTime DateTime
Definition: sqlite3.c:21730
struct Hash Hash
Definition: sqlite3.c:13845
PYBIND11_TYPE_CASTER(QByteArray, _("QByteArray"))
static handle cast(const QByteArray &src, return_value_policy, handle)
Definition: casters.h:80
bool load(handle src, bool)
Definition: casters.h:35
PYBIND11_TYPE_CASTER(QString, _("QString"))
static handle cast(const QString &src, return_value_policy policy, handle parent)
Definition: casters.h:49
static handle cast(const QVariant &src, return_value_policy policy, handle handle)
Definition: casters.h:120
PYBIND11_TYPE_CASTER(QVariant, _("QVariant"))