example14.cpp 2.09 KB
Newer Older
Wenzel Jakob's avatar
Wenzel Jakob committed
1
/*
2
    example/example14.cpp -- opaque types, passing void pointers
Wenzel Jakob's avatar
Wenzel Jakob committed
3

4
    Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
Wenzel Jakob's avatar
Wenzel Jakob committed
5
6
7
8
9
10
11
12
13
14
15

    All rights reserved. Use of this source code is governed by a
    BSD-style license that can be found in the LICENSE file.
*/

#include "example.h"
#include <pybind11/stl.h>
#include <vector>

typedef std::vector<std::string> StringList;

Wenzel Jakob's avatar
Wenzel Jakob committed
16
17
18
19
20
class ClassWithSTLVecProperty {
public:
    StringList stringList;
};

Wenzel Jakob's avatar
Wenzel Jakob committed
21
22
23
24
25
26
27
void init_ex14(py::module &m) {
    py::class_<py::opaque<StringList>>(m, "StringList")
        .def(py::init<>())
        .def("push_back", [](py::opaque<StringList> &l, const std::string &str) { l->push_back(str); })
        .def("pop_back", [](py::opaque<StringList> &l) { l->pop_back(); })
        .def("back", [](py::opaque<StringList> &l) { return l->back(); });

Wenzel Jakob's avatar
Wenzel Jakob committed
28
29
30
31
32
33
34
    py::class_<ClassWithSTLVecProperty>(m, "ClassWithSTLVecProperty")
        .def(py::init<>())
        /* Need to cast properties to opaque types to avoid pybind11-internal
           STL conversion code from becoming active */
        .def_readwrite("stringList", (py::opaque<StringList> ClassWithSTLVecProperty:: *)
                                     &ClassWithSTLVecProperty::stringList);

Wenzel Jakob's avatar
Wenzel Jakob committed
35
36
    m.def("print_opaque_list", [](py::opaque<StringList> &_l) {
        StringList &l = _l;
Wenzel Jakob's avatar
Wenzel Jakob committed
37
38
39
40
41
42
43
44
45
        std::cout << "Opaque list: [";
        bool first = true;
        for (auto entry : l) {
            if (!first)
                std::cout << ", ";
            std::cout << entry;
            first = false;
        }
        std::cout << "]" << std::endl;
Wenzel Jakob's avatar
Wenzel Jakob committed
46
    });
47
48
49

    m.def("return_void_ptr", []() { return (void *) 1234; });
    m.def("print_void_ptr", [](void *ptr) { std::cout << "Got void ptr : " << (uint64_t) ptr << std::endl; });
50
51
    m.def("return_null_str", []() { return (char *) nullptr; });
    m.def("print_null_str", [](char *ptr) { std::cout << "Got null str : " << (uint64_t) ptr << std::endl; });
52
53
54
55
56
57

    m.def("return_unique_ptr", []() -> std::unique_ptr<StringList> {
        StringList *result = new StringList();
        result->push_back("some value");
        return std::unique_ptr<StringList>(result);
    });
Wenzel Jakob's avatar
Wenzel Jakob committed
58
}