1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef TEST_SUPPORT_COUNTING_PREDICATES_H
10 #define TEST_SUPPORT_COUNTING_PREDICATES_H
14 #include "test_macros.h"
16 template <typename Predicate
, typename Arg
>
17 struct unary_counting_predicate
{
19 typedef Arg argument_type
;
20 typedef bool result_type
;
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() {}
27 bool operator () (const Arg
&a
) const { ++count_
; return p_(a
); }
28 std::size_t count() const { return count_
; }
29 void reset() { count_
= 0; }
33 mutable std::size_t count_
;
37 template <typename Predicate
, typename Arg1
, typename Arg2
=Arg1
>
38 struct binary_counting_predicate
{
40 typedef Arg1 first_argument_type
;
41 typedef Arg2 second_argument_type
;
42 typedef bool result_type
;
44 binary_counting_predicate ( Predicate p
) : p_(p
), count_(0) {}
45 ~binary_counting_predicate() {}
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; }
53 mutable std::size_t count_
;
58 template <class Predicate
>
59 class counting_predicate
{
61 int* count_
= nullptr;
64 constexpr counting_predicate() = default;
65 constexpr counting_predicate(Predicate pred
, int& count
) : pred_(std::move(pred
)), count_(&count
) {}
67 template <class... Args
>
68 constexpr decltype(auto) operator()(Args
&& ...args
) {
70 return pred_(std::forward
<Args
>(args
)...);
73 template <class... Args
>
74 constexpr decltype(auto) operator()(Args
&& ...args
) const {
76 return pred_(std::forward
<Args
>(args
)...);
80 template <class Predicate
>
81 counting_predicate(Predicate pred
, int& count
) -> counting_predicate
<Predicate
>;
83 #endif // TEST_STD_VER > 14
85 #endif // TEST_SUPPORT_COUNTING_PREDICATES_H