test_opaque_types.cpp revision 14299:2fbea9df56d2
112855Sgabeblack@google.com/*
212855Sgabeblack@google.com    tests/test_opaque_types.cpp -- opaque types, passing void pointers
312855Sgabeblack@google.com
412855Sgabeblack@google.com    Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
512855Sgabeblack@google.com
612855Sgabeblack@google.com    All rights reserved. Use of this source code is governed by a
712855Sgabeblack@google.com    BSD-style license that can be found in the LICENSE file.
812855Sgabeblack@google.com*/
912855Sgabeblack@google.com
1012855Sgabeblack@google.com#include "pybind11_tests.h"
1112855Sgabeblack@google.com#include <pybind11/stl.h>
1212855Sgabeblack@google.com#include <vector>
1312855Sgabeblack@google.com
1412855Sgabeblack@google.com// IMPORTANT: Disable internal pybind11 translation mechanisms for STL data structures
1512855Sgabeblack@google.com//
1612855Sgabeblack@google.com// This also deliberately doesn't use the below StringList type alias to test
1712855Sgabeblack@google.com// that MAKE_OPAQUE can handle a type containing a `,`.  (The `std::allocator`
1812855Sgabeblack@google.com// bit is just the default `std::vector` allocator).
1912855Sgabeblack@google.comPYBIND11_MAKE_OPAQUE(std::vector<std::string, std::allocator<std::string>>);
2012855Sgabeblack@google.com
2112855Sgabeblack@google.comusing StringList = std::vector<std::string, std::allocator<std::string>>;
2212855Sgabeblack@google.com
2312855Sgabeblack@google.comTEST_SUBMODULE(opaque_types, m) {
2412855Sgabeblack@google.com    // test_string_list
2512855Sgabeblack@google.com    py::class_<StringList>(m, "StringList")
2612855Sgabeblack@google.com        .def(py::init<>())
2712855Sgabeblack@google.com        .def("pop_back", &StringList::pop_back)
2812855Sgabeblack@google.com        /* There are multiple versions of push_back(), etc. Select the right ones. */
2912855Sgabeblack@google.com        .def("push_back", (void (StringList::*)(const std::string &)) &StringList::push_back)
3012855Sgabeblack@google.com        .def("back", (std::string &(StringList::*)()) &StringList::back)
3112855Sgabeblack@google.com        .def("__len__", [](const StringList &v) { return v.size(); })
3212855Sgabeblack@google.com        .def("__iter__", [](StringList &v) {
3312855Sgabeblack@google.com           return py::make_iterator(v.begin(), v.end());
3412855Sgabeblack@google.com        }, py::keep_alive<0, 1>());
3512855Sgabeblack@google.com
3612855Sgabeblack@google.com    class ClassWithSTLVecProperty {
3712855Sgabeblack@google.com    public:
3812855Sgabeblack@google.com        StringList stringList;
3912855Sgabeblack@google.com    };
4012855Sgabeblack@google.com    py::class_<ClassWithSTLVecProperty>(m, "ClassWithSTLVecProperty")
4112855Sgabeblack@google.com        .def(py::init<>())
4212855Sgabeblack@google.com        .def_readwrite("stringList", &ClassWithSTLVecProperty::stringList);
4312855Sgabeblack@google.com
4412855Sgabeblack@google.com    m.def("print_opaque_list", [](const StringList &l) {
4512855Sgabeblack@google.com        std::string ret = "Opaque list: [";
4612855Sgabeblack@google.com        bool first = true;
4712855Sgabeblack@google.com        for (auto entry : l) {
4812855Sgabeblack@google.com            if (!first)
4912855Sgabeblack@google.com                ret += ", ";
5012855Sgabeblack@google.com            ret += entry;
5112855Sgabeblack@google.com            first = false;
5212855Sgabeblack@google.com        }
5312855Sgabeblack@google.com        return ret + "]";
5412855Sgabeblack@google.com    });
5512855Sgabeblack@google.com
5612855Sgabeblack@google.com    // test_pointers
57    m.def("return_void_ptr", []() { return (void *) 0x1234; });
58    m.def("get_void_ptr_value", [](void *ptr) { return reinterpret_cast<std::intptr_t>(ptr); });
59    m.def("return_null_str", []() { return (char *) nullptr; });
60    m.def("get_null_str_value", [](char *ptr) { return reinterpret_cast<std::intptr_t>(ptr); });
61
62    m.def("return_unique_ptr", []() -> std::unique_ptr<StringList> {
63        StringList *result = new StringList();
64        result->push_back("some value");
65        return std::unique_ptr<StringList>(result);
66    });
67}
68