Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
any_of.hpp
Go to the documentation of this file.
1// Copyright 2024, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ALGORITHM_ANY_OF_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ALGORITHM_ANY_OF_HPP_
7
8// IWYU pragma: private, include "arene/base/algorithm.hpp"
9// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
10
11// parasoft-begin-suppress AUTOSAR-A7_1_5-a-2 "Trailing return syntax permitted by A7-1-5 Permit #1 v1.0.0"
12
13// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
14#include "arene/base/algorithm/detail/convert_to.hpp"
15#include "arene/base/algorithm/detail/traits.hpp"
16#include "arene/base/compiler_support/cpp14_inline.hpp"
17#include "arene/base/constraints/constraints.hpp"
18#include "arene/base/functional/identity.hpp"
19#include "arene/base/stdlib_choice/declval.hpp"
20#include "arene/base/stdlib_choice/enable_if.hpp"
21#include "arene/base/stdlib_choice/initializer_list.hpp"
22#include "arene/base/stdlib_choice/is_convertible.hpp"
23#include "arene/base/stdlib_choice/iterator_traits.hpp"
24#include "arene/base/stdlib_choice/move.hpp"
25#include "arene/base/type_traits/all_are_same.hpp"
26#include "arene/base/type_traits/is_invocable.hpp"
27#include "arene/base/type_traits/is_predicate.hpp"
28#include "arene/base/type_traits/iterator_category_traits.hpp"
29#include "arene/base/utility/make_subrange.hpp"
30// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
31
32namespace arene {
33namespace base {
34
35namespace any_of_detail {
36
37/// @brief function object implementing 'any_of'
38class any_of_fn //
39{
40 public:
41 /// @brief Check if any of the elements in a range are true
42 /// @tparam Iterator the type of the iterator
43 /// @tparam UnaryPredicate the type of the predicate
44 /// @tparam Projection projection to apply to element in the range
45 /// @param begin The start of the range
46 /// @param end The end of the range
47 /// @param pred The predicate to apply to each element of the range
48 /// @param proj Projection to apply to elements before checking the predicate
49 /// @return bool @c true if @c pred returns true for any of the projected
50 /// elements in the range, @c false otherwise.
51 template <
52 typename Iterator,
53 typename UnaryPredicate,
54 typename Projection = identity,
55 constraints<
56 std::enable_if_t<base::is_input_iterator_v<Iterator>>,
57 std::enable_if_t<is_predicate_v<
58 UnaryPredicate&,
59 invoke_result_t<Projection&, algorithm_detail::iter_reference_t<Iterator>>>>> = nullptr>
60 constexpr auto operator()(Iterator begin, Iterator end, UnaryPredicate pred, Projection proj = {}) const noexcept( //
61 noexcept(std::declval<Iterator&>() != std::declval<Iterator&>()) && //
62 noexcept(pred(proj(*++std::declval<Iterator&>()))) && //
63 noexcept(make_subrange(std::declval<Iterator&&>(), std::declval<Iterator&&>())) //
64 ) //
65 -> bool //
66 {
67 // This implements a 'constexpr' version of 'any_of'
68 // NOLINTNEXTLINE(readability-use-anyofallof)
69 for (auto const& element : arene::base::make_subrange(std::move(begin), std::move(end))) {
70 if (pred(proj(element))) {
71 return true;
72 }
73 }
74 return false;
75 }
76
77 /// @brief Check if any of the elements in a range are true
78 /// @tparam Iterator the type of the iterator
79 /// @param begin The start of the range
80 /// @param end The end of the range
81 /// @return bool @c true if any of the elements in the range are @c true, @c false
82 /// otherwise.
83 ///
84 /// This overload casts the elements to @c bool.
85 ///
86 template <
87 typename Iterator,
88 constraints<
89 std::enable_if_t<base::is_input_iterator_v<Iterator>>,
90 std::enable_if_t<std::is_convertible<algorithm_detail::iter_reference_t<Iterator>, bool>::value>> = nullptr>
91 constexpr auto operator()(Iterator begin, Iterator end) const noexcept( //
92 is_nothrow_invocable_v< //
93 any_of_fn const&, //
94 Iterator&&, //
95 Iterator&&, //
96 algorithm_detail::convert_to<bool>> //
97 ) //
98 -> bool { // CODEQLFP(DCL51-CPP)
99 return (*this)( //
100 std::move(begin),
101 std::move(end),
102 algorithm_detail::convert_to<bool>{}
103 );
104 }
105
106 /// @brief Check if any the values in the supplied list are true.
107 /// @param values A list of values
108 /// @return bool @c true if any of the values are @c true, @c false othewise
109 constexpr auto operator()(std::initializer_list<bool> const values) const noexcept -> bool { // CODEQLFP(DCL51-CPP)
110 return (*this)(values.begin(), values.end());
111 }
112
113 /// @brief Check if any of the supplied arguments are true.
114 /// @tparam Args The types of the arguments
115 /// @param args the arguments
116 /// @pre All the arguments must be bool.
117 /// @return bool @c true if any of the elements are @c true, @c false otherwise
118 template <typename... Args, constraints<std::enable_if_t<base::all_are_same_v<bool, Args...>>> = nullptr>
119 constexpr auto operator()(Args... args) const noexcept -> bool { // CODEQLFP(DCL51-CPP) CODEQLFP(A7-1-1)
120 return (*this)({args...}); // CODEQLFP(M8-5-2)
121 }
122};
123} // namespace any_of_detail
124
125/// @def arene::base::any_of
126/// @brief checks if a predicate is true for any of the elements in a range
127/// @copydoc arene::base::any_of_detail::any_of_fn::operator()
128// parasoft-begin-suppress AUTOSAR-M7_3_3-a "An unnamed namespace is used to create a per-TU reference to a global
129// object used in multiple TUs."
130// parasoft-begin-suppress CERT_CPP-DCL59-a "An unnamed namespace is used to create a per-TU reference to a global
131// object used in multiple TUs."
132ARENE_CPP14_INLINE_VARIABLE(any_of_detail::any_of_fn, any_of);
133// parasoft-end-suppress AUTOSAR-M7_3_3-a
134// parasoft-end-suppress CERT_CPP-DCL59-a
135
136} // namespace base
137} // namespace arene
138
139#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ALGORITHM_ANY_OF_HPP_
Definition array_exceptions_disabled.cpp:11
ARENE_CPP14_INLINE_VARIABLE(any_of_detail::any_of_fn, any_of)
Copyright 2026, Toyota Motor Corporation.
Definition array_exceptions_disabled.cpp:10