test_opaque_types.cpp revision 11986
12207SN/A/*
25254Sksewell@umich.edu    tests/test_opaque_types.cpp -- opaque types, passing void pointers
35254Sksewell@umich.edu
42207SN/A    Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
55254Sksewell@umich.edu
65254Sksewell@umich.edu    All rights reserved. Use of this source code is governed by a
75254Sksewell@umich.edu    BSD-style license that can be found in the LICENSE file.
85254Sksewell@umich.edu*/
95254Sksewell@umich.edu
105254Sksewell@umich.edu#include "pybind11_tests.h"
115254Sksewell@umich.edu#include <pybind11/stl.h>
125254Sksewell@umich.edu#include <vector>
135254Sksewell@umich.edu
145254Sksewell@umich.edutypedef std::vector<std::string> StringList;
152207SN/A
165254Sksewell@umich.educlass ClassWithSTLVecProperty {
175254Sksewell@umich.edupublic:
185254Sksewell@umich.edu    StringList stringList;
195254Sksewell@umich.edu};
205254Sksewell@umich.edu
215254Sksewell@umich.edu/* IMPORTANT: Disable internal pybind11 translation mechanisms for STL data structures */
225254Sksewell@umich.eduPYBIND11_MAKE_OPAQUE(StringList);
235254Sksewell@umich.edu
245254Sksewell@umich.edutest_initializer opaque_types([](py::module &m) {
255254Sksewell@umich.edu    py::class_<StringList>(m, "StringList")
265254Sksewell@umich.edu        .def(py::init<>())
272665Ssaidi@eecs.umich.edu        .def("pop_back", &StringList::pop_back)
285254Sksewell@umich.edu        /* There are multiple versions of push_back(), etc. Select the right ones. */
295254Sksewell@umich.edu        .def("push_back", (void (StringList::*)(const std::string &)) &StringList::push_back)
305254Sksewell@umich.edu        .def("back", (std::string &(StringList::*)()) &StringList::back)
312207SN/A        .def("__len__", [](const StringList &v) { return v.size(); })
322207SN/A        .def("__iter__", [](StringList &v) {
332474SN/A           return py::make_iterator(v.begin(), v.end());
342207SN/A        }, py::keep_alive<0, 1>());
358229Snate@binkert.org
362454SN/A    py::class_<ClassWithSTLVecProperty>(m, "ClassWithSTLVecProperty")
372454SN/A        .def(py::init<>())
382680Sktlim@umich.edu        .def_readwrite("stringList", &ClassWithSTLVecProperty::stringList);
398232Snate@binkert.org
406650Sksewell@umich.edu    m.def("print_opaque_list", [](const StringList &l) {
416650Sksewell@umich.edu        std::string ret = "Opaque list: [";
426650Sksewell@umich.edu        bool first = true;
432474SN/A        for (auto entry : l) {
442207SN/A            if (!first)
452447SN/A                ret += ", ";
462474SN/A            ret += entry;
472447SN/A            first = false;
485154Sgblack@eecs.umich.edu        }
495154Sgblack@eecs.umich.edu        return ret + "]";
505154Sgblack@eecs.umich.edu    });
512474SN/A
522686Sksewell@umich.edu    m.def("return_void_ptr", []() { return (void *) 0x1234; });
532686Sksewell@umich.edu    m.def("get_void_ptr_value", [](void *ptr) { return reinterpret_cast<std::intptr_t>(ptr); });
542935Sksewell@umich.edu    m.def("return_null_str", []() { return (char *) nullptr; });
552474SN/A    m.def("get_null_str_value", [](char *ptr) { return reinterpret_cast<std::intptr_t>(ptr); });
562474SN/A
572474SN/A    m.def("return_unique_ptr", []() -> std::unique_ptr<StringList> {
582474SN/A        StringList *result = new StringList();
592686Sksewell@umich.edu        result->push_back("some value");
602686Sksewell@umich.edu        return std::unique_ptr<StringList>(result);
6110318Sandreas.hansson@arm.com    });
622686Sksewell@umich.edu});
636811SMatt DeVuyst