blob: 54f4dc7a5d2ba9789b82858e29362b369cefab47 [file] [log] [blame]
Wenzel Jakobeda978e2016-03-15 15:05:40 +01001/*
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02002 tests/test_opaque_types.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
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020010#include "pybind11_tests.h"
Wenzel Jakobeda978e2016-03-15 15:05:40 +010011#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
Jason Rhinelander52f4be82016-09-03 14:54:22 -040024test_initializer opaque_types([](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) {
Dean Moldovan665e8802016-08-12 22:28:31 +020041 std::string ret = "Opaque list: [";
Wenzel Jakob08712282016-04-22 16:52:15 +020042 bool first = true;
43 for (auto entry : l) {
44 if (!first)
Dean Moldovan665e8802016-08-12 22:28:31 +020045 ret += ", ";
46 ret += entry;
Wenzel Jakob08712282016-04-22 16:52:15 +020047 first = false;
48 }
Dean Moldovan665e8802016-08-12 22:28:31 +020049 return ret + "]";
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; });
Dean Moldovan665e8802016-08-12 22:28:31 +020053 m.def("get_void_ptr_value", [](void *ptr) { return reinterpret_cast<std::intptr_t>(ptr); });
Wenzel Jakob9883ec02016-03-26 23:37:51 +010054 m.def("return_null_str", []() { return (char *) nullptr; });
Dean Moldovan665e8802016-08-12 22:28:31 +020055 m.def("get_null_str_value", [](char *ptr) { return reinterpret_cast<std::intptr_t>(ptr); });
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 });
Jason Rhinelander52f4be82016-09-03 14:54:22 -040062});