NVIDIA DeepStream SDK API Reference

9.2 Release
service-maker/sources/python/src/bindings/value.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 namespace pybind11 {
19 namespace detail {
20 
21 template<> struct type_caster<Object::Value> : type_caster_base<Object::Value> {
22 public:
23  type_caster() = default;
24  type_caster(type_caster&&) noexcept = default;
25  type_caster& operator=(type_caster&&) noexcept = default;
26 
31  PYBIND11_TYPE_CASTER(Object::Value, const_name("_deepstream.Value"));
32 
33  bool load(handle src, bool) {
34  /* Walk through all the supported types */
35  PyTypeObject *srctype = Py_TYPE(src.ptr());
36  if (strcmp(srctype->tp_name, "str") == 0) {
37  auto tmp = reinterpret_borrow<py::str>(src).cast<string>();
38  value = Object::Value(tmp);
39  } else if (strcmp(srctype->tp_name, "int") == 0) {
40  auto tmp = reinterpret_borrow<py::int_>(src).cast<int>();
41  value = Object::Value(tmp);
42  } else if (strcmp(srctype->tp_name, "bool") == 0) {
43  auto tmp = reinterpret_borrow<py::bool_>(src).cast<bool>();
44  value = Object::Value(tmp);
45  } else if (strcmp(srctype->tp_name, "float") == 0) {
46  auto tmp = reinterpret_borrow<py::float_>(src).cast<float>();
47  value = Object::Value(tmp);
48  } else {
49  printf("Type not supported for value: %s\n", srctype->tp_name);
50  return false;
51  }
52 
53  return true;
54  }
55 
56  // Conversion from C++ type to Python object
57  static py::handle cast(const Object::Value& src, py::return_value_policy /* policy */, py::handle /* parent */) {
58  if (src.isInteger()) {
59  auto value = (int)src;
60  return PyLong_FromLong((long)value);
61  } else if (src.isUnsignedInteger()) {
62  auto value = (unsigned int)src;
63  return PyLong_FromLong((long)value);
64  } else if (src.isString()) {
65  auto value = (string)src;
66  return PyUnicode_FromString(value.c_str());
67  } else if (src.isFloat()) {
68  auto value = (float)src;
69  return PyFloat_FromDouble((double)value);
70  } else if (src.isDouble()) {
71  auto value = (double)src;
72  return PyFloat_FromDouble(value);
73  } else if (src.isBoolean()) {
74  auto value = (bool)src;
75  PyObject* object = value ? Py_True:Py_False;
76  return object;
77  } else {
78  printf("Type from Object::Value is not supported\n");
79  }
80 
81  return py::none();;
82  }
83 };
84 
85 }
86 }
pybind11::detail::type_caster< Object::Value >::load
bool load(handle src, bool)
Definition: service-maker/sources/python/src/bindings/value.h:33
pybind11
Definition: service-maker/sources/python/src/bindings/value.h:18
pybind11::detail::type_caster< Object::Value >::cast
static py::handle cast(const Object::Value &src, py::return_value_policy, py::handle)
Definition: service-maker/sources/python/src/bindings/value.h:57