40 const std::string
str1{
"default1"},
str2{
"default2"};
109 if (
value)
return std::to_string(*
value);
else return "(null)";
132 virtual ~
NCVirt() =
default;
136 virtual Movable get_movable(
int a,
int b) = 0;
138 std::string
print_nc(
int a,
int b) {
return get_noncopyable(a, b).get_value(); }
139 std::string
print_movable(
int a,
int b) {
return get_movable(a, b).get_value(); }
142 #if !defined(__INTEL_COMPILER) && !defined(__CUDACC__) && !defined(__PGIC__) 154 virtual std::string
dispatch()
const {
return {}; };
155 virtual ~Base() =
default;
157 Base(
const Base&) =
delete;
168 py::gil_scoped_acquire lock;
174 py::gil_scoped_acquire lock;
181 py::gil_scoped_release
release;
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;
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;
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")
void print(const Matrix &A, const string &s, ostream &stream)
std::string say_something(unsigned times) override
std::string dispatch() const override
std::unique_ptr< int > value
int unlucky_number() override
double lucky_number() override
virtual void pure_virtual()=0
std::string print_movable(int a, int b)
void initialize_inherited_virtuals(py::module &m)
int unlucky_number() override
void print_destroyed(T *inst, Values &&...values)
NonCopyable(int a, int b)
void print_copy_created(T *inst, Values &&...values)
Movable get_movable(int a, int b) override
double lucky_number() override
ExampleVirt(ExampleVirt &&e)
static void test_gil_from_thread()
virtual Movable get_movable(int a, int b)=0
NonCopyable(NonCopyable &&o)
std::string say_something(unsigned times) override
std::string get_value() const
std::string say_something(unsigned times) override
virtual std::string dispatch() const
virtual const std::string * get_string2()
int run(int value) override
int unlucky_number() override
virtual bool run_bool()=0
TEST_SUBMODULE(virtual_functions, m)
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Array< double, 1, 3 > e(1./3., 0.5, 2.)
double lucky_number() override
void pure_virtual() override
A_METHODS A_Repeat()=default
#define PYBIND11_OVERRIDE(ret_type, cname, fn,...)
virtual NonCopyable get_noncopyable(int a, int b)
int unlucky_number() override
std::string say_something(unsigned times) override
double lucky_number() override
void print_created(T *inst, Values &&...values)
ExampleVirt(const ExampleVirt &e)
std::string get_value() const
detail::initimpl::constructor< Args... > init()
Binds an existing constructor taking arguments Args...
Movable(const Movable &m)
std::string print_nc(int a, int b)
#define PYBIND11_OVERRIDE_PURE(ret_type, cname, fn,...)
int unlucky_number() override
const std::string * get_string2() override
virtual const std::string & get_string1()
const std::string & get_string1() override
std::string say_something(unsigned times) override
void print_move_created(T *inst, Values &&...values)
NonCopyable get_noncopyable(int a, int b) override
int unlucky_number() override
#define PYBIND11_TYPE(...)
virtual int run(int value)