pybind11_tests.cpp revision 11986
111986Sandreas.sandberg@arm.com/*
211986Sandreas.sandberg@arm.com    tests/pybind11_tests.cpp -- pybind example plugin
311986Sandreas.sandberg@arm.com
411986Sandreas.sandberg@arm.com    Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
511986Sandreas.sandberg@arm.com
611986Sandreas.sandberg@arm.com    All rights reserved. Use of this source code is governed by a
711986Sandreas.sandberg@arm.com    BSD-style license that can be found in the LICENSE file.
811986Sandreas.sandberg@arm.com*/
911986Sandreas.sandberg@arm.com
1011986Sandreas.sandberg@arm.com#include "pybind11_tests.h"
1111986Sandreas.sandberg@arm.com#include "constructor_stats.h"
1211986Sandreas.sandberg@arm.com
1311986Sandreas.sandberg@arm.comstd::list<std::function<void(py::module &)>> &initializers() {
1411986Sandreas.sandberg@arm.com    static std::list<std::function<void(py::module &)>> inits;
1511986Sandreas.sandberg@arm.com    return inits;
1611986Sandreas.sandberg@arm.com}
1711986Sandreas.sandberg@arm.com
1811986Sandreas.sandberg@arm.comtest_initializer::test_initializer(std::function<void(py::module &)> initializer) {
1911986Sandreas.sandberg@arm.com    initializers().push_back(std::move(initializer));
2011986Sandreas.sandberg@arm.com}
2111986Sandreas.sandberg@arm.com
2211986Sandreas.sandberg@arm.comvoid bind_ConstructorStats(py::module &m) {
2311986Sandreas.sandberg@arm.com    py::class_<ConstructorStats>(m, "ConstructorStats")
2411986Sandreas.sandberg@arm.com        .def("alive", &ConstructorStats::alive)
2511986Sandreas.sandberg@arm.com        .def("values", &ConstructorStats::values)
2611986Sandreas.sandberg@arm.com        .def_readwrite("default_constructions", &ConstructorStats::default_constructions)
2711986Sandreas.sandberg@arm.com        .def_readwrite("copy_assignments", &ConstructorStats::copy_assignments)
2811986Sandreas.sandberg@arm.com        .def_readwrite("move_assignments", &ConstructorStats::move_assignments)
2911986Sandreas.sandberg@arm.com        .def_readwrite("copy_constructions", &ConstructorStats::copy_constructions)
3011986Sandreas.sandberg@arm.com        .def_readwrite("move_constructions", &ConstructorStats::move_constructions)
3111986Sandreas.sandberg@arm.com        .def_static("get", (ConstructorStats &(*)(py::object)) &ConstructorStats::get, py::return_value_policy::reference_internal);
3211986Sandreas.sandberg@arm.com}
3311986Sandreas.sandberg@arm.com
3411986Sandreas.sandberg@arm.comPYBIND11_PLUGIN(pybind11_tests) {
3511986Sandreas.sandberg@arm.com    py::module m("pybind11_tests", "pybind example plugin");
3611986Sandreas.sandberg@arm.com
3711986Sandreas.sandberg@arm.com    bind_ConstructorStats(m);
3811986Sandreas.sandberg@arm.com
3911986Sandreas.sandberg@arm.com    for (const auto &initializer : initializers())
4011986Sandreas.sandberg@arm.com        initializer(m);
4111986Sandreas.sandberg@arm.com
4211986Sandreas.sandberg@arm.com    if (!py::hasattr(m, "have_eigen")) m.attr("have_eigen") = false;
4311986Sandreas.sandberg@arm.com
4411986Sandreas.sandberg@arm.com    return m.ptr();
4511986Sandreas.sandberg@arm.com}
46