xref: /aosp_15_r20/external/webrtc/rtc_base/containers/move_only_int.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2021 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 // This implementation is borrowed from Chromium.
12 
13 #ifndef RTC_BASE_CONTAINERS_MOVE_ONLY_INT_H_
14 #define RTC_BASE_CONTAINERS_MOVE_ONLY_INT_H_
15 
16 namespace webrtc {
17 
18 // A move-only class that holds an integer. This is designed for testing
19 // containers. See also CopyOnlyInt.
20 class MoveOnlyInt {
21  public:
data_(data)22   explicit MoveOnlyInt(int data = 1) : data_(data) {}
23   MoveOnlyInt(const MoveOnlyInt& other) = delete;
24   MoveOnlyInt& operator=(const MoveOnlyInt& other) = delete;
MoveOnlyInt(MoveOnlyInt && other)25   MoveOnlyInt(MoveOnlyInt&& other) : data_(other.data_) { other.data_ = 0; }
~MoveOnlyInt()26   ~MoveOnlyInt() { data_ = 0; }
27 
28   MoveOnlyInt& operator=(MoveOnlyInt&& other) {
29     data_ = other.data_;
30     other.data_ = 0;
31     return *this;
32   }
33 
34   friend bool operator==(const MoveOnlyInt& lhs, const MoveOnlyInt& rhs) {
35     return lhs.data_ == rhs.data_;
36   }
37 
38   friend bool operator!=(const MoveOnlyInt& lhs, const MoveOnlyInt& rhs) {
39     return !operator==(lhs, rhs);
40   }
41 
42   friend bool operator<(const MoveOnlyInt& lhs, int rhs) {
43     return lhs.data_ < rhs;
44   }
45 
46   friend bool operator<(int lhs, const MoveOnlyInt& rhs) {
47     return lhs < rhs.data_;
48   }
49 
50   friend bool operator<(const MoveOnlyInt& lhs, const MoveOnlyInt& rhs) {
51     return lhs.data_ < rhs.data_;
52   }
53 
54   friend bool operator>(const MoveOnlyInt& lhs, const MoveOnlyInt& rhs) {
55     return rhs < lhs;
56   }
57 
58   friend bool operator<=(const MoveOnlyInt& lhs, const MoveOnlyInt& rhs) {
59     return !(rhs < lhs);
60   }
61 
62   friend bool operator>=(const MoveOnlyInt& lhs, const MoveOnlyInt& rhs) {
63     return !(lhs < rhs);
64   }
65 
data()66   int data() const { return data_; }
67 
68  private:
69   volatile int data_;
70 };
71 
72 }  // namespace webrtc
73 
74 #endif  // RTC_BASE_CONTAINERS_MOVE_ONLY_INT_H_
75