Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
generate.hpp
Go to the documentation of this file.
1// parasoft-begin-suppress AUTOSAR-A2_8_1-a-2 "False positive: also defines arene::base::generate"
2
3// Copyright 2026, Toyota Motor Corporation
4//
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
7#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ALGORITHM_GENERATE_HPP_
8#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ALGORITHM_GENERATE_HPP_
9
10// IWYU pragma: private, include "arene/base/algorithm.hpp"
11// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
12
13// parasoft-begin-suppress AUTOSAR-A7_1_5-a-2 "Trailing return syntax permitted by A7-1-5 Permit #1 v1.0.0"
14
15// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
16#include "arene/base/algorithm/detail/synthesize_range1_overload.hpp" // IWYU pragma: export
20#include "arene/base/stdlib_choice/enable_if.hpp"
21#include "arene/base/stdlib_choice/is_assignable.hpp"
25// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
26
27namespace arene {
28namespace base {
29
30namespace generate_detail {
31
32/// @brief function object implementing 'generate'
33class generate_fn {
34 public:
35 /// @brief Assigns each element in a sequence the result of a successive invocation of a generator.
36 ///
37 /// @tparam Itr The type of the iterator for the sequence. It must be a forward iterator whose @c reference is
38 /// assignable from the result of invoking @c Generator
39 /// @tparam Sent The type of the sentinel to mark the end of the sequence. It must be inequality comparable with
40 /// @c Itr
41 /// @tparam Generator The type of the generator. It must be invocable with no arguments.
42 /// @param first Iterator to the first position in the sequence to generate into.
43 /// @param last Sentinel to mark the end of the sequence to generate into.
44 /// @param gen The generator invoked once per element of the sequence.
45 /// @post All elements in the sequence @c [first,last) will have been assigned the result of invoking @c gen
46 /// @pre Repeated incrementing of @c first must eventually produce an iterator which compares equal to @c last ,
47 /// else behaviour is undefined.
48 ///
49 /// Performs exactly @c std::distance(first,last) invocations of @c gen and the same number of assignments.
50 /// Backport of the @c constexpr [std::generate](https://en.cppreference.com/w/cpp/algorithm/generate) from C++20 .
51 template <
52 typename Itr,
53 typename Sent,
54 typename Generator,
60 constexpr auto operator()(Itr first, Sent last, Generator gen) const
61 noexcept(std::is_nothrow_assignable<iter_reference_t<Itr>, invoke_result_t<Generator&>>::value && is_nothrow_invocable_v<Generator&> && denotes_nothrow_iterable_range_v<Itr, Sent>)
62 -> void {
63 // parasoft-begin-suppress AUTOSAR-A6_5_2-a "False Positive: The loop counter is 'first'"
64 for (; first != last; ++first) {
65 *first = gen();
66 }
67 // parasoft-end-suppress AUTOSAR-A6_5_2-a
68 }
69};
70
71} // namespace generate_detail
72
73/// @brief Assigns each element in a sequence the result of a successive invocation of a generator.
74///
75/// An additional overload accepting a range in place of the iterator/sentinel pair is provided, which forwards to the
76/// iterator-based overload with the @c begin and @c end of the range.
77// parasoft-begin-suppress AUTOSAR-M7_3_3-a "An unnamed namespace is used to create a per-TU reference to a global
78// object used in multiple TUs."
79// parasoft-begin-suppress CERT_CPP-DCL59-a "An unnamed namespace is used to create a per-TU reference to a global
80// object used in multiple TUs."
81// parasoft-begin-suppress CERT_CPP-DCL56-a "False positive: variable is initialized"
82ARENE_CPP14_INLINE_VARIABLE(algorithm_detail::synthesize_range1_overload_t<generate_detail::generate_fn>, generate);
83// parasoft-end-suppress CERT_CPP-DCL56-a
84// parasoft-end-suppress AUTOSAR-M7_3_3-a
85// parasoft-end-suppress CERT_CPP-DCL59-a
86
87} // namespace base
88} // namespace arene
89
90#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ALGORITHM_GENERATE_HPP_
91// parasoft-end-suppress AUTOSAR-A2_8_1-a-2
#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
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 denotes_nothrow_iterable_range_v
Check if an iterator/sentinel pair form an iterable range that can be iterated without throwing.
constexpr bool is_nothrow_invocable_v
Determine if a functor type is no-throw invocable with the specified argument types.
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
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