Eric Fiselier | 38236b5 | 2016-01-19 21:52:04 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
Chandler Carruth | 57b08b0 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 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 |
Eric Fiselier | 38236b5 | 2016-01-19 21:52:04 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
Eric Fiselier | 66e41b6 | 2015-03-09 18:02:16 +0000 | [diff] [blame] | 8 | #ifndef SUPPORT_TRACKED_VALUE_H |
| 9 | #define SUPPORT_TRACKED_VALUE_H |
| 10 | |
| 11 | #include <cassert> |
| 12 | |
Eric Fiselier | 9adebed | 2017-04-19 01:02:49 +0000 | [diff] [blame] | 13 | #include "test_macros.h" |
| 14 | |
Eric Fiselier | 66e41b6 | 2015-03-09 18:02:16 +0000 | [diff] [blame] | 15 | struct TrackedValue { |
| 16 | enum State { CONSTRUCTED, MOVED_FROM, DESTROYED }; |
| 17 | State state; |
| 18 | |
| 19 | TrackedValue() : state(State::CONSTRUCTED) {} |
| 20 | |
| 21 | 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 | |
Eric Fiselier | 9adebed | 2017-04-19 01:02:49 +0000 | [diff] [blame] | 26 | #if TEST_STD_VER >= 11 |
Eric Fiselier | 66e41b6 | 2015-03-09 18:02:16 +0000 | [diff] [blame] | 27 | 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 | |
Eric Fiselier | 9adebed | 2017-04-19 01:02:49 +0000 | [diff] [blame] | 42 | #if TEST_STD_VER >= 11 |
Eric Fiselier | 66e41b6 | 2015-03-09 18:02:16 +0000 | [diff] [blame] | 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 | |
| 53 | ~TrackedValue() { |
| 54 | assert(state != State::DESTROYED && "double-destroying an object"); |
| 55 | state = State::DESTROYED; |
| 56 | } |
| 57 | }; |
| 58 | |
| 59 | #endif // SUPPORT_TRACKED_VALUE_H |