1 // -*- C++ -*- 2 //===----------------------------------------------------------------------===// 3 // 4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 // See https://llvm.org/LICENSE.txt for license information. 6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef LIBCPP_TEST_STD_UTILITIES_MEMORY_SPECIALIZED_ALGORITHMS_COUNTED_H 11 #define LIBCPP_TEST_STD_UTILITIES_MEMORY_SPECIALIZED_ALGORITHMS_COUNTED_H 12 13 #include "test_macros.h" 14 15 struct Counted { 16 static int current_objects; 17 static int total_objects; 18 static int total_copies; 19 static int total_moves; 20 static int throw_on; 21 22 int value; 23 bool moved_from = false; 24 CountedCounted25 explicit Counted() { 26 check_throw(); 27 increase_counters(); 28 } 29 CountedCounted30 explicit Counted(int v) : value(v) { 31 check_throw(); 32 increase_counters(); 33 } 34 ~CountedCounted35 ~Counted() { --current_objects; } 36 resetCounted37 static void reset() { 38 current_objects = total_objects = 0; 39 total_copies = total_moves = 0; 40 throw_on = -1; 41 } 42 CountedCounted43 Counted(const Counted& rhs) : value(rhs.value) { 44 check_throw(); 45 increase_counters(); 46 ++total_copies; 47 } 48 CountedCounted49 Counted(Counted&& rhs) : value(rhs.value) { 50 check_throw(); 51 increase_counters(); 52 53 rhs.moved_from = true; 54 ++total_moves; 55 } 56 57 friend bool operator==(const Counted& l, const Counted& r) { 58 return l.value == r.value; 59 } 60 61 friend bool operator!=(const Counted& l, const Counted& r) { 62 return !(l == r); 63 } 64 65 friend void operator&(Counted) = delete; 66 67 private: check_throwCounted68 void check_throw() { 69 if (throw_on == total_objects) { 70 TEST_THROW(1); 71 } 72 } 73 increase_countersCounted74 void increase_counters() { 75 ++current_objects; 76 ++total_objects; 77 } 78 }; 79 int Counted::current_objects = 0; 80 int Counted::total_objects = 0; 81 int Counted::total_copies = 0; 82 int Counted::total_moves = 0; 83 int Counted::throw_on = -1; 84 85 #endif // LIBCPP_TEST_STD_UTILITIES_MEMORY_SPECIALIZED_ALGORITHMS_COUNTED_H 86