16 # pragma warning(disable: 4324) // warning C4324: structure was padded due to alignment specifier 30 struct NoConstructor {
31 NoConstructor() =
default;
32 NoConstructor(
const NoConstructor &) =
default;
33 NoConstructor(NoConstructor &&) =
default;
34 static NoConstructor *new_instance() {
35 auto *
ptr =
new NoConstructor();
42 py::class_<NoConstructor>(
m,
"NoConstructor")
43 .def_static(
"new_instance", &NoConstructor::new_instance,
"Return an instance");
48 Pet(
const std::string &
name,
const std::string &species)
49 : m_name(name), m_species(species) {}
50 std::string
name()
const {
return m_name; }
51 std::string species()
const {
return m_species; }
54 std::string m_species;
57 class Dog :
public Pet {
59 Dog(
const std::string &
name) : Pet(name,
"dog") {}
60 std::string bark()
const {
return "Woof!"; }
63 class Rabbit :
public Pet {
65 Rabbit(
const std::string &
name) : Pet(name,
"parrot") {}
68 class Hamster :
public Pet {
70 Hamster(
const std::string &
name) : Pet(name,
"rodent") {}
73 class Chimera :
public Pet {
74 Chimera() : Pet(
"Kimmy",
"chimera") {}
77 py::class_<Pet> pet_class(m,
"Pet");
79 .def(py::init<std::string, std::string>())
81 .def(
"species", &Pet::species);
84 py::class_<Dog>(
m,
"Dog", pet_class)
85 .def(py::init<std::string>());
88 py::class_<Rabbit, Pet>(
m,
"Rabbit")
89 .def(py::init<std::string>());
92 py::class_<Hamster, Pet>(
m,
"Hamster")
93 .def(py::init<std::string>());
96 py::class_<Chimera, Pet>(
m,
"Chimera");
98 m.def(
"pet_name_species", [](
const Pet &pet) {
return pet.name() +
" is a " + pet.species(); });
99 m.def(
"dog_bark", [](
const Dog &dog) {
return dog.
bark(); });
103 BaseClass() =
default;
104 BaseClass(
const BaseClass &) =
default;
105 BaseClass(BaseClass &&) =
default;
106 virtual ~BaseClass() =
default;
108 struct DerivedClass1 : BaseClass { };
109 struct DerivedClass2 : BaseClass { };
111 py::class_<BaseClass>(
m,
"BaseClass").def(py::init<>());
112 py::class_<DerivedClass1>(
m,
"DerivedClass1").def(py::init<>());
113 py::class_<DerivedClass2>(
m,
"DerivedClass2").def(py::init<>());
115 m.def(
"return_class_1", []() -> BaseClass* {
return new DerivedClass1(); });
116 m.def(
"return_class_2", []() -> BaseClass* {
return new DerivedClass2(); });
117 m.def(
"return_class_n", [](
int n) -> BaseClass* {
118 if (n == 1)
return new DerivedClass1();
119 if (n == 2)
return new DerivedClass2();
120 return new BaseClass();
122 m.def(
"return_none", []() -> BaseClass* {
return nullptr; });
125 m.def(
"check_instances", [](py::list
l) {
127 py::isinstance<py::tuple>(l[0]),
128 py::isinstance<py::dict>(l[1]),
129 py::isinstance<Pet>(l[2]),
130 py::isinstance<Pet>(l[3]),
131 py::isinstance<Dog>(l[4]),
132 py::isinstance<Rabbit>(l[5]),
133 py::isinstance<UnregisteredType>(l[6])
140 m.def(
"check_type", [](
int category) {
146 return py::type::of<DerivedClass1>();
148 return py::type::of<Invalid>();
151 m.def(
"get_type_of", [](py::object ob) {
152 return py::type::of(ob);
155 m.def(
"as_type", [](py::object ob) {
157 if (py::isinstance<py::type>(ob))
160 throw std::runtime_error(
"Invalid type");
164 struct MismatchBase1 { };
165 struct MismatchDerived1 : MismatchBase1 { };
167 struct MismatchBase2 { };
168 struct MismatchDerived2 : MismatchBase2 { };
170 m.def(
"mismatched_holder_1", []() {
171 auto mod = py::module::import(
"__main__");
172 py::class_<MismatchBase1, std::shared_ptr<MismatchBase1>>(
mod,
"MismatchBase1");
173 py::class_<MismatchDerived1, MismatchBase1>(
mod,
"MismatchDerived1");
175 m.def(
"mismatched_holder_2", []() {
176 auto mod = py::module::import(
"__main__");
177 py::class_<MismatchBase2>(
mod,
"MismatchBase2");
178 py::class_<MismatchDerived2, std::shared_ptr<MismatchDerived2>,
179 MismatchBase2>(
mod,
"MismatchDerived2");
185 static std::unique_ptr<MyBase>
make() {
186 return std::unique_ptr<MyBase>(
new MyBase());
190 struct MyDerived : MyBase {
191 static std::unique_ptr<MyDerived>
make() {
192 return std::unique_ptr<MyDerived>(
new MyDerived());
196 py::class_<MyBase>(
m,
"MyBase")
199 py::class_<MyDerived, MyBase>(
m,
"MyDerived")
204 struct ConvertibleFromUserType {
207 ConvertibleFromUserType(UserType u) :
i(u.value()) { }
210 py::class_<ConvertibleFromUserType>(
m,
"AcceptsUserType")
211 .def(py::init<UserType>());
212 py::implicitly_convertible<UserType, ConvertibleFromUserType>();
214 m.def(
"implicitly_convert_argument", [](
const ConvertibleFromUserType &r) {
return r.i; });
215 m.def(
"implicitly_convert_variable", [](py::object o) {
219 const auto &r = o.cast<
const ConvertibleFromUserType &>();
222 m.add_object(
"implicitly_convert_variable_fail", [&] {
223 auto f = [](PyObject *, PyObject *
args) -> PyObject * {
224 auto o = py::reinterpret_borrow<py::tuple>(
args)[0];
226 o.cast<
const ConvertibleFromUserType &>();
227 }
catch (
const py::cast_error &
e) {
230 return py::str().release().ptr();
233 auto def =
new PyMethodDef{
"f",
f, METH_VARARGS,
nullptr};
234 return py::reinterpret_steal<py::object>(PyCFunction_NewEx(def,
nullptr, m.ptr()));
240 static void *
operator new(
size_t s) {
py::print(
"A new",
s); return ::operator
new(
s); }
241 static void *
operator new(
size_t s,
void *
ptr) {
py::print(
"A placement-new", s);
return ptr; }
242 static void operator delete(
void *
p) {
py::print(
"A delete"); return ::operator
delete(
p); }
244 struct HasOpNewDelSize {
246 static void *
operator new(
size_t s) {
py::print(
"B new",
s); return ::operator
new(
s); }
247 static void *
operator new(
size_t s,
void *
ptr) {
py::print(
"B placement-new", s);
return ptr; }
248 static void operator delete(
void *
p,
size_t s) {
py::print(
"B delete", s); return ::operator
delete(
p); }
250 struct AliasedHasOpNewDelSize {
252 static void *
operator new(
size_t s) {
py::print(
"C new",
s); return ::operator
new(
s); }
253 static void *
operator new(
size_t s,
void *
ptr) {
py::print(
"C placement-new", s);
return ptr; }
254 static void operator delete(
void *
p,
size_t s) {
py::print(
"C delete", s); return ::operator
delete(
p); }
255 virtual ~AliasedHasOpNewDelSize() =
default;
256 AliasedHasOpNewDelSize() =
default;
257 AliasedHasOpNewDelSize(
const AliasedHasOpNewDelSize&) =
delete;
259 struct PyAliasedHasOpNewDelSize : AliasedHasOpNewDelSize {
260 PyAliasedHasOpNewDelSize() =
default;
261 PyAliasedHasOpNewDelSize(
int) { }
264 struct HasOpNewDelBoth {
266 static void *
operator new(
size_t s) {
py::print(
"D new",
s); return ::operator
new(
s); }
267 static void *
operator new(
size_t s,
void *
ptr) {
py::print(
"D placement-new", s);
return ptr; }
268 static void operator delete(
void *
p) {
py::print(
"D delete"); return ::operator
delete(
p); }
269 static void operator delete(
void *
p,
size_t s) {
py::print(
"D wrong delete", s); return ::operator
delete(
p); }
271 py::class_<HasOpNewDel>(
m,
"HasOpNewDel").def(py::init<>());
272 py::class_<HasOpNewDelSize>(
m,
"HasOpNewDelSize").def(py::init<>());
273 py::class_<HasOpNewDelBoth>(
m,
"HasOpNewDelBoth").def(py::init<>());
274 py::class_<AliasedHasOpNewDelSize, PyAliasedHasOpNewDelSize> aliased(m,
"AliasedHasOpNewDelSize");
275 aliased.def(py::init<>());
276 aliased.attr(
"size_noalias") = py::int_(
sizeof(AliasedHasOpNewDelSize));
277 aliased.attr(
"size_alias") = py::int_(
sizeof(PyAliasedHasOpNewDelSize));
281 bind_local<LocalExternal, 17>(
m,
"LocalExternal", py::module_local());
292 class PublicistA :
public ProtectedA {
297 py::class_<ProtectedA>(
m,
"ProtectedA")
299 #
if !defined(_MSC_VER) || _MSC_VER >= 1910
302 .def(
"foo",
static_cast<int (ProtectedA::*)() const
>(&
PublicistA::foo));
307 virtual ~ProtectedB() =
default;
308 ProtectedB() =
default;
309 ProtectedB(
const ProtectedB &) =
delete;
312 virtual int foo()
const {
return value; }
318 class TrampolineB :
public ProtectedB {
323 class PublicistB :
public ProtectedB {
328 py::class_<ProtectedB, TrampolineB>(
m,
"ProtectedB")
330 #
if !defined(_MSC_VER) || _MSC_VER >= 1910
333 .def(
"foo",
static_cast<int (ProtectedB::*)() const
>(&
PublicistB::foo));
337 struct BraceInitialization {
342 py::class_<BraceInitialization>(
m,
"BraceInitialization")
343 .def(py::init<int, const std::string &>())
344 .def_readwrite(
"field1", &BraceInitialization::field1)
345 .def_readwrite(
"field2", &BraceInitialization::field2);
349 py::class_<NoBraceInitialization>(
m,
"NoBraceInitialization")
355 struct BogusImplicitConversion {
356 BogusImplicitConversion(
const BogusImplicitConversion &) =
default;
359 py::class_<BogusImplicitConversion>(
m,
"BogusImplicitConversion")
360 .def(py::init<const BogusImplicitConversion &>());
362 py::implicitly_convertible<int, BogusImplicitConversion>();
369 py::class_<NestBase>
base(m,
"NestBase");
370 base.def(py::init<>());
371 py::class_<Nested>(base,
"Nested")
373 .def(
"fn", [](Nested &,
int, NestBase &, Nested &) {})
374 .def(
"fa", [](Nested &,
int, NestBase &, Nested &) {},
375 "a"_a,
"b"_a,
"c"_a);
376 base.def(
"g", [](NestBase &, Nested &) {});
377 base.def(
"h", []() {
return NestBase(); });
384 struct NotRegistered {};
385 struct StringWrapper { std::string
str; };
386 m.def(
"test_error_after_conversions", [](
int) {});
387 m.def(
"test_error_after_conversions",
388 [](StringWrapper) -> NotRegistered {
return {}; });
389 py::class_<StringWrapper>(
m,
"StringWrapper").def(py::init<std::string>());
390 py::implicitly_convertible<std::string, StringWrapper>();
392 #if defined(PYBIND11_CPP17) 396 py::class_<Aligned>(
m,
"Aligned")
402 struct IsFinal final {};
403 py::class_<IsFinal>(
m,
"IsFinal", py::is_final());
406 struct IsNonFinalFinal {};
407 py::class_<IsNonFinalFinal>(
m,
"IsNonFinalFinal", py::is_final());
409 struct PyPrintDestructor {
410 PyPrintDestructor() =
default;
411 ~PyPrintDestructor() {
414 void throw_something() {
throw std::runtime_error(
"error"); }
416 py::class_<PyPrintDestructor>(
m,
"PyPrintDestructor")
418 .def(
"throw_something", &PyPrintDestructor::throw_something);
430 using DoesntBreak3 = py::class_<BreaksBase<3>, std::unique_ptr<BreaksBase<3>>>;
435 using DoesntBreak8 = py::class_<BreaksBase<8>, std::shared_ptr<BreaksBase<8>>>;
436 #define CHECK_BASE(N) static_assert(std::is_same<typename DoesntBreak##N::type, BreaksBase<N>>::value, \ 437 "DoesntBreak" #N " has wrong type!") 439 #define CHECK_ALIAS(N) static_assert(DoesntBreak##N::has_alias && std::is_same<typename DoesntBreak##N::type_alias, BreaksTramp<N>>::value, \ 440 "DoesntBreak" #N " has wrong type_alias!") 441 #define CHECK_NOALIAS(N) static_assert(!DoesntBreak##N::has_alias && std::is_void<typename DoesntBreak##N::type_alias>::value, \ 442 "DoesntBreak" #N " has type alias, but shouldn't!") 444 #define CHECK_HOLDER(N, TYPE) static_assert(std::is_same<typename DoesntBreak##N::holder_type, std::TYPE##_ptr<BreaksBase<N>>>::value, \ 445 "DoesntBreak" #N " has wrong holder_type!") 454 #define CHECK_BROKEN(N) static_assert(std::is_same<typename Breaks##N::type, BreaksBase<-N>>::value, \ 455 "Breaks1 has wrong type!"); void print(const Matrix &A, const string &s, ostream &stream)
py::class_< BreaksBase< 3 >, std::unique_ptr< BreaksBase< 3 >>> DoesntBreak3
py::class_< BreaksBase< 7 >, BreaksTramp< 7 >, std::shared_ptr< BreaksBase< 7 >>> DoesntBreak7
NoBraceInitialization(std::initializer_list< T > l)
static LabeledSymbol make(gtsam::Key key)
NoBraceInitialization(std::vector< int > v)
py::class_< BreaksBase< 4 >, BreaksTramp< 4 >> DoesntBreak4
void print_destroyed(T *inst, Values &&...values)
TEST_SUBMODULE(class_, m)
py::class_< BreaksBase< 8 >, std::shared_ptr< BreaksBase< 8 >>> DoesntBreak8
void foo(CV_QUALIFIER Matrix3d &m)
static const Line3 l(Rot3(), 1, 1)
Tuple< Args... > make_tuple(Args...args)
Creates a tuple object, deducing the target type from the types of arguments.
py::class_< BreaksBase< 5 >> DoesntBreak5
py::class_< BreaksBase< 1 >, std::unique_ptr< BreaksBase< 1 >>, BreaksTramp< 1 >> DoesntBreak1
unsigned __int64 uint64_t
py::class_< BreaksBase< 6 >, std::shared_ptr< BreaksBase< 6 >>, BreaksTramp< 6 >> DoesntBreak6
const mpreal mod(const mpreal &x, const mpreal &y, mp_rnd_t rnd_mode=mpreal::get_default_rnd())
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Array< double, 1, 3 > e(1./3., 0.5, 2.)
#define PYBIND11_OVERRIDE(ret_type, cname, fn,...)
_W64 unsigned int uintptr_t
void print_created(T *inst, Values &&...values)
detail::initimpl::constructor< Args... > init()
Binds an existing constructor taking arguments Args...
#define CHECK_HOLDER(N, TYPE)
py::class_< BreaksBase< 2 >, BreaksTramp< 2 >, std::unique_ptr< BreaksBase< 2 >>> DoesntBreak2
Annotation for function names.
Annotation indicating that a class derives from another given type.