blob: 01b8c840d19bb99119ee18dfca72e237d94ee480 [file] [log] [blame]
Eric Fiselier38236b52016-01-19 21:52:04 +00001//===----------------------------------------------------------------------===//
2//
Chandler Carruth57b08b02019-01-19 10:56:40 +00003// 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 Fiselier38236b52016-01-19 21:52:04 +00006//
7//===----------------------------------------------------------------------===//
Eric Fiselier66e41b62015-03-09 18:02:16 +00008#ifndef SUPPORT_TRACKED_VALUE_H
9#define SUPPORT_TRACKED_VALUE_H
10
11#include <cassert>
12
Eric Fiselier9adebed2017-04-19 01:02:49 +000013#include "test_macros.h"
14
Eric Fiselier66e41b62015-03-09 18:02:16 +000015struct 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 Fiselier9adebed2017-04-19 01:02:49 +000026#if TEST_STD_VER >= 11
Eric Fiselier66e41b62015-03-09 18:02:16 +000027 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 Fiselier9adebed2017-04-19 01:02:49 +000042#if TEST_STD_VER >= 11
Eric Fiselier66e41b62015-03-09 18:02:16 +000043 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