xref: /aosp_15_r20/external/cronet/base/test/copy_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_COPY_ONLY_INT_H_
6 #define BASE_TEST_COPY_ONLY_INT_H_
7 
8 
9 namespace base {
10 
11 // A copy-only (not moveable) class that holds an integer. This is designed for
12 // testing containers. See also MoveOnlyInt.
13 class CopyOnlyInt {
14  public:
data_(data)15   explicit CopyOnlyInt(int data = 1) : data_(data) {}
CopyOnlyInt(const CopyOnlyInt & other)16   CopyOnlyInt(const CopyOnlyInt& other) : data_(other.data_) { ++num_copies_; }
~CopyOnlyInt()17   ~CopyOnlyInt() { data_ = 0; }
18 
19   friend bool operator==(const CopyOnlyInt& lhs, const CopyOnlyInt& rhs) {
20     return lhs.data_ == rhs.data_;
21   }
22 
23   friend bool operator!=(const CopyOnlyInt& lhs, const CopyOnlyInt& rhs) {
24     return !operator==(lhs, rhs);
25   }
26 
27   friend bool operator<(const CopyOnlyInt& lhs, const CopyOnlyInt& rhs) {
28     return lhs.data_ < rhs.data_;
29   }
30 
31   friend bool operator>(const CopyOnlyInt& lhs, const CopyOnlyInt& rhs) {
32     return rhs < lhs;
33   }
34 
35   friend bool operator<=(const CopyOnlyInt& lhs, const CopyOnlyInt& rhs) {
36     return !(rhs < lhs);
37   }
38 
39   friend bool operator>=(const CopyOnlyInt& lhs, const CopyOnlyInt& rhs) {
40     return !(lhs < rhs);
41   }
42 
data()43   int data() const { return data_; }
44 
reset_num_copies()45   static void reset_num_copies() { num_copies_ = 0; }
46 
num_copies()47   static int num_copies() { return num_copies_; }
48 
49  private:
50   volatile int data_;
51 
52   static int num_copies_;
53 
54   CopyOnlyInt(CopyOnlyInt&&) = delete;
55   CopyOnlyInt& operator=(CopyOnlyInt&) = delete;
56 };
57 
58 }  // namespace base
59 
60 #endif  // BASE_TEST_COPY_ONLY_INT_H_
61