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/synthesize_range1_overload.hpp" // IWYU pragma: export
15#include "arene/base/algorithm/detail/traits.hpp"
20#include "arene/base/stdlib_choice/declval.hpp"
21#include "arene/base/stdlib_choice/enable_if.hpp"
22#include "arene/base/stdlib_choice/initializer_list.hpp"
23#include "arene/base/stdlib_choice/is_convertible.hpp"
24#include "arene/base/stdlib_choice/iterator_traits.hpp"
25#include "arene/base/stdlib_choice/move.hpp"
31// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
32
33namespace arene {
34namespace base {
35
36namespace any_of_detail {
37
38/// @brief function object implementing 'any_of'
39class any_of_fn //
40{
41 public:
42 /// @brief Check if any of the elements in a range are true
43 /// @tparam Iterator the type of the iterator
44 /// @tparam UnaryPredicate the type of the predicate
45 /// @tparam Projection projection to apply to element in the range
46 /// @param begin The start of the range
47 /// @param end The end of the range
48 /// @param pred The predicate to apply to each element of the range
49 /// @param proj Projection to apply to elements before checking the predicate
50 /// @return bool @c true if @c pred returns true for any of the projected
51 /// elements in the range, @c false otherwise.
52 // parasoft-begin-suppress AUTOSAR-A2_10_1-a "False positive: identifiers 'begin' and 'end' do not hide anything"
53 template <
54 typename Iterator,
55 typename UnaryPredicate,
56 typename Projection = identity,
60 UnaryPredicate&,
62 constexpr auto operator()(Iterator begin, Iterator end, UnaryPredicate pred, Projection proj = {}) const noexcept( //
64 noexcept(pred(proj(*++std::declval<Iterator&>()))) && //
66 ) //
67 -> bool //
68 {
69 // This implements a 'constexpr' version of 'any_of'
70 // NOLINTNEXTLINE(readability-use-anyofallof)
71 for (auto const& element : arene::base::make_subrange(std::move(begin), std::move(end))) {
72 if (pred(proj(element))) {
73 return true;
74 }
75 }
76 return false;
77 }
78 // parasoft-end-suppress AUTOSAR-A2_10_1-a
79
80 /// @brief Check if any of the elements in a range are true
81 /// @tparam Iterator the type of the iterator
82 /// @param begin The start of the range
83 /// @param end The end of the range
84 /// @return bool @c true if any of the elements in the range are @c true, @c false
85 /// otherwise.
86 ///
87 /// This overload casts the elements to @c bool.
88 ///
89 // parasoft-begin-suppress AUTOSAR-A2_10_1-a "False positive: identifiers 'begin' and 'end' do not hide anything"
90 template <
91 typename Iterator,
95 constexpr auto operator()(Iterator begin, Iterator end) const noexcept( //
97 any_of_fn const&, //
98 Iterator&&, //
99 Iterator&&, //
100 decltype(construct<bool>)> //
101 ) //
102 -> bool { // CODEQLFP(DCL51-CPP)
103 return (*this)( //
104 std::move(begin),
105 std::move(end),
106 construct<bool>
107 );
108 }
109 // parasoft-end-suppress AUTOSAR-A2_10_1-a
110
111 /// @brief Check if any the values in the supplied list are true.
112 /// @param values A list of values
113 /// @return bool @c true if any of the values are @c true, @c false othewise
114 constexpr auto operator()(std::initializer_list<bool> const values) const noexcept -> bool { // CODEQLFP(DCL51-CPP)
115 return (*this)(values.begin(), values.end());
116 }
117
118 /// @brief Check if any of the supplied arguments are true.
119 /// @tparam Args The types of the arguments
120 /// @param args the arguments
121 /// @pre All the arguments must be bool.
122 /// @return bool @c true if any of the elements are @c true, @c false otherwise
123 template <typename... Args, constraints<std::enable_if_t<base::all_are_same_v<bool, Args...>>> = nullptr>
124 constexpr auto operator()(Args... args) const noexcept -> bool { // CODEQLFP(DCL51-CPP) CODEQLFP(A7-1-1)
125 return (*this)({args...}); // CODEQLFP(M8-5-2)
126 }
127};
128} // namespace any_of_detail
129
130/// @brief checks if a predicate is true for any of the elements in a range
131/// Returns @c true if a predicate holds for at least one projected element in the range.
132/// Overloads also check whether any of the supplied Boolean values are true.
133// parasoft-begin-suppress AUTOSAR-M7_3_3-a "An unnamed namespace is used to create a per-TU reference to a global
134// object used in multiple TUs."
135// parasoft-begin-suppress CERT_CPP-DCL59-a "An unnamed namespace is used to create a per-TU reference to a global
136// object used in multiple TUs."
137// parasoft-begin-suppress CERT_CPP-DCL56-a "False positive: variable is initialized"
138ARENE_CPP14_INLINE_VARIABLE(algorithm_detail::synthesize_range1_overload_t<any_of_detail::any_of_fn>, any_of);
139// parasoft-end-suppress CERT_CPP-DCL56-a
140// parasoft-end-suppress AUTOSAR-M7_3_3-a
141// parasoft-end-suppress CERT_CPP-DCL59-a
142
143} // namespace base
144} // namespace arene
145
146#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ALGORITHM_ANY_OF_HPP_
#define ARENE_CPP14_INLINE_VARIABLE(type, name)
Emulates C++17's inline variable declarations for function objects.
Definition cpp14_inline.hpp:69
Definition accumulate.hpp:37
constexpr bool is_predicate_v
backport for the C++20 predicate concept
typename invoke_result< Fn, Args... >::type invoke_result_t
The return type of a functor invoked with the specified arguments.
Definition is_invocable.hpp:271
constexpr auto make_subrange(Iterator iterator, Sentinel sentinel) noexcept(std::is_nothrow_move_constructible< Iterator >::value &&//std::is_nothrow_move_constructible< Sentinel >::value) -> make_subrange_detail::iterator_range< Iterator, Sentinel >
Combine an iterator and sentinel into a range type that can be used with range-based for loops.
Definition make_subrange.hpp:98
constexpr bool is_nothrow_invocable_v
Determine if a functor type is no-throw invocable with the specified argument types.
constexpr bool all_are_same_v
typename constraints_impl::constraints< EnableIfs... >::type constraints
Helper used to express template constraints in a consistent way using std::enable_if_t.
Definition constraints.hpp:51
Definition accumulate.hpp:36
bool
Check if element Idx and all subsequent elements of the two tuples are equal.
Definition tuple.hpp:649
constexpr auto move(T &&value) noexcept -> remove_reference_t< T > &&
Cast a value to an rvalue.
Definition move.hpp:30
typename enable_if< Condition, Type >::type enable_if_t
Type alias to support constraining templates. Is an alias for the specified type if the Condition is ...
Definition enable_if.hpp:41
auto declval() noexcept -> T &&
A dummy function that is declared but not defined, with a user-specified return type,...