|
Packit |
534379 |
/*
|
|
Packit |
534379 |
tests/test_opaque_types.cpp -- opaque types, passing void pointers
|
|
Packit |
534379 |
|
|
Packit |
534379 |
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
|
|
Packit |
534379 |
|
|
Packit |
534379 |
All rights reserved. Use of this source code is governed by a
|
|
Packit |
534379 |
BSD-style license that can be found in the LICENSE file.
|
|
Packit |
534379 |
*/
|
|
Packit |
534379 |
|
|
Packit |
534379 |
#include "pybind11_tests.h"
|
|
Packit |
534379 |
#include <pybind11/stl.h>
|
|
Packit |
534379 |
#include <vector>
|
|
Packit |
534379 |
|
|
Packit |
534379 |
// IMPORTANT: Disable internal pybind11 translation mechanisms for STL data structures
|
|
Packit |
534379 |
//
|
|
Packit |
534379 |
// This also deliberately doesn't use the below StringList type alias to test
|
|
Packit |
534379 |
// that MAKE_OPAQUE can handle a type containing a `,`. (The `std::allocator`
|
|
Packit |
534379 |
// bit is just the default `std::vector` allocator).
|
|
Packit |
534379 |
PYBIND11_MAKE_OPAQUE(std::vector<std::string, std::allocator<std::string>>);
|
|
Packit |
534379 |
|
|
Packit |
534379 |
using StringList = std::vector<std::string, std::allocator<std::string>>;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
TEST_SUBMODULE(opaque_types, m) {
|
|
Packit |
534379 |
// test_string_list
|
|
Packit |
534379 |
py::class_<StringList>(m, "StringList")
|
|
Packit |
534379 |
.def(py::init<>())
|
|
Packit |
534379 |
.def("pop_back", &StringList::pop_back)
|
|
Packit |
534379 |
/* There are multiple versions of push_back(), etc. Select the right ones. */
|
|
Packit |
534379 |
.def("push_back", (void (StringList::*)(const std::string &)) &StringList::push_back)
|
|
Packit |
534379 |
.def("back", (std::string &(StringList::*)()) &StringList::back)
|
|
Packit |
534379 |
.def("__len__", [](const StringList &v) { return v.size(); })
|
|
Packit |
534379 |
.def("__iter__", [](StringList &v) {
|
|
Packit |
534379 |
return py::make_iterator(v.begin(), v.end());
|
|
Packit |
534379 |
}, py::keep_alive<0, 1>());
|
|
Packit |
534379 |
|
|
Packit |
534379 |
class ClassWithSTLVecProperty {
|
|
Packit |
534379 |
public:
|
|
Packit |
534379 |
StringList stringList;
|
|
Packit |
534379 |
};
|
|
Packit |
534379 |
py::class_<ClassWithSTLVecProperty>(m, "ClassWithSTLVecProperty")
|
|
Packit |
534379 |
.def(py::init<>())
|
|
Packit |
534379 |
.def_readwrite("stringList", &ClassWithSTLVecProperty::stringList);
|
|
Packit |
534379 |
|
|
Packit |
534379 |
m.def("print_opaque_list", [](const StringList &l) {
|
|
Packit |
534379 |
std::string ret = "Opaque list: [";
|
|
Packit |
534379 |
bool first = true;
|
|
Packit |
534379 |
for (auto entry : l) {
|
|
Packit |
534379 |
if (!first)
|
|
Packit |
534379 |
ret += ", ";
|
|
Packit |
534379 |
ret += entry;
|
|
Packit |
534379 |
first = false;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
return ret + "]";
|
|
Packit |
534379 |
});
|
|
Packit |
534379 |
|
|
Packit |
534379 |
// test_pointers
|
|
Packit |
534379 |
m.def("return_void_ptr", []() { return (void *) 0x1234; });
|
|
Packit |
534379 |
m.def("get_void_ptr_value", [](void *ptr) { return reinterpret_cast<std::intptr_t>(ptr); });
|
|
Packit |
534379 |
m.def("return_null_str", []() { return (char *) nullptr; });
|
|
Packit |
534379 |
m.def("get_null_str_value", [](char *ptr) { return reinterpret_cast<std::intptr_t>(ptr); });
|
|
Packit |
534379 |
|
|
Packit |
534379 |
m.def("return_unique_ptr", []() -> std::unique_ptr<StringList> {
|
|
Packit |
534379 |
StringList *result = new StringList();
|
|
Packit |
534379 |
result->push_back("some value");
|
|
Packit |
534379 |
return std::unique_ptr<StringList>(result);
|
|
Packit |
534379 |
});
|
|
Packit |
534379 |
}
|