78 virtual int get() {
return value; }
145 #define MAKE_TAG_TYPE(Name) \ 146 struct Name##_tag {}; \ 147 py::class_<Name##_tag>(m_tag, #Name "_tag").def(py::init<>()); \ 148 m_tag.attr(#Name) = py::cast(Name##_tag{}) 180 auto c4a = [c](pointer_tag, TF4_tag,
int a) { (void) c;
return new TestFactory4(a);};
192 .
def(
py::init([](shared_ptr_tag, TF4_tag,
int a) {
return std::make_shared<TestFactory4>(a); }))
193 .
def(
py::init([](shared_ptr_tag, TF5_tag,
int a) {
return std::make_shared<TestFactory5>(a); }))
231 [](
int i) {
return PyTF7(i); }))
234 [](pointer_tag,
int i) {
return new PyTF7(i); }))
237 [](mixed_tag,
int i) {
return PyTF7(i); }))
239 [](mixed_tag, std::string s) {
return TestFactory7((
int) s.size()); },
240 [](mixed_tag, std::string s) {
return new PyTF7((
int) s.size()); }))
242 [](base_tag, pointer_tag,
int i) {
return new TestFactory7(i); },
245 [](alias_tag, pointer_tag,
int i) {
return new PyTF7(i); },
246 [](alias_tag, pointer_tag,
int i) {
return new PyTF7(10*i); }))
248 [](shared_ptr_tag, base_tag,
int i) {
return std::make_shared<TestFactory7>(
i); },
249 [](shared_ptr_tag, base_tag,
int i) {
auto *p =
new PyTF7(i);
return std::shared_ptr<TestFactory7>(p); }))
251 [](shared_ptr_tag, invalid_base_tag,
int i) {
return std::make_shared<TestFactory7>(
i); },
252 [](shared_ptr_tag, invalid_base_tag,
int i) {
return std::make_shared<TestFactory7>(
i); }))
263 class NoPlacementNew {
265 NoPlacementNew(
int i) :
i(i) { }
267 auto *p = ::operator
new(s);
268 py::print(
"operator new called, returning", reinterpret_cast<uintptr_t>(p));
271 static void operator delete(
void *p) {
272 py::print(
"operator delete called on", reinterpret_cast<uintptr_t>(p));
273 ::operator
delete(p);
279 .def(py::init<int>())
280 .def(
py::init([]() {
return new NoPlacementNew(100); }))
288 NoisyAlloc(
const NoisyAlloc &) =
default;
291 ~NoisyAlloc() {
py::print(
"~NoisyAlloc()"); }
293 static void *
operator new(
size_t s) {
py::print(
"noisy new"); return ::operator
new(s); }
294 static void *
operator new(
size_t,
void *p) {
py::print(
"noisy placement new");
return p; }
295 static void operator delete(
void *p,
size_t) {
py::print(
"noisy delete"); ::operator
delete(p); }
296 static void operator delete(
void *,
void *) {
py::print(
"noisy placement delete"); }
297 #if defined(_MSC_VER) && _MSC_VER < 1910 299 static void operator delete(
void *p) {
py::print(
"noisy delete"); ::operator
delete(p); }
306 .def(
"__init__", [](NoisyAlloc *a,
int i) {
new (a) NoisyAlloc(i); })
307 .def(
py::init([](
double d) {
return new NoisyAlloc(d); }))
310 .
def(
py::init([](
int i,
int) {
return new NoisyAlloc(i); }))
312 .
def(
py::init([](
double d,
int) {
return NoisyAlloc(d); }))
314 .
def(
"__init__", [](NoisyAlloc &a,
double d,
double) {
new (&a) NoisyAlloc(d); })
316 .def(
py::init([](
int i,
double) {
return new NoisyAlloc(i); }))
318 .
def(
"__init__", [](NoisyAlloc &a,
int i, std::string) {
new (&a) NoisyAlloc(i); })
327 struct BadF1 : BadF1Base {};
328 struct PyBadF1 : BadF1 {};
333 bf1.
def(
py::init([]() {
static int three = 3;
return &three; }));
336 bf1.
def(
py::init([]() {
return std::shared_ptr<BadF1Base>(
new BadF1()); }));
static TestFactory1 * construct1_string(std::string a)
TestFactory6(TestFactory6 &&f)
static TestFactory3 * construct3()
TestFactory2(TestFactory2 &&m)
void print_destroyed(T *inst, Values &&...values)
PyTF6(TestFactory6 &&base)
TestFactory1(std::string v)
void print_copy_created(T *inst, Values &&...values)
auto format(const std::locale &loc, const CharT *fmt, const Streamable &tp) -> decltype(to_stream(std::declval< std::basic_ostream< CharT > &>(), fmt, tp), std::basic_string< CharT >
test_initializer factory_constructors("factory_constructors", test_submodule_factory_constructors)
TestFactory3(std::string v)
Wrapper for Python extension modules.
TestFactory3(TestFactory3 &&m)
void print_default_created(T *inst, Values &&...values)
static TestFactory2 * construct2()
detail::initimpl::constructor< Args... > init()
Binds an existing constructor taking arguments Args...
static TestFactory1 * construct1()
TestFactory3 & operator=(TestFactory3 &&m)
void print_move_assigned(T *inst, Values &&...values)
static std::shared_ptr< TestFactory3 > construct3(int a)
TestFactory2(std::string v)
TestFactory2 & operator=(TestFactory2 &&m)
TestFactory1 & operator=(TestFactory1 &&)=delete
TestFactory7(const TestFactory7 &f)
class_ & def_static(const char *name_, Func &&f, const Extra &... extra)
static std::unique_ptr< TestFactory2 > construct2(int a)
static TestFactory2 construct2(std::string a)
TestFactory6(const TestFactory6 &f)
void print_created(T *inst, Values &&...values)
detail::enable_if_t<!detail::move_never< T >::value, T > move(object &&obj)
#define MAKE_TAG_TYPE(Name)
static std::unique_ptr< TestFactory1 > construct1(int a)
void print(Args &&...args)
void print_move_created(T *inst, Values &&...values)
#define TEST_SUBMODULE(name, variable)
TestFactory7(TestFactory7 &&f)
bool typename Extra class_ & def(const char *name_, Func &&f, const Extra &... extra)
auto to_string(T &&value) -> decltype(std::forward< T >(value))
Convert an object to a string (directly forward if this can become a string)
#define PYBIND11_OVERLOAD(ret_type, cname, fn,...)
class_ & def_readwrite(const char *name, D C::*pm, const Extra &... extra)