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. #include "base/macros.h"
  7. namespace base {
  8. // A move-only class that holds an integer. This is designed for testing
  9. // containers. See also CopyOnlyInt.
  10. class MoveOnlyInt {
  11. public:
  12. explicit MoveOnlyInt(int data = 1) : data_(data) {}
  13. MoveOnlyInt(MoveOnlyInt&& other) : data_(other.data_) { other.data_ = 0; }
  14. ~MoveOnlyInt() { data_ = 0; }
  15. MoveOnlyInt& operator=(MoveOnlyInt&& other) {
  16. data_ = other.data_;
  17. other.data_ = 0;
  18. return *this;
  19. }
  20. friend bool operator==(const MoveOnlyInt& lhs, const MoveOnlyInt& rhs) {
  21. return lhs.data_ == rhs.data_;
  22. }
  23. friend bool operator!=(const MoveOnlyInt& lhs, const MoveOnlyInt& rhs) {
  24. return !operator==(lhs, rhs);
  25. }
  26. friend bool operator<(const MoveOnlyInt& lhs, int rhs) {
  27. return lhs.data_ < rhs;
  28. }
  29. friend bool operator<(int lhs, const MoveOnlyInt& rhs) {
  30. return lhs < rhs.data_;
  31. }
  32. friend bool operator<(const MoveOnlyInt& lhs, const MoveOnlyInt& rhs) {
  33. return lhs.data_ < rhs.data_;
  34. }
  35. friend bool operator>(const MoveOnlyInt& lhs, const MoveOnlyInt& rhs) {
  36. return rhs < lhs;
  37. }
  38. friend bool operator<=(const MoveOnlyInt& lhs, const MoveOnlyInt& rhs) {
  39. return !(rhs < lhs);
  40. }
  41. friend bool operator>=(const MoveOnlyInt& lhs, const MoveOnlyInt& rhs) {
  42. return !(lhs < rhs);
  43. }
  44. int data() const { return data_; }
  45. private:
  46. volatile int data_;
  47. DISALLOW_COPY_AND_ASSIGN(MoveOnlyInt);
  48. };
  49. } // namespace base
  50. #endif // BASE_TEST_MOVE_ONLY_INT_H_