| 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-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
#ifndef TEST_SUPPORT_COUNTING_PREDICATES_H |
| 10 |
#define TEST_SUPPORT_COUNTING_PREDICATES_H |
| 11 |
|
| 12 |
#include <cstddef> |
| 13 |
#include <utility> |
| 14 |
#include "test_macros.h" |
| 15 |
|
| 16 |
template <typename Predicate, typename Arg> |
| 17 |
struct unary_counting_predicate { |
| 18 |
public: |
| 19 |
typedef Arg argument_type; |
| 20 |
typedef bool result_type; |
| 21 |
|
| 22 |
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 |
~unary_counting_predicate() {} |
| 26 |
|
| 27 |
bool operator () (const Arg &a) const { ++count_; return p_(a); } |
| 28 |
std::size_t count() const { return count_; } |
| 29 |
void reset() { count_ = 0; } |
| 30 |
|
| 31 |
private: |
| 32 |
Predicate p_; |
| 33 |
mutable std::size_t count_; |
| 34 |
}; |
| 35 |
|
| 36 |
|
| 37 |
template <typename Predicate, typename Arg1, typename Arg2=Arg1> |
| 38 |
struct binary_counting_predicate { |
| 39 |
public: |
| 40 |
typedef Arg1 first_argument_type; |
| 41 |
typedef Arg2 second_argument_type; |
| 42 |
typedef bool result_type; |
| 43 |
|
| 44 |
binary_counting_predicate ( Predicate p ) : p_(p), count_(0) {} |
| 45 |
~binary_counting_predicate() {} |
| 46 |
|
| 47 |
bool operator () (const Arg1 &a1, const Arg2 &a2) const { ++count_; return p_(a1, a2); } |
| 48 |
std::size_t count() const { return count_; } |
| 49 |
void reset() { count_ = 0; } |
| 50 |
|
| 51 |
private: |
| 52 |
Predicate p_; |
| 53 |
mutable std::size_t count_; |
| 54 |
}; |
| 55 |
|
| 56 |
#if TEST_STD_VER > 14 |
| 57 |
|
| 58 |
template <class Predicate> |
| 59 |
class counting_predicate { |
| 60 |
Predicate pred_; |
| 61 |
int* count_ = nullptr; |
| 62 |
|
| 63 |
public: |
| 64 |
constexpr counting_predicate() = default; |
| 65 |
constexpr counting_predicate(Predicate pred, int& count) : pred_(std::move(pred)), count_(&count) {} |
| 66 |
|
| 67 |
template <class... Args> |
| 68 |
constexpr decltype(auto) operator()(Args&& ...args) { |
| 69 |
++(*count_); |
| 70 |
return pred_(std::forward<Args>(args)...); |
| 71 |
} |
| 72 |
|
| 73 |
template <class... Args> |
| 74 |
constexpr decltype(auto) operator()(Args&& ...args) const { |
| 75 |
++(*count_); |
| 76 |
return pred_(std::forward<Args>(args)...); |
| 77 |
} |
| 78 |
}; |
| 79 |
|
| 80 |
template <class Predicate> |
| 81 |
counting_predicate(Predicate pred, int& count) -> counting_predicate<Predicate>; |
| 82 |
|
| 83 |
#endif // TEST_STD_VER > 14 |
| 84 |
|
| 85 |
#endif // TEST_SUPPORT_COUNTING_PREDICATES_H |