Note: Not thread safe!
| 150 | |
| 151 | // Note: Not thread safe! |
| 152 | struct Foo |
| 153 | { |
| 154 | static int& nextId() { static int i; return i; } |
| 155 | static int& createCount() { static int c; return c; } |
| 156 | static int& destroyCount() { static int c; return c; } |
| 157 | static bool& destroyedInOrder() { static bool d = true; return d; } |
| 158 | static void reset() { createCount() = 0; destroyCount() = 0; nextId() = 0; destroyedInOrder() = true; lastDestroyedId() = -1; } |
| 159 | |
| 160 | Foo() { id = nextId()++; ++createCount(); } |
| 161 | Foo(Foo const&) MOODYCAMEL_DELETE_FUNCTION; |
| 162 | Foo(Foo&& other) { id = other.id; other.id = -1; } |
| 163 | void operator=(Foo&& other) { id = other.id; other.id = -1; } |
| 164 | ~Foo() |
| 165 | { |
| 166 | ++destroyCount(); |
| 167 | if (id == -2) { |
| 168 | // Double free! |
| 169 | destroyedInOrder() = false; |
| 170 | } |
| 171 | else if (id != -1) { |
| 172 | if (id <= lastDestroyedId()) { |
| 173 | destroyedInOrder() = false; |
| 174 | } |
| 175 | lastDestroyedId() = id; |
| 176 | } |
| 177 | id = -2; |
| 178 | } |
| 179 | |
| 180 | private: |
| 181 | int id; |
| 182 | static int& lastDestroyedId() { static int i = -1; return i; } |
| 183 | }; |
| 184 | |
| 185 | struct Copyable { |
| 186 | Copyable(int id) : copied(false), id(id) { } |
no outgoing calls