40 const std::string str1{
"default1"}, str2{
"default2"};
109 if (value)
return std::to_string(*value);
else return "(null)";
114 std::unique_ptr<int> value;
124 std::string
get_value()
const {
return std::to_string(value); }
142 #if !defined(__INTEL_COMPILER) && !defined(__CUDACC__) && !defined(__PGIC__)
143 NonCopyable get_noncopyable(
int a,
int b)
override {
147 Movable get_movable(
int a,
int b)
override {
154 virtual std::string
dispatch()
const {
return {}; };
166 static void test_gil() {
168 py::gil_scoped_acquire lock;
174 py::gil_scoped_acquire lock;
180 static void test_gil_from_thread() {
181 py::gil_scoped_release release;
183 std::thread t(test_gil);
194 py::class_<ExampleVirt, PyExampleVirt>(
m,
"ExampleVirt")
195 .def(py::init<int>())
201 py::class_<NonCopyable>(
m,
"NonCopyable")
202 .def(py::init<int, int>());
204 py::class_<Movable>(
m,
"Movable")
205 .def(py::init<int, int>());
208 #if !defined(__INTEL_COMPILER) && !defined(__CUDACC__) && !defined(__PGIC__)
209 py::class_<NCVirt, NCVirtTrampoline>(
m,
"NCVirt")
221 m.def(
"cstats_debug", &ConstructorStats::get<ExampleVirt>);
229 A(
const A&) =
delete;
230 virtual ~
A() =
default;
236 PyA(
const PyA&) =
delete;
237 ~PyA()
override {
py::print(
"PyA.~PyA()"); }
247 py::class_<A, PyA>(
m,
"A")
251 m.def(
"call_f", [](
A *a) { a->f(); });
257 A2(
const A2&) =
delete;
258 virtual ~A2() =
default;
259 virtual void f() {
py::print(
"A2.f()"); }
264 PyA2(
const PyA2&) =
delete;
265 ~PyA2()
override {
py::print(
"PyA2.~PyA2()"); }
272 py::class_<A2, PyA2>(
m,
"A2")
273 .def(py::init_alias<>())
274 .def(
py::init([](
int) {
return new PyA2(); }))
277 m.def(
"call_f", [](A2 *a2) { a2->f(); });
281 py::class_<Base, DispatchIssue>(
m,
"DispatchIssue")
285 m.def(
"dispatch_issue_go", [](
const Base * b) {
return b->
dispatch(); });
291 struct A { std::string
value =
"hi"; };
294 explicit OverrideTest(
const std::string &v) : v{v} {}
295 OverrideTest() =
default;
296 OverrideTest(
const OverrideTest&) =
delete;
297 virtual std::string str_value() {
return v; }
298 virtual std::string &str_ref() {
return v; }
299 virtual A A_value() {
return a; }
300 virtual A &A_ref() {
return a; }
301 virtual ~OverrideTest() =
default;
304 class PyOverrideTest :
public OverrideTest {
306 using OverrideTest::OverrideTest;
307 std::string str_value()
override {
PYBIND11_OVERRIDE(std::string, OverrideTest, str_value); }
314 std::string str_ref_helper() {
PYBIND11_OVERRIDE(std::string, OverrideTest, str_ref); }
316 std::string &str_ref()
override {
return _tmp = str_ref_helper(); }
322 py::class_<OverrideTest::A>(
m,
"OverrideTest_A")
324 py::class_<OverrideTest, PyOverrideTest>(
m,
"OverrideTest")
325 .def(py::init<const std::string &>())
326 .def(
"str_value", &OverrideTest::str_value)
328 .def(
"A_value", &OverrideTest::A_value)
329 .def(
"A_ref", &OverrideTest::A_ref);
343 virtual int unlucky_number() = 0; \
344 virtual std::string say_something(unsigned times) { \
345 std::string s = ""; \
346 for (unsigned i = 0; i < times; ++i) \
350 std::string say_everything() { \
351 return say_something(1) + " " + std::to_string(unlucky_number()); \
361 int unlucky_number() override { return 13; } \
362 std::string say_something(unsigned times) override { \
363 return "B says hi " + std::to_string(times) + " times"; \
365 virtual double lucky_number() { return 7.0; }
371 int unlucky_number() override { return 4444; } \
372 double lucky_number() override { return 888; }
376 #define D_METHODS // Nothing overridden.
385 virtual ~
A_Tpl() =
default;
395 using A_Repeat::A_Repeat;
401 using B_Repeat::B_Repeat;
408 using C_Repeat::C_Repeat;
415 using D_Repeat::D_Repeat;
435 template <
class Base = A_Tpl>
442 template <
class Base = B_Tpl>
466 py::class_<A_Repeat, PyA_Repeat>(
m,
"A_Repeat")
468 .def(
"unlucky_number", &A_Repeat::unlucky_number)
469 .def(
"say_something", &A_Repeat::say_something)
470 .def(
"say_everything", &A_Repeat::say_everything);
471 py::class_<B_Repeat, A_Repeat, PyB_Repeat>(
m,
"B_Repeat")
473 .def(
"lucky_number", &B_Repeat::lucky_number);
474 py::class_<C_Repeat, B_Repeat, PyC_Repeat>(
m,
"C_Repeat")
476 py::class_<D_Repeat, C_Repeat, PyD_Repeat>(
m,
"D_Repeat")
481 py::class_<A_Tpl, PyA_Tpl<>>(
m,
"A_Tpl")
483 .
def(
"unlucky_number", &A_Tpl::unlucky_number)
484 .def(
"say_something", &A_Tpl::say_something)
485 .def(
"say_everything", &A_Tpl::say_everything);
486 py::class_<B_Tpl, A_Tpl, PyB_Tpl<>>(
m,
"B_Tpl")
488 .
def(
"lucky_number", &B_Tpl::lucky_number);
489 py::class_<C_Tpl, B_Tpl, PyB_Tpl<C_Tpl>>(
m,
"C_Tpl")
491 py::class_<D_Tpl, C_Tpl, PyB_Tpl<D_Tpl>>(
m,
"D_Tpl")
496 m.def(
"test_gil", &test_gil);
497 m.def(
"test_gil_from_thread", &test_gil_from_thread);