1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 #ifndef SUPPORT_TRACKED_VALUE_H
9 #define SUPPORT_TRACKED_VALUE_H
10 
11 #include <cassert>
12 
13 #include "test_macros.h"
14 
15 struct TrackedValue {
16     enum State { CONSTRUCTED, MOVED_FROM, DESTROYED };
17     State state;
18 
TrackedValueTrackedValue19     TrackedValue() : state(State::CONSTRUCTED) {}
20 
TrackedValueTrackedValue21     TrackedValue(TrackedValue const& t) : state(State::CONSTRUCTED) {
22         assert(t.state != State::MOVED_FROM && "copying a moved-from object");
23         assert(t.state != State::DESTROYED  && "copying a destroyed object");
24     }
25 
26 #if TEST_STD_VER >= 11
TrackedValueTrackedValue27     TrackedValue(TrackedValue&& t) : state(State::CONSTRUCTED) {
28         assert(t.state != State::MOVED_FROM && "double moving from an object");
29         assert(t.state != State::DESTROYED  && "moving from a destroyed object");
30         t.state = State::MOVED_FROM;
31     }
32 #endif
33 
34     TrackedValue& operator=(TrackedValue const& t) {
35         assert(state != State::DESTROYED && "copy assigning into destroyed object");
36         assert(t.state != State::MOVED_FROM && "copying a moved-from object");
37         assert(t.state != State::DESTROYED  && "copying a destroyed object");
38         state = t.state;
39         return *this;
40     }
41 
42 #if TEST_STD_VER >= 11
43     TrackedValue& operator=(TrackedValue&& t) {
44         assert(state != State::DESTROYED && "move assigning into destroyed object");
45         assert(t.state != State::MOVED_FROM && "double moving from an object");
46         assert(t.state != State::DESTROYED  && "moving from a destroyed object");
47         state = t.state;
48         t.state = State::MOVED_FROM;
49         return *this;
50     }
51 #endif
52 
~TrackedValueTrackedValue53     ~TrackedValue() {
54         assert(state != State::DESTROYED && "double-destroying an object");
55         state = State::DESTROYED;
56     }
57 };
58 
59 #endif // SUPPORT_TRACKED_VALUE_H
60