Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
all_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_ALL_OF_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ALGORITHM_ALL_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/any_of.hpp"
15#include "arene/base/algorithm/detail/convert_to.hpp"
16#include "arene/base/algorithm/detail/traits.hpp"
17#include "arene/base/compiler_support/cpp14_inline.hpp"
18#include "arene/base/constraints/constraints.hpp"
19#include "arene/base/functional/identity.hpp"
20#include "arene/base/functional/not_fn.hpp"
21#include "arene/base/stdlib_choice/declval.hpp"
22#include "arene/base/stdlib_choice/enable_if.hpp"
23#include "arene/base/stdlib_choice/initializer_list.hpp"
24#include "arene/base/stdlib_choice/is_convertible.hpp"
25#include "arene/base/stdlib_choice/iterator_traits.hpp"
26#include "arene/base/stdlib_choice/move.hpp"
27#include "arene/base/type_traits/all_are_same.hpp"
28#include "arene/base/type_traits/is_invocable.hpp"
29#include "arene/base/type_traits/is_predicate.hpp"
30#include "arene/base/type_traits/iterator_category_traits.hpp"
31// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
32
33namespace arene {
34namespace base {
35
36namespace all_of_detail {
37
38/// @brief function object implementing 'all_of'
39class all_of_fn //
40{
41 public:
42 /// @brief Check if a predicate returns true for all elements in the range
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 all of the projected
51 /// elements in the range, @c false otherwise.
52 template <
53 typename Iterator,
54 typename UnaryPredicate,
55 typename Projection = identity,
56 constraints<
57 std::enable_if_t<base::is_input_iterator_v<Iterator>>,
58 std::enable_if_t<is_predicate_v<
59 UnaryPredicate&,
60 invoke_result_t<Projection&, algorithm_detail::iter_reference_t<Iterator>>>>> = nullptr>
61 constexpr auto operator()(Iterator begin, Iterator end, UnaryPredicate pred, Projection proj = {}) const noexcept( //
62 noexcept(any_of( //
63 std::declval<Iterator&&>(), //
64 std::declval<Iterator&&>(), //
65 not_fn(std::declval<UnaryPredicate&&>()), //
66 std::declval<Projection&&>() //
67 )) //
68 ) //
69 -> bool //
70 {
71 return !any_of( //
72 std::move(begin),
73 std::move(end),
74 not_fn(std::move(pred)),
75 std::move(proj)
76 );
77 }
78
79 /// @brief Check if all the elements in the supplied range are true when cast to
80 /// @c bool .
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 all of the elements in the range are @c true, @c false
85 /// otherwise.
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 all_of_fn, //
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 all the values in the supplied list are true.
107 /// @param values A list of values
108 /// @return bool @c true if all of the values are @c true, @c false otherwise
109 /// @note this is exported outside of detail in algorithm/all_of.hpp for
110 /// backwards compatibility reasons
111 constexpr auto operator()(std::initializer_list<bool> const values) const noexcept -> bool { // CODEQLFP(DCL51-CPP)
112 return (*this)(values.begin(), values.end());
113 }
114
115 /// @brief Check if all the supplied arguments are true.
116 /// @tparam Args The types of the arguments
117 /// @param args the arguments
118 /// @pre All the arguments must be bool.
119 /// @return bool @c true if all of the elements are @c true, @c false otherwise
120 template <typename... Args, constraints<std::enable_if_t<base::all_are_same_v<bool, Args...>>> = nullptr>
121 constexpr auto operator()(Args... args) const noexcept -> bool { // CODEQLFP(DCL51-CPP) CODEQLFP(A7-1-1)
122 return (*this)({args...}); // CODEQLFP(M8-5-2)
123 }
124};
125} // namespace all_of_detail
126
127/// @def arene::base::all_of
128/// @brief checks if a predicate is true for all of the elements in a range
129/// @copydoc arene::base::all_of_detail::all_of_fn::operator()
130// parasoft-begin-suppress AUTOSAR-M7_3_3-a "An unnamed namespace is used to create a per-TU reference to a global
131// object used in multiple TUs."
132// parasoft-begin-suppress CERT_CPP-DCL59-a "An unnamed namespace is used to create a per-TU reference to a global
133// object used in multiple TUs."
134ARENE_CPP14_INLINE_VARIABLE(all_of_detail::all_of_fn, all_of);
135// parasoft-end-suppress AUTOSAR-M7_3_3-a
136// parasoft-end-suppress CERT_CPP-DCL59-a
137
138} // namespace base
139} // namespace arene
140
141#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ALGORITHM_ALL_OF_HPP_
Definition array_exceptions_disabled.cpp:11
ARENE_CPP14_INLINE_VARIABLE(all_of_detail::all_of_fn, all_of)
Copyright 2026, Toyota Motor Corporation.
Definition array_exceptions_disabled.cpp:10