tracked_value.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. #include <cassert>
  11. #include "test_macros.h"
  12. struct TrackedValue {
  13. enum State { CONSTRUCTED, MOVED_FROM, DESTROYED };
  14. State state;
  15. TrackedValue() : state(State::CONSTRUCTED) {}
  16. TrackedValue(TrackedValue const& t) : state(State::CONSTRUCTED) {
  17. assert(t.state != State::MOVED_FROM && "copying a moved-from object");
  18. assert(t.state != State::DESTROYED && "copying a destroyed object");
  19. }
  20. #if TEST_STD_VER >= 11
  21. TrackedValue(TrackedValue&& t) : state(State::CONSTRUCTED) {
  22. assert(t.state != State::MOVED_FROM && "double moving from an object");
  23. assert(t.state != State::DESTROYED && "moving from a destroyed object");
  24. t.state = State::MOVED_FROM;
  25. }
  26. #endif
  27. TrackedValue& operator=(TrackedValue const& t) {
  28. assert(state != State::DESTROYED && "copy assigning into destroyed object");
  29. assert(t.state != State::MOVED_FROM && "copying a moved-from object");
  30. assert(t.state != State::DESTROYED && "copying a destroyed object");
  31. state = t.state;
  32. return *this;
  33. }
  34. #if TEST_STD_VER >= 11
  35. TrackedValue& operator=(TrackedValue&& t) {
  36. assert(state != State::DESTROYED && "move assigning into destroyed object");
  37. assert(t.state != State::MOVED_FROM && "double moving from an object");
  38. assert(t.state != State::DESTROYED && "moving from a destroyed object");
  39. state = t.state;
  40. t.state = State::MOVED_FROM;
  41. return *this;
  42. }
  43. #endif
  44. ~TrackedValue() {
  45. assert(state != State::DESTROYED && "double-destroying an object");
  46. state = State::DESTROYED;
  47. }
  48. };
  49. #endif // SUPPORT_TRACKED_VALUE_H