You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
1.5 KiB

  1. /*
  2. example/example14.cpp -- opaque types, passing void pointers
  3. Copyright (c) 2015 Wenzel Jakob <wenzel@inf.ethz.ch>
  4. All rights reserved. Use of this source code is governed by a
  5. BSD-style license that can be found in the LICENSE file.
  6. */
  7. #include "example.h"
  8. #include <pybind11/stl.h>
  9. #include <vector>
  10. typedef std::vector<std::string> StringList;
  11. void init_ex14(py::module &m) {
  12. py::class_<py::opaque<StringList>>(m, "StringList")
  13. .def(py::init<>())
  14. .def("push_back", [](py::opaque<StringList> &l, const std::string &str) { l->push_back(str); })
  15. .def("pop_back", [](py::opaque<StringList> &l) { l->pop_back(); })
  16. .def("back", [](py::opaque<StringList> &l) { return l->back(); });
  17. m.def("print_opaque_list", [](py::opaque<StringList> &_l) {
  18. StringList &l = _l;
  19. std::cout << "Opaque list: " << std::endl;
  20. for (auto entry : l)
  21. std::cout << " " << entry << std::endl;
  22. });
  23. m.def("return_void_ptr", []() { return (void *) 1234; });
  24. m.def("print_void_ptr", [](void *ptr) { std::cout << "Got void ptr : " << (uint64_t) ptr << std::endl; });
  25. m.def("return_null_str", []() { return (char *) nullptr; });
  26. m.def("print_null_str", [](char *ptr) { std::cout << "Got null str : " << (uint64_t) ptr << std::endl; });
  27. m.def("return_unique_ptr", []() -> std::unique_ptr<StringList> {
  28. StringList *result = new StringList();
  29. result->push_back("some value");
  30. return std::unique_ptr<StringList>(result);
  31. });
  32. }