A helper class that counts the total number of constructor and destructor calls.
| 32 | /// A helper class that counts the total number of constructor and |
| 33 | /// destructor calls. |
| 34 | class Constructable { |
| 35 | private: |
| 36 | static int num_constructor_calls; |
| 37 | static int num_move_constructor_calls; |
| 38 | static int num_copy_constructor_calls; |
| 39 | static int num_deconstructor_calls; |
| 40 | static int num_assignment_calls; |
| 41 | static int num_move_assignment_calls; |
| 42 | static int num_copy_assignment_calls; |
| 43 | |
| 44 | static std::unordered_set<const Constructable*> destroyed_mem; |
| 45 | |
| 46 | bool m_constructed; |
| 47 | int m_value; |
| 48 | |
| 49 | int m_id; |
| 50 | |
| 51 | public: |
| 52 | Constructable() : m_constructed(true), m_value(0) { |
| 53 | ++num_constructor_calls; |
| 54 | m_id = num_constructor_calls; |
| 55 | destroyed_mem.erase(this); |
| 56 | } |
| 57 | |
| 58 | Constructable(int val) : m_constructed(true), m_value(val) { |
| 59 | ++num_constructor_calls; |
| 60 | m_id = num_constructor_calls; |
| 61 | destroyed_mem.erase(this); |
| 62 | } |
| 63 | |
| 64 | Constructable(const Constructable& src) : m_constructed(true) { |
| 65 | m_value = src.m_value; |
| 66 | ++num_constructor_calls; |
| 67 | m_id = num_constructor_calls; |
| 68 | EXPECT_TRUE(destroyed_mem.find(&src) == destroyed_mem.end()); |
| 69 | destroyed_mem.erase(this); |
| 70 | } |
| 71 | |
| 72 | Constructable(Constructable&& src) : m_constructed(true) { |
| 73 | m_value = src.m_value; |
| 74 | ++num_constructor_calls; |
| 75 | ++num_move_constructor_calls; |
| 76 | m_id = num_constructor_calls; |
| 77 | EXPECT_TRUE(destroyed_mem.find(&src) == destroyed_mem.end()); |
| 78 | destroyed_mem.erase(this); |
| 79 | } |
| 80 | |
| 81 | ~Constructable() { |
| 82 | EXPECT_TRUE(m_constructed); |
| 83 | ++num_deconstructor_calls; |
| 84 | m_constructed = false; |
| 85 | destroyed_mem.insert(this); |
| 86 | } |
| 87 | |
| 88 | Constructable& operator=(const Constructable& src) { |
| 89 | EXPECT_TRUE(m_constructed); |
| 90 | m_value = src.m_value; |
| 91 | ++num_assignment_calls; |