test_callbacks.cpp (11986:c12e4625ab56) test_callbacks.cpp (12037:d28054ac6ec9)
1/*
2 tests/test_callbacks.cpp -- callbacks
3
4 Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
5
6 All rights reserved. Use of this source code is governed by a
7 BSD-style license that can be found in the LICENSE file.
8*/

--- 60 unchanged lines hidden (view full) ---

69 Payload(Payload &&) {
70 print_move_created(this);
71 }
72};
73
74/// Something to trigger a conversion error
75struct Unregistered {};
76
1/*
2 tests/test_callbacks.cpp -- callbacks
3
4 Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
5
6 All rights reserved. Use of this source code is governed by a
7 BSD-style license that can be found in the LICENSE file.
8*/

--- 60 unchanged lines hidden (view full) ---

69 Payload(Payload &&) {
70 print_move_created(this);
71 }
72};
73
74/// Something to trigger a conversion error
75struct Unregistered {};
76
77class AbstractBase {
78public:
79 virtual unsigned int func() = 0;
80};
81
82void func_accepting_func_accepting_base(std::function<double(AbstractBase&)>) { }
83
84struct MovableObject {
85 bool valid = true;
86
87 MovableObject() = default;
88 MovableObject(const MovableObject &) = default;
89 MovableObject &operator=(const MovableObject &) = default;
90 MovableObject(MovableObject &&o) : valid(o.valid) { o.valid = false; }
91 MovableObject &operator=(MovableObject &&o) {
92 valid = o.valid;
93 o.valid = false;
94 return *this;
95 }
96};
97
77test_initializer callbacks([](py::module &m) {
78 m.def("test_callback1", &test_callback1);
79 m.def("test_callback2", &test_callback2);
80 m.def("test_callback3", &test_callback3);
81 m.def("test_callback4", &test_callback4);
82 m.def("test_callback5", &test_callback5);
83
84 // Test keyword args and generalized unpacking

--- 46 unchanged lines hidden (view full) ---

131 });
132
133 /* Test cleanup of lambda closure */
134 m.def("test_cleanup", []() -> std::function<void(void)> {
135 Payload p;
136
137 return [p]() {
138 /* p should be cleaned up when the returned function is garbage collected */
98test_initializer callbacks([](py::module &m) {
99 m.def("test_callback1", &test_callback1);
100 m.def("test_callback2", &test_callback2);
101 m.def("test_callback3", &test_callback3);
102 m.def("test_callback4", &test_callback4);
103 m.def("test_callback5", &test_callback5);
104
105 // Test keyword args and generalized unpacking

--- 46 unchanged lines hidden (view full) ---

152 });
153
154 /* Test cleanup of lambda closure */
155 m.def("test_cleanup", []() -> std::function<void(void)> {
156 Payload p;
157
158 return [p]() {
159 /* p should be cleaned up when the returned function is garbage collected */
160 (void) p;
139 };
140 });
141
142 /* Test if passing a function pointer from C++ -> Python -> C++ yields the original pointer */
143 m.def("dummy_function", &dummy_function);
144 m.def("dummy_function2", &dummy_function2);
145 m.def("roundtrip", &roundtrip, py::arg("f"), py::arg("expect_none")=false);
146 m.def("test_dummy_function", &test_dummy_function);
147 // Export the payload constructor statistics for testing purposes:
148 m.def("payload_cstats", &ConstructorStats::get<Payload>);
161 };
162 });
163
164 /* Test if passing a function pointer from C++ -> Python -> C++ yields the original pointer */
165 m.def("dummy_function", &dummy_function);
166 m.def("dummy_function2", &dummy_function2);
167 m.def("roundtrip", &roundtrip, py::arg("f"), py::arg("expect_none")=false);
168 m.def("test_dummy_function", &test_dummy_function);
169 // Export the payload constructor statistics for testing purposes:
170 m.def("payload_cstats", &ConstructorStats::get<Payload>);
171
172 m.def("func_accepting_func_accepting_base",
173 func_accepting_func_accepting_base);
174
175 py::class_<MovableObject>(m, "MovableObject");
176
177 m.def("callback_with_movable", [](std::function<void(MovableObject &)> f) {
178 auto x = MovableObject();
179 f(x); // lvalue reference shouldn't move out object
180 return x.valid; // must still return `true`
181 });
149});
182});