blob: ad67fe419965716c11fb2104a1b31c689879f064 [file] [log] [blame]
Wenzel Jakobeda978e2016-03-15 15:05:40 +01001/*
Wenzel Jakobde1bca82016-03-26 17:51:09 +01002 example/example14.cpp -- opaque types, passing void pointers
Wenzel Jakobeda978e2016-03-15 15:05:40 +01003
Wenzel Jakob8cb6cb32016-04-17 20:21:41 +02004 Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
Wenzel Jakobeda978e2016-03-15 15:05:40 +01005
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 "example.h"
11#include <pybind11/stl.h>
12#include <vector>
13
14typedef std::vector<std::string> StringList;
15
Wenzel Jakob08712282016-04-22 16:52:15 +020016class ClassWithSTLVecProperty {
17public:
18 StringList stringList;
19};
20
Wenzel Jakob06f56ee2016-04-28 16:25:24 +020021/* IMPORTANT: Disable internal pybind11 translation mechanisms for STL data structures */
Wenzel Jakobe8b9dd22016-04-30 19:35:24 +020022PYBIND11_MAKE_OPAQUE(StringList);
Wenzel Jakob06f56ee2016-04-28 16:25:24 +020023
Wenzel Jakobeda978e2016-03-15 15:05:40 +010024void init_ex14(py::module &m) {
Wenzel Jakob06f56ee2016-04-28 16:25:24 +020025 py::class_<StringList>(m, "StringList")
Wenzel Jakobeda978e2016-03-15 15:05:40 +010026 .def(py::init<>())
Wenzel Jakob06f56ee2016-04-28 16:25:24 +020027 .def("pop_back", &StringList::pop_back)
28 /* There are multiple versions of push_back(), etc. Select the right ones. */
29 .def("push_back", (void (StringList::*)(const std::string &)) &StringList::push_back)
30 .def("back", (std::string &(StringList::*)()) &StringList::back)
31 .def("__len__", [](const StringList &v) { return v.size(); })
32 .def("__iter__", [](StringList &v) {
33 return py::make_iterator(v.begin(), v.end());
34 }, py::keep_alive<0, 1>());
Wenzel Jakobeda978e2016-03-15 15:05:40 +010035
Wenzel Jakob08712282016-04-22 16:52:15 +020036 py::class_<ClassWithSTLVecProperty>(m, "ClassWithSTLVecProperty")
37 .def(py::init<>())
Wenzel Jakob06f56ee2016-04-28 16:25:24 +020038 .def_readwrite("stringList", &ClassWithSTLVecProperty::stringList);
Wenzel Jakob08712282016-04-22 16:52:15 +020039
Wenzel Jakob06f56ee2016-04-28 16:25:24 +020040 m.def("print_opaque_list", [](const StringList &l) {
Wenzel Jakob08712282016-04-22 16:52:15 +020041 std::cout << "Opaque list: [";
42 bool first = true;
43 for (auto entry : l) {
44 if (!first)
45 std::cout << ", ";
46 std::cout << entry;
47 first = false;
48 }
49 std::cout << "]" << std::endl;
Wenzel Jakobeda978e2016-03-15 15:05:40 +010050 });
Wenzel Jakobde1bca82016-03-26 17:51:09 +010051
Wenzel Jakob772c6d52016-04-30 19:56:10 +020052 m.def("return_void_ptr", []() { return (void *) 0x1234; });
53 m.def("print_void_ptr", [](void *ptr) { std::cout << "Got void ptr : 0x" << std::hex << (uint64_t) ptr << std::endl; });
Wenzel Jakob9883ec02016-03-26 23:37:51 +010054 m.def("return_null_str", []() { return (char *) nullptr; });
Wenzel Jakob772c6d52016-04-30 19:56:10 +020055 m.def("print_null_str", [](char *ptr) { std::cout << "Got null str : 0x" << std::hex << (uint64_t) ptr << std::endl; });
Wenzel Jakob9f0dfce2016-04-06 17:38:18 +020056
57 m.def("return_unique_ptr", []() -> std::unique_ptr<StringList> {
58 StringList *result = new StringList();
59 result->push_back("some value");
60 return std::unique_ptr<StringList>(result);
61 });
Wenzel Jakobeda978e2016-03-15 15:05:40 +010062}