test_modules.cpp revision 11986
16502Snate@binkert.org/*
26502Snate@binkert.org    tests/test_modules.cpp -- nested modules, importing modules, and
36502Snate@binkert.org                            internal references
46502Snate@binkert.org
56502Snate@binkert.org    Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
66502Snate@binkert.org
76502Snate@binkert.org    All rights reserved. Use of this source code is governed by a
86502Snate@binkert.org    BSD-style license that can be found in the LICENSE file.
96502Snate@binkert.org*/
106502Snate@binkert.org
116502Snate@binkert.org#include "pybind11_tests.h"
126502Snate@binkert.org#include "constructor_stats.h"
136502Snate@binkert.org
146502Snate@binkert.orgstd::string submodule_func() {
156502Snate@binkert.org    return "submodule_func()";
166502Snate@binkert.org}
176502Snate@binkert.org
186502Snate@binkert.orgclass A {
196502Snate@binkert.orgpublic:
206502Snate@binkert.org    A(int v) : v(v) { print_created(this, v); }
216502Snate@binkert.org    ~A() { print_destroyed(this); }
226502Snate@binkert.org    A(const A&) { print_copy_created(this); }
236502Snate@binkert.org    A& operator=(const A &copy) { print_copy_assigned(this); v = copy.v; return *this; }
246502Snate@binkert.org    std::string toString() { return "A[" + std::to_string(v) + "]"; }
256502Snate@binkert.orgprivate:
266502Snate@binkert.org    int v;
276651Snate@binkert.org};
286502Snate@binkert.org
296502Snate@binkert.orgclass B {
306502Snate@binkert.orgpublic:
316502Snate@binkert.org    B() { print_default_created(this); }
326502Snate@binkert.org    ~B() { print_destroyed(this); }
336502Snate@binkert.org    B(const B&) { print_copy_created(this); }
346502Snate@binkert.org    B& operator=(const B &copy) { print_copy_assigned(this); a1 = copy.a1; a2 = copy.a2; return *this; }
356502Snate@binkert.org    A &get_a1() { return a1; }
366502Snate@binkert.org    A &get_a2() { return a2; }
376502Snate@binkert.org
386502Snate@binkert.org    A a1{1};
396502Snate@binkert.org    A a2{2};
406502Snate@binkert.org};
416502Snate@binkert.org
426502Snate@binkert.orgtest_initializer modules([](py::module &m) {
436502Snate@binkert.org    py::module m_sub = m.def_submodule("submodule");
446502Snate@binkert.org    m_sub.def("submodule_func", &submodule_func);
456502Snate@binkert.org
466502Snate@binkert.org    py::class_<A>(m_sub, "A")
476502Snate@binkert.org        .def(py::init<int>())
486502Snate@binkert.org        .def("__repr__", &A::toString);
496502Snate@binkert.org
506502Snate@binkert.org    py::class_<B>(m_sub, "B")
516502Snate@binkert.org        .def(py::init<>())
526502Snate@binkert.org        .def("get_a1", &B::get_a1, "Return the internal A 1", py::return_value_policy::reference_internal)
536502Snate@binkert.org        .def("get_a2", &B::get_a2, "Return the internal A 2", py::return_value_policy::reference_internal)
546502Snate@binkert.org        .def_readwrite("a1", &B::a1)  // def_readonly uses an internal reference return policy by default
556502Snate@binkert.org        .def_readwrite("a2", &B::a2);
566502Snate@binkert.org
576502Snate@binkert.org    m.attr("OD") = py::module::import("collections").attr("OrderedDict");
586502Snate@binkert.org});
596502Snate@binkert.org