move_only_int.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_TEST_MOVE_ONLY_INT_H_
  5. #define BASE_TEST_MOVE_ONLY_INT_H_
  6. namespace base {
  7. // A move-only class that holds an integer. This is designed for testing
  8. // containers. See also CopyOnlyInt.
  9. class MoveOnlyInt {
  10. public:
  11. explicit MoveOnlyInt(int data = 1) : data_(data) {}
  12. MoveOnlyInt(MoveOnlyInt&& other) : data_(other.data_) { other.data_ = 0; }
  13. MoveOnlyInt(const MoveOnlyInt&) = delete;
  14. MoveOnlyInt& operator=(const MoveOnlyInt&) = delete;
  15. ~MoveOnlyInt() { data_ = 0; }
  16. MoveOnlyInt& operator=(MoveOnlyInt&& other) {
  17. data_ = other.data_;
  18. other.data_ = 0;
  19. return *this;
  20. }
  21. friend bool operator==(const MoveOnlyInt& lhs, const MoveOnlyInt& rhs) {
  22. return lhs.data_ == rhs.data_;
  23. }
  24. friend bool operator!=(const MoveOnlyInt& lhs, const MoveOnlyInt& rhs) {
  25. return !operator==(lhs, rhs);
  26. }
  27. friend bool operator<(const MoveOnlyInt& lhs, int rhs) {
  28. return lhs.data_ < rhs;
  29. }
  30. friend bool operator<(int lhs, const MoveOnlyInt& rhs) {
  31. return lhs < rhs.data_;
  32. }
  33. friend bool operator<(const MoveOnlyInt& lhs, const MoveOnlyInt& rhs) {
  34. return lhs.data_ < rhs.data_;
  35. }
  36. friend bool operator>(const MoveOnlyInt& lhs, const MoveOnlyInt& rhs) {
  37. return rhs < lhs;
  38. }
  39. friend bool operator<=(const MoveOnlyInt& lhs, const MoveOnlyInt& rhs) {
  40. return !(rhs < lhs);
  41. }
  42. friend bool operator>=(const MoveOnlyInt& lhs, const MoveOnlyInt& rhs) {
  43. return !(lhs < rhs);
  44. }
  45. int data() const { return data_; }
  46. private:
  47. volatile int data_;
  48. };
  49. } // namespace base
  50. #endif // BASE_TEST_MOVE_ONLY_INT_H_