1 // Copyright 2023 The Pigweed Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 4 // use this file except in compliance with the License. You may obtain a copy of 5 // the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations under 13 // the License. 14 #pragma once 15 16 #include <cstddef> 17 18 namespace pw::containers::test { 19 20 struct CopyOnly { CopyOnlyCopyOnly21 explicit CopyOnly(int val) : value(val) {} 22 CopyOnlyCopyOnly23 CopyOnly(const CopyOnly& other) { value = other.value; } 24 25 CopyOnly& operator=(const CopyOnly& other) { 26 value = other.value; 27 return *this; 28 } 29 30 CopyOnly(CopyOnly&&) = delete; 31 32 int value; 33 }; 34 35 struct MoveOnly { MoveOnlyMoveOnly36 explicit MoveOnly(int val) : value(val) {} 37 38 MoveOnly(const MoveOnly&) = delete; 39 MoveOnlyMoveOnly40 MoveOnly(MoveOnly&& other) { 41 value = other.value; 42 other.value = kDeleted; 43 } 44 45 static constexpr int kDeleted = -1138; 46 47 int value; 48 }; 49 50 // Counter objects CANNOT be globally scoped. 51 struct Counter { 52 static int created; 53 static int destroyed; 54 static int moved; 55 ResetCounter56 static void Reset() { created = destroyed = moved = 0; } 57 valueCounter58 Counter(int val = 0) : value(val), set_to_this_when_constructed_(this) { 59 objects_.Constructed(); 60 created += 1; 61 } 62 CounterCounter63 Counter(const Counter& other) : Counter(other.value) {} 64 CounterCounter65 Counter(Counter&& other) 66 : value(other.value), set_to_this_when_constructed_(this) { 67 objects_.Constructed(); 68 other.value = 0; 69 moved += 1; 70 } 71 72 Counter& operator=(const Counter& other); 73 Counter& operator=(Counter&& other); 74 75 ~Counter(); 76 77 int value; 78 79 private: 80 class ObjectCounter { 81 public: ObjectCounterCounter82 constexpr ObjectCounter() : count_(0) {} 83 84 ~ObjectCounter(); 85 ConstructedCounter86 void Constructed() { count_ += 1; } 87 void Destructed(); 88 89 private: 90 size_t count_; 91 }; 92 93 static ObjectCounter objects_; 94 95 Counter* set_to_this_when_constructed_; 96 }; 97 98 } // namespace pw::containers::test 99