46 value = std::move(
m.value);
66 value = std::move(
m.value);
179 return std::unique_ptr<TestFactory1>(
new TestFactory1(a));
191 return std::unique_ptr<TestFactory2>(
new TestFactory2(a));
201 return std::shared_ptr<TestFactory3>(
new TestFactory3(a));
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); }))
296 [](base_tag, alias_tag, pointer_tag,
int i) {
return (
TestFactory6 *)
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);
348 py::print(
"operator new called, returning", reinterpret_cast<uintptr_t>(
p));
351 static void operator delete(
void *
p) {
352 py::print(
"operator delete called on", reinterpret_cast<uintptr_t>(
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()); }));
TestFactory2(std::string v)
TestFactory7(TestFactory7 &&f) noexcept
TestFactory7(const TestFactory7 &f)
PyTF7(PyTF7 &&f) noexcept
void print_destroyed(T *inst, Values &&...values)
TestFactory6(const TestFactory6 &f)
void ignoreOldStyleInitWarnings(F &&body)
EIGEN_STRONG_INLINE Packet4f print(const Packet4f &a)
static TestFactory2 construct2(std::string a)
void print_copy_created(T *inst, Values &&...values)
void print_default_created(T *inst, Values &&...values)
static TestFactory2 * construct2()
void print_move_assigned(T *inst, Values &&...values)
static std::shared_ptr< TestFactory3 > construct3(int a)
Array< int, Dynamic, 1 > v
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
static std::unique_ptr< TestFactory1 > construct1(int a)
TestFactory2 & operator=(TestFactory2 &&m) noexcept
PyTF6(TestFactory6 &&base)
TestFactory3 & operator=(TestFactory3 &&m) noexcept
PyTF6(PyTF6 &&f) noexcept
#define PYBIND11_OVERRIDE(ret_type, cname, fn,...)
static TestFactory1 * construct1_string(std::string a)
static std::unique_ptr< TestFactory2 > construct2(int a)
TestFactory3(std::string v)
void print_created(T *inst, Values &&...values)
TestFactory6(TestFactory6 &&f) noexcept
detail::initimpl::constructor< Args... > init()
Binds an existing constructor taking arguments Args...
std::string format(const std::string &str, const std::vector< std::string > &find, const std::vector< std::string > &replace)
#define MAKE_TAG_TYPE(Name)
TestFactory1(std::string v)
static TestFactory1 * construct1()
Annotation indicating that a class derives from another given type.
void print_move_created(T *inst, Values &&...values)
static TestFactory3 * construct3()
TestFactory3(TestFactory3 &&m) noexcept
TestFactory1 & operator=(TestFactory1 &&)=delete
TEST_SUBMODULE(factory_constructors, m)
TestFactory2(TestFactory2 &&m) noexcept