2015-07-05 18:05:44 +00:00
|
|
|
/*
|
2016-08-12 11:50:00 +00:00
|
|
|
tests/test_smart_ptr.cpp -- binding classes with custom reference counting,
|
2015-07-29 15:51:54 +00:00
|
|
|
implicit conversions between types
|
2015-07-05 18:05:44 +00:00
|
|
|
|
2016-04-17 18:21:41 +00:00
|
|
|
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
|
2015-07-05 18:05:44 +00:00
|
|
|
|
|
|
|
All rights reserved. Use of this source code is governed by a
|
|
|
|
BSD-style license that can be found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2016-08-12 11:50:00 +00:00
|
|
|
#include "pybind11_tests.h"
|
2015-07-05 18:05:44 +00:00
|
|
|
#include "object.h"
|
|
|
|
|
2016-01-17 21:36:40 +00:00
|
|
|
/// Custom object with builtin reference counting (see 'object.h' for the implementation)
|
|
|
|
class MyObject1 : public Object {
|
2015-07-05 18:05:44 +00:00
|
|
|
public:
|
2016-01-17 21:36:40 +00:00
|
|
|
MyObject1(int value) : value(value) {
|
Improve constructor/destructor tracking
This commit rewrites the examples that look for constructor/destructor
calls to do so via static variable tracking rather than output parsing.
The added ConstructorStats class provides methods to keep track of
constructors and destructors, number of default/copy/move constructors,
and number of copy/move assignments. It also provides a mechanism for
storing values (e.g. for value construction), and then allows all of
this to be checked at the end of a test by getting the statistics for a
C++ (or python mapping) class.
By not relying on the precise pattern of constructions/destructions,
but rather simply ensuring that every construction is matched with a
destruction on the same object, we ensure that everything that gets
created also gets destroyed as expected.
This replaces all of the various "std::cout << whatever" code in
constructors/destructors with
`print_created(this)`/`print_destroyed(this)`/etc. functions which
provide similar output, but now has a unified format across the
different examples, including a new ### prefix that makes mixed example
output and lifecycle events easier to distinguish.
With this change, relaxed mode is no longer needed, which enables
testing for proper destruction under MSVC, and under any other compiler
that generates code calling extra constructors, or optimizes away any
constructors. GCC/clang are used as the baseline for move
constructors; the tests are adapted to allow more move constructors to
be evoked (but other types are constructors much have matching counts).
This commit also disables output buffering of tests, as the buffering
sometimes results in C++ output ending up in the middle of python
output (or vice versa), depending on the OS/python version.
2016-08-07 17:05:26 +00:00
|
|
|
print_created(this, toString());
|
2015-07-05 18:05:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string toString() const {
|
2016-01-17 21:36:40 +00:00
|
|
|
return "MyObject1[" + std::to_string(value) + "]";
|
2015-07-05 18:05:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2016-01-17 21:36:40 +00:00
|
|
|
virtual ~MyObject1() {
|
Improve constructor/destructor tracking
This commit rewrites the examples that look for constructor/destructor
calls to do so via static variable tracking rather than output parsing.
The added ConstructorStats class provides methods to keep track of
constructors and destructors, number of default/copy/move constructors,
and number of copy/move assignments. It also provides a mechanism for
storing values (e.g. for value construction), and then allows all of
this to be checked at the end of a test by getting the statistics for a
C++ (or python mapping) class.
By not relying on the precise pattern of constructions/destructions,
but rather simply ensuring that every construction is matched with a
destruction on the same object, we ensure that everything that gets
created also gets destroyed as expected.
This replaces all of the various "std::cout << whatever" code in
constructors/destructors with
`print_created(this)`/`print_destroyed(this)`/etc. functions which
provide similar output, but now has a unified format across the
different examples, including a new ### prefix that makes mixed example
output and lifecycle events easier to distinguish.
With this change, relaxed mode is no longer needed, which enables
testing for proper destruction under MSVC, and under any other compiler
that generates code calling extra constructors, or optimizes away any
constructors. GCC/clang are used as the baseline for move
constructors; the tests are adapted to allow more move constructors to
be evoked (but other types are constructors much have matching counts).
This commit also disables output buffering of tests, as the buffering
sometimes results in C++ output ending up in the middle of python
output (or vice versa), depending on the OS/python version.
2016-08-07 17:05:26 +00:00
|
|
|
print_destroyed(this);
|
2015-07-05 18:05:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
int value;
|
|
|
|
};
|
|
|
|
|
2016-01-17 21:36:40 +00:00
|
|
|
/// Object managed by a std::shared_ptr<>
|
|
|
|
class MyObject2 {
|
2015-11-24 22:05:58 +00:00
|
|
|
public:
|
|
|
|
MyObject2(int value) : value(value) {
|
Improve constructor/destructor tracking
This commit rewrites the examples that look for constructor/destructor
calls to do so via static variable tracking rather than output parsing.
The added ConstructorStats class provides methods to keep track of
constructors and destructors, number of default/copy/move constructors,
and number of copy/move assignments. It also provides a mechanism for
storing values (e.g. for value construction), and then allows all of
this to be checked at the end of a test by getting the statistics for a
C++ (or python mapping) class.
By not relying on the precise pattern of constructions/destructions,
but rather simply ensuring that every construction is matched with a
destruction on the same object, we ensure that everything that gets
created also gets destroyed as expected.
This replaces all of the various "std::cout << whatever" code in
constructors/destructors with
`print_created(this)`/`print_destroyed(this)`/etc. functions which
provide similar output, but now has a unified format across the
different examples, including a new ### prefix that makes mixed example
output and lifecycle events easier to distinguish.
With this change, relaxed mode is no longer needed, which enables
testing for proper destruction under MSVC, and under any other compiler
that generates code calling extra constructors, or optimizes away any
constructors. GCC/clang are used as the baseline for move
constructors; the tests are adapted to allow more move constructors to
be evoked (but other types are constructors much have matching counts).
This commit also disables output buffering of tests, as the buffering
sometimes results in C++ output ending up in the middle of python
output (or vice versa), depending on the OS/python version.
2016-08-07 17:05:26 +00:00
|
|
|
print_created(this, toString());
|
2015-11-24 22:05:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string toString() const {
|
|
|
|
return "MyObject2[" + std::to_string(value) + "]";
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~MyObject2() {
|
Improve constructor/destructor tracking
This commit rewrites the examples that look for constructor/destructor
calls to do so via static variable tracking rather than output parsing.
The added ConstructorStats class provides methods to keep track of
constructors and destructors, number of default/copy/move constructors,
and number of copy/move assignments. It also provides a mechanism for
storing values (e.g. for value construction), and then allows all of
this to be checked at the end of a test by getting the statistics for a
C++ (or python mapping) class.
By not relying on the precise pattern of constructions/destructions,
but rather simply ensuring that every construction is matched with a
destruction on the same object, we ensure that everything that gets
created also gets destroyed as expected.
This replaces all of the various "std::cout << whatever" code in
constructors/destructors with
`print_created(this)`/`print_destroyed(this)`/etc. functions which
provide similar output, but now has a unified format across the
different examples, including a new ### prefix that makes mixed example
output and lifecycle events easier to distinguish.
With this change, relaxed mode is no longer needed, which enables
testing for proper destruction under MSVC, and under any other compiler
that generates code calling extra constructors, or optimizes away any
constructors. GCC/clang are used as the baseline for move
constructors; the tests are adapted to allow more move constructors to
be evoked (but other types are constructors much have matching counts).
This commit also disables output buffering of tests, as the buffering
sometimes results in C++ output ending up in the middle of python
output (or vice versa), depending on the OS/python version.
2016-08-07 17:05:26 +00:00
|
|
|
print_destroyed(this);
|
2015-11-24 22:05:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
int value;
|
|
|
|
};
|
|
|
|
|
2016-01-17 21:36:40 +00:00
|
|
|
/// Object managed by a std::shared_ptr<>, additionally derives from std::enable_shared_from_this<>
|
|
|
|
class MyObject3 : public std::enable_shared_from_this<MyObject3> {
|
|
|
|
public:
|
|
|
|
MyObject3(int value) : value(value) {
|
Improve constructor/destructor tracking
This commit rewrites the examples that look for constructor/destructor
calls to do so via static variable tracking rather than output parsing.
The added ConstructorStats class provides methods to keep track of
constructors and destructors, number of default/copy/move constructors,
and number of copy/move assignments. It also provides a mechanism for
storing values (e.g. for value construction), and then allows all of
this to be checked at the end of a test by getting the statistics for a
C++ (or python mapping) class.
By not relying on the precise pattern of constructions/destructions,
but rather simply ensuring that every construction is matched with a
destruction on the same object, we ensure that everything that gets
created also gets destroyed as expected.
This replaces all of the various "std::cout << whatever" code in
constructors/destructors with
`print_created(this)`/`print_destroyed(this)`/etc. functions which
provide similar output, but now has a unified format across the
different examples, including a new ### prefix that makes mixed example
output and lifecycle events easier to distinguish.
With this change, relaxed mode is no longer needed, which enables
testing for proper destruction under MSVC, and under any other compiler
that generates code calling extra constructors, or optimizes away any
constructors. GCC/clang are used as the baseline for move
constructors; the tests are adapted to allow more move constructors to
be evoked (but other types are constructors much have matching counts).
This commit also disables output buffering of tests, as the buffering
sometimes results in C++ output ending up in the middle of python
output (or vice versa), depending on the OS/python version.
2016-08-07 17:05:26 +00:00
|
|
|
print_created(this, toString());
|
2016-01-17 21:36:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string toString() const {
|
|
|
|
return "MyObject3[" + std::to_string(value) + "]";
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~MyObject3() {
|
Improve constructor/destructor tracking
This commit rewrites the examples that look for constructor/destructor
calls to do so via static variable tracking rather than output parsing.
The added ConstructorStats class provides methods to keep track of
constructors and destructors, number of default/copy/move constructors,
and number of copy/move assignments. It also provides a mechanism for
storing values (e.g. for value construction), and then allows all of
this to be checked at the end of a test by getting the statistics for a
C++ (or python mapping) class.
By not relying on the precise pattern of constructions/destructions,
but rather simply ensuring that every construction is matched with a
destruction on the same object, we ensure that everything that gets
created also gets destroyed as expected.
This replaces all of the various "std::cout << whatever" code in
constructors/destructors with
`print_created(this)`/`print_destroyed(this)`/etc. functions which
provide similar output, but now has a unified format across the
different examples, including a new ### prefix that makes mixed example
output and lifecycle events easier to distinguish.
With this change, relaxed mode is no longer needed, which enables
testing for proper destruction under MSVC, and under any other compiler
that generates code calling extra constructors, or optimizes away any
constructors. GCC/clang are used as the baseline for move
constructors; the tests are adapted to allow more move constructors to
be evoked (but other types are constructors much have matching counts).
This commit also disables output buffering of tests, as the buffering
sometimes results in C++ output ending up in the middle of python
output (or vice versa), depending on the OS/python version.
2016-08-07 17:05:26 +00:00
|
|
|
print_destroyed(this);
|
2016-01-17 21:36:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
int value;
|
|
|
|
};
|
|
|
|
|
2016-09-04 22:23:55 +00:00
|
|
|
class MyObject4 {
|
|
|
|
public:
|
|
|
|
MyObject4(int value) : value{value} {
|
|
|
|
print_created(this);
|
|
|
|
}
|
|
|
|
int value;
|
|
|
|
private:
|
|
|
|
~MyObject4() {
|
|
|
|
print_destroyed(this);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-01-17 21:36:40 +00:00
|
|
|
/// Make pybind aware of the ref-counted wrapper type (s)
|
2015-10-18 14:48:30 +00:00
|
|
|
PYBIND11_DECLARE_HOLDER_TYPE(T, ref<T>);
|
2015-11-24 22:05:58 +00:00
|
|
|
PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr<T>);
|
2015-07-05 18:05:44 +00:00
|
|
|
|
2016-01-17 21:36:40 +00:00
|
|
|
Object *make_object_1() { return new MyObject1(1); }
|
|
|
|
ref<Object> make_object_2() { return new MyObject1(2); }
|
2015-07-05 18:05:44 +00:00
|
|
|
|
2016-01-17 21:36:40 +00:00
|
|
|
MyObject1 *make_myobject1_1() { return new MyObject1(4); }
|
|
|
|
ref<MyObject1> make_myobject1_2() { return new MyObject1(5); }
|
|
|
|
|
|
|
|
MyObject2 *make_myobject2_1() { return new MyObject2(6); }
|
|
|
|
std::shared_ptr<MyObject2> make_myobject2_2() { return std::make_shared<MyObject2>(7); }
|
|
|
|
|
|
|
|
MyObject3 *make_myobject3_1() { return new MyObject3(8); }
|
|
|
|
std::shared_ptr<MyObject3> make_myobject3_2() { return std::make_shared<MyObject3>(9); }
|
2015-11-24 22:05:58 +00:00
|
|
|
|
2015-07-05 18:05:44 +00:00
|
|
|
void print_object_1(const Object *obj) { std::cout << obj->toString() << std::endl; }
|
|
|
|
void print_object_2(ref<Object> obj) { std::cout << obj->toString() << std::endl; }
|
|
|
|
void print_object_3(const ref<Object> &obj) { std::cout << obj->toString() << std::endl; }
|
|
|
|
void print_object_4(const ref<Object> *obj) { std::cout << (*obj)->toString() << std::endl; }
|
|
|
|
|
2016-01-17 21:36:40 +00:00
|
|
|
void print_myobject1_1(const MyObject1 *obj) { std::cout << obj->toString() << std::endl; }
|
|
|
|
void print_myobject1_2(ref<MyObject1> obj) { std::cout << obj->toString() << std::endl; }
|
|
|
|
void print_myobject1_3(const ref<MyObject1> &obj) { std::cout << obj->toString() << std::endl; }
|
|
|
|
void print_myobject1_4(const ref<MyObject1> *obj) { std::cout << (*obj)->toString() << std::endl; }
|
2015-07-05 18:05:44 +00:00
|
|
|
|
2015-11-24 22:05:58 +00:00
|
|
|
void print_myobject2_1(const MyObject2 *obj) { std::cout << obj->toString() << std::endl; }
|
|
|
|
void print_myobject2_2(std::shared_ptr<MyObject2> obj) { std::cout << obj->toString() << std::endl; }
|
|
|
|
void print_myobject2_3(const std::shared_ptr<MyObject2> &obj) { std::cout << obj->toString() << std::endl; }
|
|
|
|
void print_myobject2_4(const std::shared_ptr<MyObject2> *obj) { std::cout << (*obj)->toString() << std::endl; }
|
|
|
|
|
2016-01-17 21:36:40 +00:00
|
|
|
void print_myobject3_1(const MyObject3 *obj) { std::cout << obj->toString() << std::endl; }
|
|
|
|
void print_myobject3_2(std::shared_ptr<MyObject3> obj) { std::cout << obj->toString() << std::endl; }
|
|
|
|
void print_myobject3_3(const std::shared_ptr<MyObject3> &obj) { std::cout << obj->toString() << std::endl; }
|
|
|
|
void print_myobject3_4(const std::shared_ptr<MyObject3> *obj) { std::cout << (*obj)->toString() << std::endl; }
|
|
|
|
|
2016-09-03 18:54:22 +00:00
|
|
|
test_initializer smart_ptr([](py::module &m) {
|
2015-07-05 18:05:44 +00:00
|
|
|
py::class_<Object, ref<Object>> obj(m, "Object");
|
|
|
|
obj.def("getRefCount", &Object::getRefCount);
|
|
|
|
|
2016-01-17 21:36:40 +00:00
|
|
|
py::class_<MyObject1, ref<MyObject1>>(m, "MyObject1", obj)
|
2015-07-05 18:05:44 +00:00
|
|
|
.def(py::init<int>());
|
|
|
|
|
|
|
|
m.def("make_object_1", &make_object_1);
|
|
|
|
m.def("make_object_2", &make_object_2);
|
2016-01-17 21:36:40 +00:00
|
|
|
m.def("make_myobject1_1", &make_myobject1_1);
|
|
|
|
m.def("make_myobject1_2", &make_myobject1_2);
|
2015-07-05 18:05:44 +00:00
|
|
|
m.def("print_object_1", &print_object_1);
|
|
|
|
m.def("print_object_2", &print_object_2);
|
|
|
|
m.def("print_object_3", &print_object_3);
|
|
|
|
m.def("print_object_4", &print_object_4);
|
2016-01-17 21:36:40 +00:00
|
|
|
m.def("print_myobject1_1", &print_myobject1_1);
|
|
|
|
m.def("print_myobject1_2", &print_myobject1_2);
|
|
|
|
m.def("print_myobject1_3", &print_myobject1_3);
|
|
|
|
m.def("print_myobject1_4", &print_myobject1_4);
|
2015-07-05 18:05:44 +00:00
|
|
|
|
2015-11-24 22:05:58 +00:00
|
|
|
py::class_<MyObject2, std::shared_ptr<MyObject2>>(m, "MyObject2")
|
|
|
|
.def(py::init<int>());
|
|
|
|
m.def("make_myobject2_1", &make_myobject2_1);
|
|
|
|
m.def("make_myobject2_2", &make_myobject2_2);
|
|
|
|
m.def("print_myobject2_1", &print_myobject2_1);
|
|
|
|
m.def("print_myobject2_2", &print_myobject2_2);
|
|
|
|
m.def("print_myobject2_3", &print_myobject2_3);
|
|
|
|
m.def("print_myobject2_4", &print_myobject2_4);
|
|
|
|
|
2016-01-17 21:36:40 +00:00
|
|
|
py::class_<MyObject3, std::shared_ptr<MyObject3>>(m, "MyObject3")
|
|
|
|
.def(py::init<int>());
|
|
|
|
m.def("make_myobject3_1", &make_myobject3_1);
|
|
|
|
m.def("make_myobject3_2", &make_myobject3_2);
|
|
|
|
m.def("print_myobject3_1", &print_myobject3_1);
|
|
|
|
m.def("print_myobject3_2", &print_myobject3_2);
|
|
|
|
m.def("print_myobject3_3", &print_myobject3_3);
|
|
|
|
m.def("print_myobject3_4", &print_myobject3_4);
|
|
|
|
|
2016-09-04 22:23:55 +00:00
|
|
|
py::class_<MyObject4, std::unique_ptr<MyObject4, py::nodelete>>(m, "MyObject4")
|
|
|
|
.def(py::init<int>())
|
|
|
|
.def_readwrite("value", &MyObject4::value);
|
|
|
|
|
2016-01-17 21:36:40 +00:00
|
|
|
py::implicitly_convertible<py::int_, MyObject1>();
|
Improve constructor/destructor tracking
This commit rewrites the examples that look for constructor/destructor
calls to do so via static variable tracking rather than output parsing.
The added ConstructorStats class provides methods to keep track of
constructors and destructors, number of default/copy/move constructors,
and number of copy/move assignments. It also provides a mechanism for
storing values (e.g. for value construction), and then allows all of
this to be checked at the end of a test by getting the statistics for a
C++ (or python mapping) class.
By not relying on the precise pattern of constructions/destructions,
but rather simply ensuring that every construction is matched with a
destruction on the same object, we ensure that everything that gets
created also gets destroyed as expected.
This replaces all of the various "std::cout << whatever" code in
constructors/destructors with
`print_created(this)`/`print_destroyed(this)`/etc. functions which
provide similar output, but now has a unified format across the
different examples, including a new ### prefix that makes mixed example
output and lifecycle events easier to distinguish.
With this change, relaxed mode is no longer needed, which enables
testing for proper destruction under MSVC, and under any other compiler
that generates code calling extra constructors, or optimizes away any
constructors. GCC/clang are used as the baseline for move
constructors; the tests are adapted to allow more move constructors to
be evoked (but other types are constructors much have matching counts).
This commit also disables output buffering of tests, as the buffering
sometimes results in C++ output ending up in the middle of python
output (or vice versa), depending on the OS/python version.
2016-08-07 17:05:26 +00:00
|
|
|
|
|
|
|
// Expose constructor stats for the ref type
|
|
|
|
m.def("cstats_ref", &ConstructorStats::get<ref_tag>);
|
2016-09-03 18:54:22 +00:00
|
|
|
});
|