86 lines · c
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-exception7//8//===----------------------------------------------------------------------===//9 10#ifndef LIBCPP_TEST_STD_UTILITIES_MEMORY_SPECIALIZED_ALGORITHMS_COUNTED_H11#define LIBCPP_TEST_STD_UTILITIES_MEMORY_SPECIALIZED_ALGORITHMS_COUNTED_H12 13#include "test_macros.h"14 15struct 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 25 explicit Counted() {26 check_throw();27 increase_counters();28 }29 30 explicit Counted(int v) : value(v) {31 check_throw();32 increase_counters();33 }34 35 ~Counted() { --current_objects; }36 37 static void reset() {38 current_objects = total_objects = 0;39 total_copies = total_moves = 0;40 throw_on = -1;41 }42 43 Counted(const Counted& rhs) : value(rhs.value) {44 check_throw();45 increase_counters();46 ++total_copies;47 }48 49 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 67private:68 void check_throw() {69 if (throw_on == total_objects) {70 TEST_THROW(1);71 }72 }73 74 void increase_counters() {75 ++current_objects;76 ++total_objects;77 }78};79int Counted::current_objects = 0;80int Counted::total_objects = 0;81int Counted::total_copies = 0;82int Counted::total_moves = 0;83int Counted::throw_on = -1;84 85#endif // LIBCPP_TEST_STD_UTILITIES_MEMORY_SPECIALIZED_ALGORITHMS_COUNTED_H86