test_open_spiel_pattern.cpp 905 Bytes
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "pybind11_tests.h"

#include <pybind11/functional.h>
#include <pybind11/smart_holder.h>

#include <functional>

namespace pybind11_tests {
namespace open_spiel_pattern {

class Foo {
public:
    int num;
    Foo(int i) : num{i} {}
    int bar(int i) { return num + i; }
};

18
19
} // namespace open_spiel_pattern
} // namespace pybind11_tests
20

21
22
23
24
25
26
27
28
29
30
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::open_spiel_pattern::Foo)

namespace pybind11_tests {
namespace open_spiel_pattern {

int RecycleFoo() {
    Foo foo_orig(123);
    py::object foo_py = py::cast(foo_orig);
    auto foo_from_py  = py::cast<std::unique_ptr<Foo>>(foo_py);
    return foo_from_py->bar(456);
31
32
33
34
35
36
37
38
}

} // namespace open_spiel_pattern
} // namespace pybind11_tests

TEST_SUBMODULE(open_spiel_pattern, m) {
    using namespace pybind11_tests::open_spiel_pattern;

39
    py::classh<Foo>(m, "Foo");
40

41
    m.def("recycle_foo", RecycleFoo);
42
}