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