test_opaque_types.cpp
Go to the documentation of this file.
1 /*
2  tests/test_opaque_types.cpp -- opaque types, passing void pointers
3 
4  Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
5 
6  All rights reserved. Use of this source code is governed by a
7  BSD-style license that can be found in the LICENSE file.
8 */
9 
10 #include <pybind11/stl.h>
11 
12 #include "pybind11_tests.h"
13 
14 #include <vector>
15 
16 // IMPORTANT: Disable internal pybind11 translation mechanisms for STL data structures
17 //
18 // This also deliberately doesn't use the below StringList type alias to test
19 // that MAKE_OPAQUE can handle a type containing a `,`. (The `std::allocator`
20 // bit is just the default `std::vector` allocator).
21 PYBIND11_MAKE_OPAQUE(std::vector<std::string, std::allocator<std::string>>);
22 
23 using StringList = std::vector<std::string, std::allocator<std::string>>;
24 
25 TEST_SUBMODULE(opaque_types, m) {
26  // test_string_list
27  py::class_<StringList>(m, "StringList")
28  .def(py::init<>())
29  .def("pop_back", &StringList::pop_back)
30  /* There are multiple versions of push_back(), etc. Select the right ones. */
31  .def("push_back", (void(StringList::*)(const std::string &)) & StringList::push_back)
32  .def("back", (std::string & (StringList::*) ()) & StringList::back)
33  .def("__len__", [](const StringList &v) { return v.size(); })
34  .def(
35  "__iter__",
36  [](StringList &v) { return py::make_iterator(v.begin(), v.end()); },
37  py::keep_alive<0, 1>());
38 
39  class ClassWithSTLVecProperty {
40  public:
41  StringList stringList;
42  };
43  py::class_<ClassWithSTLVecProperty>(m, "ClassWithSTLVecProperty")
44  .def(py::init<>())
45  .def_readwrite("stringList", &ClassWithSTLVecProperty::stringList);
46 
47  m.def("print_opaque_list", [](const StringList &l) {
48  std::string ret = "Opaque list: [";
49  bool first = true;
50  for (const auto &entry : l) {
51  if (!first) {
52  ret += ", ";
53  }
54  ret += entry;
55  first = false;
56  }
57  return ret + "]";
58  });
59 
60  // test_pointers
61  m.def("return_void_ptr", []() { return (void *) 0x1234; });
62  m.def("get_void_ptr_value", [](void *ptr) { return reinterpret_cast<std::intptr_t>(ptr); });
63  m.def("return_null_str", []() { return (char *) nullptr; });
64  m.def("get_null_str_value", [](char *ptr) { return reinterpret_cast<std::intptr_t>(ptr); });
65 
66  m.def("return_unique_ptr", []() -> std::unique_ptr<StringList> {
67  auto *result = new StringList();
68  result->push_back("some value");
69  return std::unique_ptr<StringList>(result);
70  });
71 
72  // test unions
73  py::class_<IntFloat>(m, "IntFloat")
74  .def(py::init<>())
75  .def_readwrite("i", &IntFloat::i)
76  .def_readwrite("f", &IntFloat::f);
77 }
Matrix3f m
EIGEN_CONSTEXPR Index first(const T &x) EIGEN_NOEXCEPT
static const Line3 l(Rot3(), 1, 1)
Values result
Array< int, Dynamic, 1 > v
TEST_SUBMODULE(opaque_types, m)
DenseIndex ret
PYBIND11_MAKE_OPAQUE(std::vector< std::string, std::allocator< std::string >>)
_W64 signed int intptr_t
Definition: ms_stdint.h:123
iterator make_iterator(Iterator &&first, Sentinel &&last, Extra &&...extra)
Makes a python iterator from a first and past-the-end C++ InputIterator.
Definition: pybind11.h:2373
std::vector< std::string, std::allocator< std::string > > StringList


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:37:46