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