14 #if !defined(PYBIND11_OVERLOAD_CAST) 15 template <
typename... Args>
79 int get()
const {
return value; }
80 void set(
int v) { value = v; }
99 const UserType &
get1()
const {
return v1; }
100 const UserType &
get2()
const {
return v2; }
102 void set1(
int v) { v1.set(v); }
103 void set2(
int v) { v2.set(v); }
109 class ArgInspector1 {
public: std::string arg =
"(default arg inspector 1)"; };
110 class ArgInspector2 {
public: std::string arg =
"(default arg inspector 2)"; };
112 namespace pybind11 {
namespace detail {
118 value.arg =
"loading ArgInspector1 argument " +
119 std::string(convert ?
"WITH" :
"WITHOUT") +
" conversion allowed. " 120 "Argument value = " + (std::string)
str(src);
125 return str(src.
arg).release();
133 value.arg =
"loading ArgInspector2 argument " +
134 std::string(convert ?
"WITH" :
"WITHOUT") +
" conversion allowed. " 135 "Argument value = " + (std::string)
str(src);
140 return str(src.
arg).release();
167 namespace pybind11 {
namespace detail {
182 int none3(std::shared_ptr<NoneTester> &obj) {
return obj ? obj->answer : -1; }
183 int none4(std::shared_ptr<NoneTester> *obj) {
return obj && *obj ? (*obj)->answer : -1; }
184 int none5(std::shared_ptr<NoneTester> obj) {
return obj ? obj->answer : -1; }
202 double ro_value = 1.25;
206 using UnregisteredBase::UnregisteredBase;
207 double sum()
const {
return rw_value + ro_value; }
213 emna.
def(py::init<>())
214 .def(py::init<int>())
215 .def(py::init<const ExampleMandA&>())
236 #if defined(PYBIND11_OVERLOAD_CAST) 266 .def_static(
"add_mixed_overloads1", []() {
267 auto emna = py::reinterpret_borrow<py::class_<ExampleMandA>>(py::module::import(
"pybind11_tests.methods_and_attributes").attr(
"ExampleMandA"));
271 .def_static(
"add_mixed_overloads2", []() {
272 auto emna = py::reinterpret_borrow<py::class_<ExampleMandA>>(py::module::import(
"pybind11_tests.methods_and_attributes").attr(
"ExampleMandA"));
281 emna.
attr(
"add2b") = emna.
attr(
"add2");
293 .
def_property(
"def_property_impossible",
nullptr,
nullptr)
298 .def_property_readonly_static(
"def_property_readonly_static",
300 .def_property_static(
"def_property_writeonly_static",
nullptr,
302 .def_property_static(
"def_property_static",
305 .def_property_static(
"static_cls",
318 auto rvp_copy = py::return_value_policy::copy;
340 struct MetaclassOverride { };
342 .def_property_readonly_static(
"readonly", [](
py::object) {
return 1; });
344 #if !defined(PYPY_VERSION) 354 class CppDerivedDynamicClass :
public DynamicClass { };
381 m.
def(
"floats_preferred", [](
double f) {
return 0.5 * f; },
py::arg(
"f"));
383 m.
def(
"ints_preferred", [](
int i) {
return i / 2; },
py::arg(
"i"));
389 m.
attr(
"debug_enabled") =
true;
391 m.
attr(
"debug_enabled") =
false;
393 m.
def(
"bad_arg_def_named", []{
394 auto m = py::module::import(
"pybind11_tests");
397 m.
def(
"bad_arg_def_unnamed", []{
398 auto m = py::module::import(
"pybind11_tests");
419 .def(py::init<int>())
421 .def(
"__str__", [](
const StrIssue &si) {
453 m.
def(
"custom_caster_no_destroy", []() {
static auto *dt =
new DestructionTester();
return dt; });
456 py::return_value_policy::take_ownership);
458 py::return_value_policy::take_ownership);
class_ & def_readwrite_static(const char *name, D *pm, const Extra &...extra)
class_ & def_property_static(const char *name, const Getter &fget, const cpp_function &fset, const Extra &...extra)
Uses return_value_policy::reference by default.
py::str overloaded(int, float) const
py::str overloaded(int, float)
const UserType & get1() const
DestructionTester(DestructionTester &&)
Wraps an arbitrary C++ function/method/lambda function/.. into a callable Python object.
py::str overloaded(float, int) const
void add10(const int *other)
static handle cast(const DestructionTester &, return_value_policy, handle)
Dummy type which is not exported anywhere – something to trigger a conversion error.
glibc defines I as a macro which breaks things, e.g., boost template names
void print_destroyed(T *inst, Values &&...values)
void print_copy_assigned(T *inst, Values &&...values)
class_ & def_property_readonly(const char *name, const Getter &fget, const Extra &...extra)
Uses return_value_policy::reference_internal by default.
const ExampleMandA * self5()
void print_copy_created(T *inst, Values &&...values)
void operator=(ExampleMandA &&e)
static constexpr auto const_
constexpr descr< N - 1 > _(char const(&text)[N])
Annotation which enables dynamic attributes, i.e. adds __dict__ to a class.
const ExampleMandA & self3()
class_ & def_readonly_static(const char *name, const D *pm, const Extra &...extra)
obj_attr_accessor attr(handle key) const
py::str overloaded(float, float) const
void print_default_created(T *inst, Values &&...values)
detail::initimpl::constructor< Args... > init()
Binds an existing constructor taking arguments Args...
double get_double() const
void operator=(const ExampleMandA &e)
static handle cast(const ArgInspector2 &src, return_value_policy, handle)
void print_move_assigned(T *inst, Values &&...values)
int none5(std::shared_ptr< NoneTester > obj)
static handle cast(const ArgAlwaysConverts &, return_value_policy, handle)
DestructionTester & operator=(const DestructionTester &)
return_value_policy
Approach used to cast a previously unknown C++ instance into a Python object.
ExampleMandA(const ExampleMandA &e)
DestructionTester(const DestructionTester &)
test_initializer methods_and_attributes("methods_and_attributes", test_submodule_methods_and_attributes)
void add2(ExampleMandA &other)
py::str overloaded(int, int) const
ExampleMandA(ExampleMandA &&e)
bool load(handle src, bool convert)
int none4(std::shared_ptr< NoneTester > *obj)
py::str overloaded(int, int)
bool load(handle src, bool convert)
int none3(std::shared_ptr< NoneTester > &obj)
class_ & def_static(const char *name_, Func &&f, const Extra &... extra)
void add3(const ExampleMandA &other)
static handle cast(const ArgInspector1 &src, return_value_policy, handle)
py::str overloaded(int) const
static void static_set(int v)
void print_created(T *inst, Values &&...values)
const UserType & get2() const
class_ & def_property_readonly_static(const char *name, const Getter &fget, const Extra &...extra)
Uses return_value_policy::reference by default.
py::str overloaded(float, int)
void add5(const ExampleMandA *other)
DestructionTester & operator=(DestructionTester &&)
UserType get_rvalue() const
py::str overloaded(float, float)
arg & noconvert(bool flag=true)
Indicate that the type should not be converted in the type caster.
int none2(NoneTester *obj)
void add1(ExampleMandA other)
bool load(handle, bool convert)
int none1(const NoneTester &obj)
arg_v & noconvert(bool flag=true)
Same as arg::noconvert(), but returns *this as arg_v&, not arg&.
void add4(ExampleMandA *other)
class_ & def_readonly(const char *name, const D C::*pm, const Extra &...extra)
void add8(const int &other)
class_ & def_property(const char *name, const Getter &fget, const Setter &fset, const Extra &...extra)
Uses return_value_policy::reference_internal by default.
void print_move_created(T *inst, Values &&...values)
#define TEST_SUBMODULE(name, variable)
bool typename Extra class_ & def(const char *name_, Func &&f, const Extra &... extra)
#define PYBIND11_TYPE_CASTER(type, py_name)
static py::str overloaded(float)
auto to_string(T &&value) -> decltype(std::forward< T >(value))
Convert an object to a string (directly forward if this can become a string)
class_ & def_readwrite(const char *name, D C::*pm, const Extra &... extra)