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"
15#include "arene/base/algorithm/detail/synthesize_range1_overload.hpp" // IWYU pragma: export
16#include "arene/base/algorithm/detail/traits.hpp"
22#include "arene/base/stdlib_choice/declval.hpp"
23#include "arene/base/stdlib_choice/enable_if.hpp"
24#include "arene/base/stdlib_choice/initializer_list.hpp"
25#include "arene/base/stdlib_choice/is_convertible.hpp"
26#include "arene/base/stdlib_choice/iterator_traits.hpp"
27#include "arene/base/stdlib_choice/move.hpp"
32// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
33
34namespace arene {
35namespace base {
36
37namespace all_of_detail {
38
39/// @brief function object implementing 'all_of'
40class all_of_fn //
41{
42 public:
43 /// @brief Check if a predicate returns true for all elements in the range
44 /// @tparam Iterator the type of the iterator
45 /// @tparam UnaryPredicate the type of the predicate
46 /// @tparam Projection projection to apply to element in the range
47 /// @param begin The start of the range
48 /// @param end The end of the range
49 /// @param pred The predicate to apply to each element of the range
50 /// @param proj Projection to apply to elements before checking the predicate
51 /// @return bool @c true if @c pred returns true for all of the projected
52 /// elements in the range, @c false otherwise.
53 // parasoft-begin-suppress AUTOSAR-A2_10_1-a "False positive: identifiers 'begin' and 'end' do not hide anything"
54 template <
55 typename Iterator,
56 typename UnaryPredicate,
57 typename Projection = identity,
61 UnaryPredicate&,
63 constexpr auto operator()(Iterator begin, Iterator end, UnaryPredicate pred, Projection proj = {}) const noexcept( //
64 noexcept(any_of( //
69 )) //
70 ) //
71 -> bool //
72 {
73 return !any_of( //
74 std::move(begin),
75 std::move(end),
76 not_fn(std::move(pred)),
77 std::move(proj)
78 );
79 }
80 // parasoft-end-suppress AUTOSAR-A2_10_1-a
81
82 /// @brief Check if all the elements in the supplied range are true when cast to
83 /// @c bool .
84 /// @tparam Iterator the type of the iterator
85 /// @param begin The start of the range
86 /// @param end The end of the range
87 /// @return bool @c true if all of the elements in the range are @c true, @c false
88 /// otherwise.
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 all_of_fn, //
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 all the values in the supplied list are true.
112 /// @param values A list of values
113 /// @return bool @c true if all of the values are @c true, @c false otherwise
114 /// @note this is exported outside of detail in algorithm/all_of.hpp for
115 /// backwards compatibility reasons
116 constexpr auto operator()(std::initializer_list<bool> const values) const noexcept -> bool { // CODEQLFP(DCL51-CPP)
117 return (*this)(values.begin(), values.end());
118 }
119
120 /// @brief Check if all the supplied arguments are true.
121 /// @tparam Args The types of the arguments
122 /// @param args the arguments
123 /// @pre All the arguments must be bool.
124 /// @return bool @c true if all of the elements are @c true, @c false otherwise
125 template <typename... Args, constraints<std::enable_if_t<base::all_are_same_v<bool, Args...>>> = nullptr>
126 constexpr auto operator()(Args... args) const noexcept -> bool { // CODEQLFP(DCL51-CPP) CODEQLFP(A7-1-1)
127 return (*this)({args...}); // CODEQLFP(M8-5-2)
128 }
129};
130} // namespace all_of_detail
131
132/// @brief checks if a predicate is true for all of the elements in a range
133/// Accepts an iterator pair or a range, with an optional predicate and projection.
134/// Returns @c true if the predicate holds for every element; without a predicate, checks each element as a boolean.
135// parasoft-begin-suppress AUTOSAR-M7_3_3-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-DCL59-a "An unnamed namespace is used to create a per-TU reference to a global
138// object used in multiple TUs."
139// parasoft-begin-suppress AUTOSAR-M3_4_1-b "This is a per-TU reference to a global used in multiple TUs"
140// parasoft-begin-suppress CERT_CPP-DCL56-a "False positive: variable is initialized"
141ARENE_CPP14_INLINE_VARIABLE(algorithm_detail::synthesize_range1_overload_t<all_of_detail::all_of_fn>, all_of);
142// parasoft-end-suppress CERT_CPP-DCL56-a
143// parasoft-end-suppress AUTOSAR-M3_4_1-b
144// parasoft-end-suppress AUTOSAR-M7_3_3-a
145// parasoft-end-suppress CERT_CPP-DCL59-a
146
147} // namespace base
148} // namespace arene
149
150#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ALGORITHM_ALL_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 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,...