10 #if defined(__INTEL_COMPILER) && __cplusplus >= 201703L 14 # include <aligned_new> 26 # pragma warning(disable : 4324) 41 struct NoConstructor {
42 NoConstructor() =
default;
43 NoConstructor(
const NoConstructor &) =
default;
44 NoConstructor(NoConstructor &&) =
default;
45 static NoConstructor *new_instance() {
46 auto *ptr =
new NoConstructor();
52 struct NoConstructorNew {
53 NoConstructorNew() =
default;
54 NoConstructorNew(
const NoConstructorNew &) =
default;
55 NoConstructorNew(NoConstructorNew &&) =
default;
56 static NoConstructorNew *new_instance() {
57 auto *ptr =
new NoConstructorNew();
64 py::class_<NoConstructor>(
m,
"NoConstructor")
65 .def_static(
"new_instance", &NoConstructor::new_instance,
"Return an instance");
67 py::class_<NoConstructorNew>(
m,
"NoConstructorNew")
68 .def(
py::init([](
const NoConstructorNew &
self) {
return self; }))
69 .def_static(
"__new__",
70 [](
const py::object &) {
return NoConstructorNew::new_instance(); });
75 Pet(
const std::string &
name,
const std::string &species)
76 : m_name(name), m_species(species) {}
77 std::string
name()
const {
return m_name; }
78 std::string species()
const {
return m_species; }
82 std::string m_species;
85 class Dog :
public Pet {
87 explicit Dog(
const std::string &
name) : Pet(name,
"dog") {}
88 std::string bark()
const {
return "Woof!"; }
91 class Rabbit :
public Pet {
93 explicit Rabbit(
const std::string &
name) : Pet(name,
"parrot") {}
96 class Hamster :
public Pet {
98 explicit Hamster(
const std::string &
name) : Pet(name,
"rodent") {}
101 class Chimera :
public Pet {
102 Chimera() : Pet(
"Kimmy",
"chimera") {}
105 py::class_<Pet> pet_class(m,
"Pet");
106 pet_class.def(py::init<std::string, std::string>())
108 .def(
"species", &Pet::species);
111 py::class_<Dog>(
m,
"Dog", pet_class).def(py::init<std::string>());
114 py::class_<Rabbit, Pet>(
m,
"Rabbit").def(py::init<std::string>());
117 py::class_<Hamster, Pet>(
m,
"Hamster").def(py::init<std::string>());
120 py::class_<Chimera, Pet>(
m,
"Chimera");
122 m.def(
"pet_name_species",
123 [](
const Pet &pet) {
return pet.name() +
" is a " + pet.species(); });
124 m.def(
"dog_bark", [](
const Dog &dog) {
return dog.
bark(); });
128 BaseClass() =
default;
129 BaseClass(
const BaseClass &) =
default;
130 BaseClass(BaseClass &&) =
default;
131 virtual ~BaseClass() =
default;
133 struct DerivedClass1 : BaseClass {};
134 struct DerivedClass2 : BaseClass {};
136 py::class_<BaseClass>(
m,
"BaseClass").def(py::init<>());
137 py::class_<DerivedClass1>(
m,
"DerivedClass1").def(py::init<>());
138 py::class_<DerivedClass2>(
m,
"DerivedClass2").def(py::init<>());
140 m.def(
"return_class_1", []() -> BaseClass * {
return new DerivedClass1(); });
141 m.def(
"return_class_2", []() -> BaseClass * {
return new DerivedClass2(); });
142 m.def(
"return_class_n", [](
int n) -> BaseClass * {
144 return new DerivedClass1();
147 return new DerivedClass2();
149 return new BaseClass();
151 m.def(
"return_none", []() -> BaseClass * {
return nullptr; });
154 m.def(
"check_instances", [](
const py::list &
l) {
156 py::isinstance<py::dict>(l[1]),
157 py::isinstance<Pet>(l[2]),
158 py::isinstance<Pet>(l[3]),
159 py::isinstance<Dog>(l[4]),
160 py::isinstance<Rabbit>(l[5]),
161 py::isinstance<UnregisteredType>(l[6]));
167 m.def(
"check_type", [](
int category) {
173 return py::type::of<DerivedClass1>();
175 return py::type::of<Invalid>();
178 m.def(
"get_type_of", [](py::object ob) {
return py::type::of(std::move(ob)); });
180 m.def(
"get_type_classic", [](py::handle
h) {
return h.get_type(); });
182 m.def(
"as_type", [](
const py::object &ob) {
return py::type(ob); });
185 struct MismatchBase1 {};
186 struct MismatchDerived1 : MismatchBase1 {};
188 struct MismatchBase2 {};
189 struct MismatchDerived2 : MismatchBase2 {};
191 m.def(
"mismatched_holder_1", []() {
192 auto mod = py::module_::import(
"__main__");
193 py::class_<MismatchBase1, std::shared_ptr<MismatchBase1>>(mod,
"MismatchBase1");
194 py::class_<MismatchDerived1, MismatchBase1>(mod,
"MismatchDerived1");
196 m.def(
"mismatched_holder_2", []() {
197 auto mod = py::module_::import(
"__main__");
198 py::class_<MismatchBase2>(mod,
"MismatchBase2");
199 py::class_<MismatchDerived2, std::shared_ptr<MismatchDerived2>, MismatchBase2>(
200 mod,
"MismatchDerived2");
206 static std::unique_ptr<MyBase>
make() {
return std::unique_ptr<MyBase>(
new MyBase()); }
209 struct MyDerived : MyBase {
210 static std::unique_ptr<MyDerived>
make() {
211 return std::unique_ptr<MyDerived>(
new MyDerived());
215 py::class_<MyBase>(
m,
"MyBase").def_static(
"make", &
MyBase::make);
217 py::class_<MyDerived, MyBase>(
m,
"MyDerived")
222 struct ConvertibleFromUserType {
225 explicit ConvertibleFromUserType(UserType u) :
i(u.value()) {}
228 py::class_<ConvertibleFromUserType>(
m,
"AcceptsUserType").def(py::init<UserType>());
229 py::implicitly_convertible<UserType, ConvertibleFromUserType>();
231 m.def(
"implicitly_convert_argument", [](
const ConvertibleFromUserType &r) {
return r.i; });
232 m.def(
"implicitly_convert_variable", [](
const py::object &o) {
236 const auto &r = o.cast<
const ConvertibleFromUserType &>();
239 m.add_object(
"implicitly_convert_variable_fail", [&] {
240 auto f = [](PyObject *, PyObject *
args) -> PyObject * {
241 auto o = py::reinterpret_borrow<py::tuple>(
args)[0];
243 o.cast<
const ConvertibleFromUserType &>();
244 }
catch (
const py::cast_error &
e) {
247 return py::str().release().ptr();
250 auto *def =
new PyMethodDef{
"f",
f, METH_VARARGS,
nullptr};
251 py::capsule def_capsule(def,
252 [](
void *ptr) {
delete reinterpret_cast<PyMethodDef *
>(ptr); });
253 return py::reinterpret_steal<py::object>(
254 PyCFunction_NewEx(def, def_capsule.ptr(), m.ptr()));
260 static void *
operator new(
size_t s) {
262 return ::operator
new(
s);
264 static void *
operator new(
size_t s,
void *ptr) {
268 static void operator delete(
void *
p) {
270 return ::operator
delete(
p);
273 struct HasOpNewDelSize {
275 static void *
operator new(
size_t s) {
277 return ::operator
new(
s);
279 static void *
operator new(
size_t s,
void *ptr) {
283 static void operator delete(
void *
p,
size_t s) {
285 return ::operator
delete(
p);
288 struct AliasedHasOpNewDelSize {
290 static void *
operator new(
size_t s) {
292 return ::operator
new(
s);
294 static void *
operator new(
size_t s,
void *ptr) {
298 static void operator delete(
void *
p,
size_t s) {
300 return ::operator
delete(
p);
302 virtual ~AliasedHasOpNewDelSize() =
default;
303 AliasedHasOpNewDelSize() =
default;
304 AliasedHasOpNewDelSize(
const AliasedHasOpNewDelSize &) =
delete;
306 struct PyAliasedHasOpNewDelSize : AliasedHasOpNewDelSize {
307 PyAliasedHasOpNewDelSize() =
default;
308 explicit PyAliasedHasOpNewDelSize(
int) {}
311 struct HasOpNewDelBoth {
313 static void *
operator new(
size_t s) {
315 return ::operator
new(
s);
317 static void *
operator new(
size_t s,
void *ptr) {
321 static void operator delete(
void *
p) {
323 return ::operator
delete(
p);
325 static void operator delete(
void *
p,
size_t s) {
327 return ::operator
delete(
p);
330 py::class_<HasOpNewDel>(
m,
"HasOpNewDel").def(py::init<>());
331 py::class_<HasOpNewDelSize>(
m,
"HasOpNewDelSize").def(py::init<>());
332 py::class_<HasOpNewDelBoth>(
m,
"HasOpNewDelBoth").def(py::init<>());
333 py::class_<AliasedHasOpNewDelSize, PyAliasedHasOpNewDelSize> aliased(m,
334 "AliasedHasOpNewDelSize");
335 aliased.def(py::init<>());
336 aliased.attr(
"size_noalias") = py::int_(
sizeof(AliasedHasOpNewDelSize));
337 aliased.attr(
"size_alias") = py::int_(
sizeof(PyAliasedHasOpNewDelSize));
341 bind_local<LocalExternal, 17>(
m,
"LocalExternal", py::module_local());
352 class PublicistA :
public ProtectedA {
357 py::class_<ProtectedA>(
m,
"ProtectedA").def(py::init<>()).def(
"foo", &
PublicistA::foo);
361 virtual ~ProtectedB() =
default;
362 ProtectedB() =
default;
363 ProtectedB(
const ProtectedB &) =
delete;
366 virtual int foo()
const {
return value; }
372 class TrampolineB :
public ProtectedB {
377 class PublicistB :
public ProtectedB {
382 ~PublicistB()
override{};
386 py::class_<ProtectedB, TrampolineB>(
m,
"ProtectedB")
391 struct BraceInitialization {
396 py::class_<BraceInitialization>(
m,
"BraceInitialization")
397 .def(py::init<int, const std::string &>())
398 .def_readwrite(
"field1", &BraceInitialization::field1)
399 .def_readwrite(
"field2", &BraceInitialization::field2);
403 py::class_<NoBraceInitialization>(
m,
"NoBraceInitialization")
409 struct BogusImplicitConversion {
410 BogusImplicitConversion(
const BogusImplicitConversion &) =
default;
413 py::class_<BogusImplicitConversion>(
m,
"BogusImplicitConversion")
414 .def(py::init<const BogusImplicitConversion &>());
416 py::implicitly_convertible<int, BogusImplicitConversion>();
423 py::class_<NestBase>
base(m,
"NestBase");
424 base.def(py::init<>());
425 py::class_<Nested>(base,
"Nested")
427 .def(
"fn", [](Nested &,
int, NestBase &, Nested &) {})
429 "fa", [](Nested &,
int, NestBase &, Nested &) {},
"a"_a,
"b"_a,
"c"_a);
430 base.def(
"g", [](NestBase &, Nested &) {});
431 base.def(
"h", []() {
return NestBase(); });
438 struct NotRegistered {};
439 struct StringWrapper {
442 m.def(
"test_error_after_conversions", [](
int) {});
443 m.def(
"test_error_after_conversions",
444 [](
const StringWrapper &) -> NotRegistered {
return {}; });
445 py::class_<StringWrapper>(
m,
"StringWrapper").def(py::init<std::string>());
446 py::implicitly_convertible<std::string, StringWrapper>();
448 #if defined(PYBIND11_CPP17) 452 py::class_<Aligned>(
m,
"Aligned").def(py::init<>()).def(
"ptr", &Aligned::ptr);
456 struct IsFinal final {};
457 py::class_<IsFinal>(
m,
"IsFinal", py::is_final());
460 struct IsNonFinalFinal {};
461 py::class_<IsNonFinalFinal>(
m,
"IsNonFinalFinal", py::is_final());
464 struct PyPrintDestructor {
465 PyPrintDestructor() =
default;
466 ~PyPrintDestructor() {
py::print(
"Print from destructor"); }
467 void throw_something() {
throw std::runtime_error(
"error"); }
469 py::class_<PyPrintDestructor>(
m,
"PyPrintDestructor")
471 .def(
"throw_something", &PyPrintDestructor::throw_something);
474 struct SamePointer {};
475 static SamePointer samePointer;
476 py::class_<SamePointer, std::unique_ptr<SamePointer, py::nodelete>>(
m,
"SamePointer")
477 .def(
py::init([]() {
return &samePointer; }));
480 py::class_<Empty>(
m,
"Empty").def(py::init<>());
483 struct BaseWithNested {
487 struct DerivedWithNested : BaseWithNested {
491 py::class_<BaseWithNested> baseWithNested_class(m,
"BaseWithNested");
492 py::class_<DerivedWithNested, BaseWithNested> derivedWithNested_class(m,
"DerivedWithNested");
493 py::class_<BaseWithNested::Nested>(baseWithNested_class,
"Nested")
494 .def_static(
"get_name", []() {
return "BaseWithNested::Nested"; });
495 py::class_<DerivedWithNested::Nested>(derivedWithNested_class,
"Nested")
496 .def_static(
"get_name", []() {
return "DerivedWithNested::Nested"; });
500 struct OtherDuplicate {};
501 struct DuplicateNested {};
502 struct OtherDuplicateNested {};
504 m.def(
"register_duplicate_class_name", [](
const py::module_ &m) {
505 py::class_<Duplicate>(
m,
"Duplicate");
506 py::class_<OtherDuplicate>(
m,
"Duplicate");
508 m.def(
"register_duplicate_class_type", [](
const py::module_ &m) {
509 py::class_<OtherDuplicate>(
m,
"OtherDuplicate");
510 py::class_<OtherDuplicate>(
m,
"YetAnotherDuplicate");
512 m.def(
"register_duplicate_nested_class_name", [](
const py::object >) {
513 py::class_<DuplicateNested>(gt,
"DuplicateNested");
514 py::class_<OtherDuplicateNested>(gt,
"DuplicateNested");
516 m.def(
"register_duplicate_nested_class_type", [](
const py::object >) {
517 py::class_<OtherDuplicateNested>(gt,
"OtherDuplicateNested");
518 py::class_<OtherDuplicateNested>(gt,
"YetAnotherDuplicateNested");
534 using DoesntBreak3 = py::class_<BreaksBase<3>, std::unique_ptr<BreaksBase<3>>>;
539 using DoesntBreak8 = py::class_<BreaksBase<8>, std::shared_ptr<BreaksBase<8>>>;
540 #define CHECK_BASE(N) \ 541 static_assert(std::is_same<typename DoesntBreak##N::type, BreaksBase<(N)>>::value, \ 542 "DoesntBreak" #N " has wrong type!") 551 #define CHECK_ALIAS(N) \ 553 DoesntBreak##N::has_alias \ 554 && std::is_same<typename DoesntBreak##N::type_alias, BreaksTramp<(N)>>::value, \ 555 "DoesntBreak" #N " has wrong type_alias!") 556 #define CHECK_NOALIAS(N) \ 557 static_assert(!DoesntBreak##N::has_alias \ 558 && std::is_void<typename DoesntBreak##N::type_alias>::value, \ 559 "DoesntBreak" #N " has type alias, but shouldn't!") 568 #define CHECK_HOLDER(N, TYPE) \ 569 static_assert(std::is_same<typename DoesntBreak##N::holder_type, \ 570 std::TYPE##_ptr<BreaksBase<(N)>>>::value, \ 571 "DoesntBreak" #N " has wrong holder_type!") 586 #define CHECK_BROKEN(N) \ 587 static_assert(std::is_same<typename Breaks##N::type, BreaksBase<-(N)>>::value, \ 588 "Breaks1 has wrong type!"); 590 #ifdef PYBIND11_NEVER_DEFINED_EVER
NoBraceInitialization(std::initializer_list< T > l)
static LabeledSymbol make(gtsam::Key key)
NoBraceInitialization(std::vector< int > v)
py::class_< BreaksBase< 2 >, BreaksTramp< 2 >, std::unique_ptr< BreaksBase< 2 > >> DoesntBreak2
void print_destroyed(T *inst, Values &&...values)
TEST_SUBMODULE(class_, m)
py::class_< BreaksBase< 3 >, std::unique_ptr< BreaksBase< 3 > >> DoesntBreak3
EIGEN_STRONG_INLINE Packet4f print(const Packet4f &a)
py::class_< BreaksBase< 8 >, std::shared_ptr< BreaksBase< 8 > >> DoesntBreak8
py::class_< BreaksBase< 5 > > DoesntBreak5
void foo(CV_QUALIFIER Matrix3d &m)
static const Line3 l(Rot3(), 1, 1)
py::class_< BreaksBase< 7 >, BreaksTramp< 7 >, std::shared_ptr< BreaksBase< 7 > >> DoesntBreak7
Array< int, Dynamic, 1 > v
unsigned __int64 uint64_t
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Array< double, 1, 3 > e(1./3., 0.5, 2.)
py::class_< BreaksBase< 4 >, BreaksTramp< 4 > > DoesntBreak4
#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)
Annotation for function names.
Annotation indicating that a class derives from another given type.
py::class_< BreaksBase< 6 >, std::shared_ptr< BreaksBase< 6 > >, BreaksTramp< 6 > > DoesntBreak6
py::class_< BreaksBase< 1 >, std::unique_ptr< BreaksBase< 1 > >, BreaksTramp< 1 > > DoesntBreak1