mirror of
https://github.com/pybind/pybind11.git
synced 2026-07-11 09:38:05 +00:00
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.
This commit is contained in:
@@ -8,32 +8,25 @@
|
||||
BSD-style license that can be found in the LICENSE file.
|
||||
*/
|
||||
|
||||
#include <unordered_map>
|
||||
#include <list>
|
||||
#include "example.h"
|
||||
#include "constructor-stats.h"
|
||||
|
||||
class ExampleMandA {
|
||||
public:
|
||||
ExampleMandA() {
|
||||
cout << "Called ExampleMandA default constructor.." << endl;
|
||||
}
|
||||
ExampleMandA(int value) : value(value) {
|
||||
cout << "Called ExampleMandA constructor with value " << value << ".." << endl;
|
||||
}
|
||||
ExampleMandA(const ExampleMandA &e) : value(e.value) {
|
||||
cout << "Called ExampleMandA copy constructor with value " << value << ".." << endl;
|
||||
}
|
||||
ExampleMandA(ExampleMandA &&e) : value(e.value) {
|
||||
cout << "Called ExampleMandA move constructor with value " << value << ".." << endl;
|
||||
e.value = 0;
|
||||
}
|
||||
~ExampleMandA() {
|
||||
cout << "Called ExampleMandA destructor (" << value << ")" << endl;
|
||||
}
|
||||
ExampleMandA() { print_default_created(this); }
|
||||
ExampleMandA(int value) : value(value) { print_created(this, value); }
|
||||
ExampleMandA(const ExampleMandA &e) : value(e.value) { print_copy_created(this); }
|
||||
ExampleMandA(ExampleMandA &&e) : value(e.value) { print_move_created(this); }
|
||||
~ExampleMandA() { print_destroyed(this); }
|
||||
|
||||
std::string toString() {
|
||||
return "ExampleMandA[value=" + std::to_string(value) + "]";
|
||||
}
|
||||
|
||||
void operator=(const ExampleMandA &e) { cout << "Assignment operator" << endl; value = e.value; }
|
||||
void operator=(ExampleMandA &&e) { cout << "Move assignment operator" << endl; value = e.value; e.value = 0;}
|
||||
void operator=(const ExampleMandA &e) { print_copy_assigned(this); value = e.value; }
|
||||
void operator=(ExampleMandA &&e) { print_move_assigned(this); value = e.value; }
|
||||
|
||||
void add1(ExampleMandA other) { value += other.value; } // passing by value
|
||||
void add2(ExampleMandA &other) { value += other.value; } // passing by reference
|
||||
@@ -88,5 +81,6 @@ void init_ex_methods_and_attributes(py::module &m) {
|
||||
.def("internal4", &ExampleMandA::internal4)
|
||||
.def("internal5", &ExampleMandA::internal5)
|
||||
.def("__str__", &ExampleMandA::toString)
|
||||
.def_readwrite("value", &ExampleMandA::value);
|
||||
.def_readwrite("value", &ExampleMandA::value)
|
||||
;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user