89 lines · c
1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef TEST_SUPPORT_COUNTING_PREDICATES_H10#define TEST_SUPPORT_COUNTING_PREDICATES_H11 12#include <cstddef>13#include <utility>14#include "test_macros.h"15 16template <typename Predicate, typename Arg>17struct unary_counting_predicate {18public:19 typedef Arg argument_type;20 typedef bool result_type;21 22 TEST_CONSTEXPR_CXX20 unary_counting_predicate(Predicate p) : p_(p), count_(0) {}23 unary_counting_predicate(const unary_counting_predicate&) = default;24 unary_counting_predicate& operator=(const unary_counting_predicate&) = default;25 TEST_CONSTEXPR_CXX20 ~unary_counting_predicate() {}26 27 TEST_CONSTEXPR_CXX14 bool operator()(const Arg& a) const {28 ++count_;29 return p_(a);30 }31 TEST_CONSTEXPR std::size_t count() const { return count_; }32 TEST_CONSTEXPR_CXX14 void reset() { count_ = 0; }33 34private:35 Predicate p_;36 mutable std::size_t count_;37};38 39template <typename Predicate, typename Arg1, typename Arg2 = Arg1>40struct binary_counting_predicate {41public:42 typedef Arg1 first_argument_type;43 typedef Arg2 second_argument_type;44 typedef bool result_type;45 46 TEST_CONSTEXPR binary_counting_predicate(Predicate p) : p_(p), count_(0) {}47 TEST_CONSTEXPR_CXX14 bool operator()(const Arg1& a1, const Arg2& a2) const {48 ++count_;49 return p_(a1, a2);50 }51 TEST_CONSTEXPR std::size_t count() const { return count_; }52 TEST_CONSTEXPR_CXX14 void reset() { count_ = 0; }53 54private:55 Predicate p_;56 mutable std::size_t count_;57};58 59#if TEST_STD_VER > 1460 61template <class Predicate>62class counting_predicate {63 Predicate pred_;64 int* count_ = nullptr;65 66public:67 constexpr counting_predicate() = default;68 constexpr counting_predicate(Predicate pred, int& count) : pred_(std::move(pred)), count_(&count) {}69 70 template <class... Args>71 constexpr decltype(auto) operator()(Args&&... args) {72 ++(*count_);73 return pred_(std::forward<Args>(args)...);74 }75 76 template <class... Args>77 constexpr decltype(auto) operator()(Args&&... args) const {78 ++(*count_);79 return pred_(std::forward<Args>(args)...);80 }81};82 83template <class Predicate>84counting_predicate(Predicate pred, int& count) -> counting_predicate<Predicate>;85 86#endif // TEST_STD_VER > 1487 88#endif // TEST_SUPPORT_COUNTING_PREDICATES_H89