test_open_spiel_pattern.cpp 867 Bytes
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#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; }
};

using FooFactory = std::function<Foo()>;

int MakeAndUseFoo(FooFactory f) {
    auto foo = f();
    return foo.bar(456);
}

} // namespace open_spiel_pattern
} // namespace pybind11_tests

// To use py::smart_holder, uncomment the next line and change py::class_ below to py::classh.
// PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::open_spiel_pattern::Foo)

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

    py::class_<Foo>(m, "Foo").def(py::init<int>()).def("bar", &Foo::bar);

    m.def("make_and_use_foo", MakeAndUseFoo);
}