79 virtual int get() {
return value; }
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>>(
m,
"TestFactory3")
195 .def(
py::init([](shared_ptr_tag, TF4_tag,
int a) {
return std::make_shared<TestFactory4>(
a); }))
196 .def(
py::init([](shared_ptr_tag, TF5_tag,
int a) {
return std::make_shared<TestFactory5>(
a); }))
200 .def(
py::init([](null_unique_ptr_tag) {
return std::unique_ptr<TestFactory3>(); }))
201 .def(
py::init([](null_shared_ptr_tag) {
return std::shared_ptr<TestFactory3>(); }))
207 py::class_<TestFactory4, TestFactory3, std::shared_ptr<TestFactory4>>(
m,
"TestFactory4")
212 py::class_<TestFactory5, TestFactory3, std::shared_ptr<TestFactory5>>(
m,
"TestFactory5");
216 py::class_<TestFactory6, PyTF6>(
m,
"TestFactory6")
220 .def(
py::init([](alias_tag, pointer_tag,
int i) {
return new PyTF6(i); }))
227 .def_static(
"get_cstats", &ConstructorStats::get<TestFactory6>, py::return_value_policy::reference)
228 .def_static(
"get_alias_cstats", &ConstructorStats::get<PyTF6>, py::return_value_policy::reference)
233 py::class_<TestFactory7, PyTF7, std::shared_ptr<TestFactory7>>(
m,
"TestFactory7")
236 [](
int i) {
return PyTF7(i); }))
239 [](pointer_tag,
int i) {
return new PyTF7(i); }))
242 [](mixed_tag,
int i) {
return PyTF7(i); }))
244 [](mixed_tag, std::string s) {
return TestFactory7((
int) s.size()); },
245 [](mixed_tag, std::string
s) {
return new PyTF7((
int) s.size()); }))
247 [](base_tag, pointer_tag,
int i) {
return new TestFactory7(i); },
250 [](alias_tag, pointer_tag,
int i) {
return new PyTF7(i); },
251 [](alias_tag, pointer_tag,
int i) {
return new PyTF7(10*i); }))
253 [](shared_ptr_tag, base_tag,
int i) {
return std::make_shared<TestFactory7>(
i); },
254 [](shared_ptr_tag, base_tag,
int i) {
auto *
p =
new PyTF7(i);
return std::shared_ptr<TestFactory7>(
p); }))
256 [](shared_ptr_tag, invalid_base_tag,
int i) {
return std::make_shared<TestFactory7>(
i); },
257 [](shared_ptr_tag, invalid_base_tag,
int i) {
return std::make_shared<TestFactory7>(
i); }))
262 .def_static(
"get_cstats", &ConstructorStats::get<TestFactory7>, py::return_value_policy::reference)
263 .def_static(
"get_alias_cstats", &ConstructorStats::get<PyTF7>, py::return_value_policy::reference)
268 class NoPlacementNew {
270 NoPlacementNew(
int i) :
i(i) { }
272 auto *
p = ::operator
new(
s);
273 py::print(
"operator new called, returning", reinterpret_cast<uintptr_t>(
p));
276 static void operator delete(
void *
p) {
277 py::print(
"operator delete called on", reinterpret_cast<uintptr_t>(
p));
278 ::operator
delete(
p);
283 py::class_<NoPlacementNew>(
m,
"NoPlacementNew")
284 .def(py::init<int>())
285 .def(
py::init([]() {
return new NoPlacementNew(100); }))
293 NoisyAlloc(
const NoisyAlloc &) =
default;
296 ~NoisyAlloc() {
py::print(
"~NoisyAlloc()"); }
298 static void *
operator new(
size_t s) {
py::print(
"noisy new"); return ::operator
new(
s); }
299 static void *
operator new(
size_t,
void *
p) {
py::print(
"noisy placement new");
return p; }
300 static void operator delete(
void *
p,
size_t) {
py::print(
"noisy delete"); ::operator
delete(
p); }
301 static void operator delete(
void *,
void *) {
py::print(
"noisy placement delete"); }
302 #if defined(_MSC_VER) && _MSC_VER < 1910 304 static void operator delete(
void *
p) {
py::print(
"noisy delete"); ::operator
delete(
p); }
307 py::class_<NoisyAlloc>(
m,
"NoisyAlloc")
311 .def(
"__init__", [](NoisyAlloc *a,
int i) {
new (
a) NoisyAlloc(i); })
312 .def(
py::init([](
double d) {
return new NoisyAlloc(d); }))
315 .def(
py::init([](
int i,
int) {
return new NoisyAlloc(i); }))
317 .def(
py::init([](
double d,
int) {
return NoisyAlloc(d); }))
319 .def(
"__init__", [](NoisyAlloc &a,
double d,
double) {
new (&
a) NoisyAlloc(d); })
321 .def(
py::init([](
int i,
double) {
return new NoisyAlloc(i); }))
323 .def(
"__init__", [](NoisyAlloc &a,
int i, std::string) {
new (&
a) NoisyAlloc(i); })
332 struct BadF1 : BadF1Base {};
333 struct PyBadF1 : BadF1 {};
334 py::class_<BadF1, PyBadF1, std::shared_ptr<BadF1>> bf1(m,
"BadF1");
336 bf1.def(
py::init([]() {
return 3; }));
338 bf1.def(
py::init([]() {
static int three = 3;
return &three; }));
341 bf1.def(
py::init([]() {
return std::shared_ptr<BadF1Base>(
new BadF1()); }));
void print(const Matrix &A, const string &s, ostream &stream)
TestFactory2(std::string v)
TestFactory7(const TestFactory7 &f)
TestFactory3 & operator=(TestFactory3 &&m)
void print_destroyed(T *inst, Values &&...values)
TestFactory6(const TestFactory6 &f)
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)
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
TestFactory3(TestFactory3 &&m)
static std::unique_ptr< TestFactory1 > construct1(int a)
PyTF6(TestFactory6 &&base)
TestFactory7(TestFactory7 &&f)
#define PYBIND11_OVERRIDE(ret_type, cname, fn,...)
static TestFactory1 * construct1_string(std::string a)
static std::unique_ptr< TestFactory2 > construct2(int a)
TestFactory2(TestFactory2 &&m)
TestFactory3(std::string v)
void print_created(T *inst, Values &&...values)
detail::initimpl::constructor< Args... > init()
Binds an existing constructor taking arguments Args...
#define MAKE_TAG_TYPE(Name)
TestFactory2 & operator=(TestFactory2 &&m)
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()
TestFactory6(TestFactory6 &&f)
TestFactory1 & operator=(TestFactory1 &&)=delete
TEST_SUBMODULE(factory_constructors, m)