145 py::module_ m_tag =
m.def_submodule(
"tag");
146 #define MAKE_TAG_TYPE(Name) \
147 struct Name##_tag {}; \
148 py::class_<Name##_tag>(m_tag, #Name "_tag").def(py::init<>()); \
149 m_tag.attr(#Name) = py::cast(Name##_tag{})
167 py::class_<TestFactory1>(
m,
"TestFactory1")
174 py::class_<TestFactory2>(
m,
"TestFactory2")
183 auto c4a = [c](pointer_tag, TF4_tag,
int a) { (void) c;
return new TestFactory4(a);};
186 py::class_<TestFactory3, std::shared_ptr<TestFactory3>> pyTestFactory3(
m,
"TestFactory3");
198 .def(
py::init([](shared_ptr_tag, TF4_tag,
int a) {
return std::make_shared<TestFactory4>(a); }))
199 .def(
py::init([](shared_ptr_tag, TF5_tag,
int a) {
return std::make_shared<TestFactory5>(a); }))
203 .def(
py::init([](null_unique_ptr_tag) {
return std::unique_ptr<TestFactory3>(); }))
204 .def(
py::init([](null_shared_ptr_tag) {
return std::shared_ptr<TestFactory3>(); }))
210 py::class_<TestFactory4, TestFactory3, std::shared_ptr<TestFactory4>>(
m,
"TestFactory4")
215 py::class_<TestFactory5, TestFactory3, std::shared_ptr<TestFactory5>>(
m,
"TestFactory5");
219 py::class_<TestFactory6, PyTF6>(
m,
"TestFactory6")
222 .def(
py::init([](alias_tag, std::string s) {
return PyTF6(s); }))
223 .def(
py::init([](alias_tag, pointer_tag,
int i) {
return new PyTF6(
i); }))
230 .def_static(
"get_cstats", &ConstructorStats::get<TestFactory6>, py::return_value_policy::reference)
231 .def_static(
"get_alias_cstats", &ConstructorStats::get<PyTF6>, py::return_value_policy::reference)
236 py::class_<TestFactory7, PyTF7, std::shared_ptr<TestFactory7>>(
m,
"TestFactory7")
239 [](
int i) {
return PyTF7(
i); }))
242 [](pointer_tag,
int i) {
return new PyTF7(
i); }))
245 [](mixed_tag,
int i) {
return PyTF7(
i); }))
247 [](mixed_tag, std::string s) {
return TestFactory7((
int) s.size()); },
248 [](mixed_tag, std::string s) {
return new PyTF7((
int) s.size()); }))
253 [](alias_tag, pointer_tag,
int i) {
return new PyTF7(
i); },
254 [](alias_tag, pointer_tag,
int i) {
return new PyTF7(10*
i); }))
256 [](shared_ptr_tag, base_tag,
int i) {
return std::make_shared<TestFactory7>(
i); },
257 [](shared_ptr_tag, base_tag,
int i) {
auto *p =
new PyTF7(
i);
return std::shared_ptr<TestFactory7>(p); }))
259 [](shared_ptr_tag, invalid_base_tag,
int i) {
return std::make_shared<TestFactory7>(
i); },
260 [](shared_ptr_tag, invalid_base_tag,
int i) {
return std::make_shared<TestFactory7>(
i); }))
265 .def_static(
"get_cstats", &ConstructorStats::get<TestFactory7>, py::return_value_policy::reference)
266 .def_static(
"get_alias_cstats", &ConstructorStats::get<PyTF7>, py::return_value_policy::reference)
271 class NoPlacementNew {
273 NoPlacementNew(
int i) :
i(
i) { }
275 auto *p = ::operator
new(s);
276 py::print(
"operator new called, returning",
reinterpret_cast<uintptr_t
>(p));
279 static void operator delete(
void *p) {
280 py::print(
"operator delete called on",
reinterpret_cast<uintptr_t
>(p));
281 ::operator
delete(p);
286 py::class_<NoPlacementNew>(
m,
"NoPlacementNew")
287 .def(py::init<int>())
288 .def(
py::init([]() {
return new NoPlacementNew(100); }))
296 NoisyAlloc(
const NoisyAlloc &) =
default;
297 NoisyAlloc(
int i) {
py::print(py::str(
"NoisyAlloc(int {})").format(
i)); }
298 NoisyAlloc(
double d) {
py::print(py::str(
"NoisyAlloc(double {})").format(d)); }
299 ~NoisyAlloc() {
py::print(
"~NoisyAlloc()"); }
301 static void *
operator new(
size_t s) {
py::print(
"noisy new"); return ::operator
new(s); }
302 static void *
operator new(
size_t,
void *p) {
py::print(
"noisy placement new");
return p; }
303 static void operator delete(
void *p,
size_t) {
py::print(
"noisy delete"); ::operator
delete(p); }
304 static void operator delete(
void *,
void *) {
py::print(
"noisy placement delete"); }
305 #if defined(_MSC_VER) && _MSC_VER < 1910
307 static void operator delete(
void *p) {
py::print(
"noisy delete"); ::operator
delete(p); }
312 py::class_<NoisyAlloc> pyNoisyAlloc(
m,
"NoisyAlloc");
317 pyNoisyAlloc.def(
"__init__", [](NoisyAlloc *a,
int i) {
new (a) NoisyAlloc(
i); });
320 pyNoisyAlloc.def(
py::init([](
double d) {
return new NoisyAlloc(d); }));
323 pyNoisyAlloc.def(
py::init([](
int i,
int) {
return new NoisyAlloc(
i); }));
325 pyNoisyAlloc.def(
py::init([](
double d,
int) {
return NoisyAlloc(d); }));
328 pyNoisyAlloc.def(
"__init__", [](NoisyAlloc &a,
double d,
double) {
new (&a) NoisyAlloc(d); });
331 pyNoisyAlloc.def(
py::init([](
int i,
double) {
return new NoisyAlloc(
i); }));
334 pyNoisyAlloc.def(
"__init__", [](NoisyAlloc &a,
int i, std::string) {
new (&a) NoisyAlloc(
i); });
343 struct BadF1 : BadF1Base {};
344 struct PyBadF1 : BadF1 {};
345 py::class_<BadF1, PyBadF1, std::shared_ptr<BadF1>> bf1(
m,
"BadF1");
347 bf1.def(
py::init([]() {
return 3; }));
349 bf1.def(
py::init([]() {
static int three = 3;
return &three; }));
352 bf1.def(
py::init([]() {
return std::shared_ptr<BadF1Base>(
new BadF1()); }));