pybind11_tests.cpp 2.34 KB
Newer Older
Wenzel Jakob's avatar
Wenzel Jakob committed
1
/*
Dean Moldovan's avatar
Dean Moldovan committed
2
    tests/pybind11_tests.cpp -- pybind example plugin
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

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

Dean Moldovan's avatar
Dean Moldovan committed
10
11
#include "pybind11_tests.h"
#include "constructor_stats.h"
Wenzel Jakob's avatar
Wenzel Jakob committed
12

13
14
15
16
17
18
19
20
21
22
23
24
25
/*
For testing purposes, we define a static global variable here in a function that each individual
test .cpp calls with its initialization lambda.  It's convenient here because we can just not
compile some test files to disable/ignore some of the test code.

It is NOT recommended as a way to use pybind11 in practice, however: the initialization order will
be essentially random, which is okay for our test scripts (there are no dependencies between the
individual pybind11 test .cpp files), but most likely not what you want when using pybind11
productively.

Instead, see the "How can I reduce the build time?" question in the "Frequently asked questions"
section of the documentation for good practice on splitting binding code over multiple files.
*/
26
27
28
29
std::list<std::function<void(py::module &)>> &initializers() {
    static std::list<std::function<void(py::module &)>> inits;
    return inits;
}
Wenzel Jakob's avatar
Wenzel Jakob committed
30

31
32
33
test_initializer::test_initializer(std::function<void(py::module &)> initializer) {
    initializers().push_back(std::move(initializer));
}
34

35
36
37
38
39
40
41
42
43
void bind_ConstructorStats(py::module &m) {
    py::class_<ConstructorStats>(m, "ConstructorStats")
        .def("alive", &ConstructorStats::alive)
        .def("values", &ConstructorStats::values)
        .def_readwrite("default_constructions", &ConstructorStats::default_constructions)
        .def_readwrite("copy_assignments", &ConstructorStats::copy_assignments)
        .def_readwrite("move_assignments", &ConstructorStats::move_assignments)
        .def_readwrite("copy_constructions", &ConstructorStats::copy_constructions)
        .def_readwrite("move_constructions", &ConstructorStats::move_constructions)
44
        .def_static("get", (ConstructorStats &(*)(py::object)) &ConstructorStats::get, py::return_value_policy::reference_internal);
45
46
}

Dean Moldovan's avatar
Dean Moldovan committed
47
PYBIND11_PLUGIN(pybind11_tests) {
Jason Rhinelander's avatar
Jason Rhinelander committed
48
    py::module m("pybind11_tests", "pybind testing plugin");
Wenzel Jakob's avatar
Wenzel Jakob committed
49

50
51
    bind_ConstructorStats(m);

52
53
    for (const auto &initializer : initializers())
        initializer(m);
Wenzel Jakob's avatar
Wenzel Jakob committed
54

55
    if (!py::hasattr(m, "have_eigen")) m.attr("have_eigen") = false;
56

Wenzel Jakob's avatar
Wenzel Jakob committed
57
58
    return m.ptr();
}