46 value = std::move(
m.value);
66 value = std::move(
m.value);
208 py::module_ m_tag =
m.def_submodule(
"tag");
209 #define MAKE_TAG_TYPE(Name) \
210 struct Name##_tag {}; \
211 py::class_<Name##_tag>(m_tag, #Name "_tag").def(py::init<>()); \
212 m_tag.attr(#Name) = py::cast(Name##_tag{})
230 py::class_<TestFactory1>(
m,
"TestFactory1")
237 py::class_<TestFactory2>(
m,
"TestFactory2")
239 .def(
py::init([](unique_ptr_tag, std::string
v) {
247 auto c4a = [
c](pointer_tag, TF4_tag,
int a) {
253 py::class_<TestFactory3, std::shared_ptr<TestFactory3>> pyTestFactory3(
m,
"TestFactory3");
258 pyTestFactory3.def(
"__init__", [](
TestFactory3 &
self, std::string
v) {
268 [](shared_ptr_tag, TF4_tag,
int a) {
return std::make_shared<TestFactory4>(
a); }))
270 [](shared_ptr_tag, TF5_tag,
int a) {
return std::make_shared<TestFactory5>(
a); }))
274 .def(
py::init([](null_unique_ptr_tag) {
return std::unique_ptr<TestFactory3>(); }))
275 .def(
py::init([](null_shared_ptr_tag) {
return std::shared_ptr<TestFactory3>(); }))
280 py::class_<TestFactory4, TestFactory3, std::shared_ptr<TestFactory4>>(
m,
"TestFactory4")
285 py::class_<TestFactory5, TestFactory3, std::shared_ptr<TestFactory5>>(
m,
"TestFactory5");
289 py::class_<TestFactory6, PyTF6>(
m,
"TestFactory6")
292 .def(
py::init([](alias_tag, std::string
s) {
return PyTF6(std::move(
s)); }))
293 .def(
py::init([](alias_tag, pointer_tag,
int i) {
return new PyTF6(
i); }))
302 "get_cstats", &ConstructorStats::get<TestFactory6>, py::return_value_policy::reference)
304 "get_alias_cstats", &ConstructorStats::get<PyTF6>, py::return_value_policy::reference);
308 py::class_<TestFactory7, PyTF7, std::shared_ptr<TestFactory7>>(
m,
"TestFactory7")
311 [](pointer_tag,
int i) {
return new PyTF7(
i); }))
313 [](mixed_tag,
int i) {
return PyTF7(
i); }))
315 [](mixed_tag,
const std::string &
s) {
return new PyTF7((
int)
s.size()); }))
318 .def(
py::init([](alias_tag, pointer_tag,
int i) {
return new PyTF7(
i); },
319 [](alias_tag, pointer_tag,
int i) {
return new PyTF7(10 *
i); }))
321 [](shared_ptr_tag, base_tag,
int i) {
return std::make_shared<TestFactory7>(
i); },
322 [](shared_ptr_tag, base_tag,
int i) {
324 return std::shared_ptr<TestFactory7>(
p);
328 int i) {
return std::make_shared<TestFactory7>(
i); },
329 [](shared_ptr_tag, invalid_base_tag,
int i) {
330 return std::make_shared<TestFactory7>(
i);
337 "get_cstats", &ConstructorStats::get<TestFactory7>, py::return_value_policy::reference)
339 "get_alias_cstats", &ConstructorStats::get<PyTF7>, py::return_value_policy::reference);
343 class NoPlacementNew {
345 explicit NoPlacementNew(
int i) :
i(
i) {}
347 auto *
p = ::operator
new(
s);
351 static void operator delete(
void *
p) {
353 ::operator
delete(
p);
358 py::class_<NoPlacementNew>(
m,
"NoPlacementNew")
359 .def(py::init<int>())
360 .def(
py::init([]() {
return new NoPlacementNew(100); }))
366 NoisyAlloc(
const NoisyAlloc &) =
default;
369 ~NoisyAlloc() {
py::print(
"~NoisyAlloc()"); }
371 static void *
operator new(
size_t s) {
373 return ::operator
new(
s);
375 static void *
operator new(
size_t,
void *
p) {
379 static void operator delete(
void *
p,
size_t) {
381 ::operator
delete(
p);
383 static void operator delete(
void *,
void *) {
py::print(
"noisy placement delete"); }
386 py::class_<NoisyAlloc> pyNoisyAlloc(
m,
"NoisyAlloc");
391 pyNoisyAlloc.def(
"__init__", [](NoisyAlloc *
a,
int i) {
392 new (
a) NoisyAlloc(
i);
396 pyNoisyAlloc.def(
py::init([](
double d) {
return new NoisyAlloc(
d); }));
399 pyNoisyAlloc.def(
py::init([](
int i,
int) {
return new NoisyAlloc(
i); }));
401 pyNoisyAlloc.def(
py::init([](
double d,
int) {
return NoisyAlloc(
d); }));
404 pyNoisyAlloc.def(
"__init__",
405 [](NoisyAlloc &
a,
double d,
double) {
new (&
a) NoisyAlloc(
d); });
408 pyNoisyAlloc.def(
py::init([](
int i,
double) {
return new NoisyAlloc(
i); }));
412 "__init__", [](NoisyAlloc &
a,
int i,
const std::string &) {
new (&
a) NoisyAlloc(
i); });
419 struct BadF1 : BadF1Base {};
420 struct PyBadF1 : BadF1 {};
421 py::class_<BadF1, PyBadF1, std::shared_ptr<BadF1>> bf1(
m,
"BadF1");
423 bf1.def(
py::init([]() {
return 3; }));
425 bf1.def(
py::init([]() {
static int three = 3;
return &three; }));
428 bf1.def(
py::init([]() {
return std::shared_ptr<BadF1Base>(
new BadF1()); }));