Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
reference_wrapper.hpp
Go to the documentation of this file.
1// Copyright 2026, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_REFERENCE_WRAPPER_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_REFERENCE_WRAPPER_HPP_
7
8// parasoft-begin-suppress CERT_CPP-DCL58-a-2 "Part of a standard library implementation"
9// parasoft-begin-suppress AUTOSAR-A17_6_1-a-2 "Part of a standard library implementation"
10
11// IWYU pragma: private, include <functional>
12// IWYU pragma: friend "stdlib_detail/.*"
13
14#include "arene/base/constraints.hpp"
15#include "arene/base/functional/function_traits.hpp"
16#include "arene/base/functional/invoke.hpp"
17#include "arene/base/functional/is_noexcept_part_of_function_type.hpp"
18#include "arene/base/type_list/concat.hpp"
19#include "arene/base/type_list/type_list.hpp"
20#include "arene/base/type_traits/is_invocable.hpp"
21#include "stdlib/include/stdlib_detail/addressof.hpp"
22#include "stdlib/include/stdlib_detail/enable_if.hpp"
23#include "stdlib/include/stdlib_detail/forward.hpp"
24#include "stdlib/include/stdlib_detail/integral_constant.hpp"
25#include "stdlib/include/stdlib_detail/is_class.hpp"
26#include "stdlib/include/stdlib_detail/is_function.hpp"
27#include "stdlib/include/stdlib_detail/is_member_function_pointer.hpp"
28#include "stdlib/include/stdlib_detail/remove_pointer.hpp"
29#include "stdlib/include/stdlib_detail/remove_reference.hpp"
30
31namespace std {
32
34
35/// @brief Add argument member types to reference wrapped functions and member
36/// function pointers.
37/// The types added depend on the number of arguments expected by the wrapped type.
38/// For member function pointers the class pointer is considered as the first argument.
39///
40/// Default case, do not add any argument member types
41///
42/// @tparam Args List of args to the wrapped function
43template <typename TypeList>
45
46/// @brief One argument case, will add @c argument_type
47/// @tparam Single argument to the wrapped function
48template <typename Arg>
50 public:
51 /// @brief Type of the wrapped function's argument
53};
54
55/// @brief Two argument case, will add @c first_argument_type and @c second_argument_type
56/// @tparam Arg1 First argument to the wrapped function
57/// @tparam Arg2 Second argument to the wrapped function
58template <typename Arg1, typename Arg2>
60 public:
61 /// @brief Type of the wrapped function's first argument
63 /// @brief Type of the wrapped function's second argument
65};
66
67/// @brief Forward member type @c result_type from a reference wrapped class
68/// containing that member type, if present
69///
70/// Case where @c reference_type is not present
71///
72/// @tparam T Wrapped class to forward type from
73template <typename T, typename = arene::base::constraints<>>
75
76/// @brief Case where @c reference_type is present and forwarded
77/// @tparam T Wrapped class to forward type from
78template <typename T>
80 public:
81 /// @brief Forwarded type from the wrapped class
82 using result_type = typename T::result_type;
83};
84
85/// @brief Forward member type @c argument_type from a reference wrapped class
86/// containing that member type, if present
87///
88/// Case where @c argument_type is not present
89///
90/// @tparam T Wrapped class to forward types from
91template <typename T, typename = arene::base::constraints<>>
93
94/// @brief Case where @c argument_type is present and forwarded
95/// @tparam T Wrapped class to forward type from
96template <typename T>
98 public:
99 /// @brief Forwarded type from the wrapped class
100 using argument_type = typename T::argument_type;
101};
102
103/// @brief Forward member types @c first_argument_type and @c second_argument_type
104/// from a reference wrapped class containing those member types, if present
105///
106/// Case where @c first_argument_type and @c second_argument_type are not present
107///
108/// @tparam T Wrapped class to forward types from
109template <typename T, typename = arene::base::constraints<>>
110/// @brief Forwarded type from the wrapped class
112
113/// @brief Case where @c first_argument_type and @c second_argument_type are
114/// present and forwarded
115/// @tparam T Wrapped class to forward types from
116template <typename T>
118 T,
120 public:
121 /// @brief Forwarded type from the wrapped class
123 /// @brief Forwarded type from the wrapped class
125};
126
127/// @brief Add member types to @c reference_wrapper based on the underlying wrapped type
128///
129/// Base case where the wrapped type does not match requirements and no member
130/// types are added
131///
132/// @tparam T Wrapped type to check
133template <typename T, typename = arene::base::constraints<>>
135
136/// @brief Wrapped function case, sets @c result_type based on the return type
137/// of the function and potentially types based on argument types
138/// @tparam T Function to extract types from
139template <typename T>
146
147/// @brief Wrapped member function pointer case, sets @c result_type based on
148/// the return type of the function and potentially types based on member class
149/// and argument types
150/// @tparam T Member function pointer to extract types from
151template <typename T>
161
162/// @brief Wrapped class case, potentially forwards member types if present
163/// within the class
164/// @tparam T Class type to forward types from
165template <typename T>
170
171} // namespace reference_wrapper_detail
172
173// parasoft-begin-suppress AUTOSAR-A2_10_1-e "False positive: 'reference_wrapper' does not hide an identifier in
174// 'tuple'"
175/// @brief wraps a reference in a copyable and assignable object
176/// @tparam T Type of the reference
177template <typename T>
179 // parasoft-begin-suppress AUTOSAR-A5_0_3-a "False positive: there is only 1 level of pointer indirection"
180 /// @brief The wrapped reference
181 T* pointer_;
182 // parasoft-end-suppress AUTOSAR-A5_0_3-a
183
184 public:
185 /// @brief Wrapped type
186 // parasoft-begin-suppress AUTOSAR-A2_10_1-e "False positive: this is in a different scope from other 'type's"
187 using type = T;
188 // parasoft-end-suppress AUTOSAR-A2_10_1-e
189
190 // TODO: Implement member types for functions
191
192 /// @brief Construct a reference_wrapper around a given input. Wrapped object must
193 /// have an lvalue reference address and not be the same type of
194 /// @c reference_wrapper.
195 /// @tparam U Type of passed object or function to wrap
196 /// @param to_wrap or function to wrap
197 // NOLINTNEXTLINE(hicpp-explicit-conversions) Not explicit per C++14 spec
198 reference_wrapper(T& to_wrap) noexcept
199 : pointer_{addressof(to_wrap)} {}
200
201 // parasoft-begin-suppress CERT_C-EXP37-a "False positive: The rule does not mention naming all parameters"
202 /// @brief Cannot be constructed from an rvalue reference
203 reference_wrapper(T&&) = delete;
204 // parasoft-end-suppress CERT_C-EXP37-a
205
206 /// @brief Access the stored reference
207 /// @return The reference wrapped by this instance
208 auto get() const noexcept -> T& { return *pointer_; }
209
210 // parasoft-begin-suppress AUTOSAR-A13_5_2-a "Not explicit per C++14 spec"
211 // parasoft-begin-suppress AUTOSAR-A13_2_3-a "False positive: Not a relational operator"
212 /// @brief Access the stored reference
213 /// @return The reference wrapped by this instance
214 // NOLINTNEXTLINE(hicpp-explicit-conversions) Not explicit per C++14 spec
215 operator T&() const noexcept { return get(); }
216 // parasoft-end-suppress AUTOSAR-A13_5_2-a
217 // parasoft-end-suppress AUTOSAR-A13_2_3-a
218
219 // parasoft-begin-suppress AUTOSAR-M2_10_1-a "Similar identifiers permitted by M2-10-1 Permit #1 v1.0.0"
220 /// @brief Calls the stored function with given args. Only available if the stored
221 /// type is callable.
222 /// @param args Arguments to forward to the stored function
223 /// @return The result of invoking the wrapped function
224 template <class... Args, arene::base::constraints<enable_if_t<arene::base::is_invocable_v<T&, Args...>>> = nullptr>
225 auto operator()(Args&&... args) const noexcept(arene::base::is_nothrow_invocable_v<T&, Args...>)
226 -> arene::base::invoke_result_t<T&, Args...> {
227 return arene::base::invoke(get(), std::forward<Args>(args)...);
228 }
229 // parasoft-end-suppress AUTOSAR-M2_10_1-a
230};
231// parasoft-end-suppress AUTOSAR-A2_10_1-e
232
233// parasoft-begin-suppress AUTOSAR-M3_3_2-a "False positive: inline function used in multiple translation units"
234/// @brief Create a type-deduced @c reference_wrapper
235/// @tparam T type of the reference
236/// @param to_wrap Object or function to wrap
237/// @return A @c reference_wrapper around the input
238template <typename T>
239auto ref(T& to_wrap) noexcept -> reference_wrapper<T> {
240 return reference_wrapper<T>{to_wrap};
241}
242// parasoft-end-suppress AUTOSAR-M3_3_2-a
243
244/// @brief Create a type-deduced copy of a @c reference_wrapper
245/// @tparam T Type wrapped by the input wrapper
246/// @param other @c reference_wrapper to copy
247/// @return A copy of the input @c reference_wrapper
248template <typename T>
249auto ref(reference_wrapper<T> other) noexcept -> reference_wrapper<T> {
250 return other;
251}
252
253// parasoft-begin-suppress CERT_C-EXP37-a-3 "False positive: The rule does not mention naming all parameters"
254/// @brief Cannot create a @c reference_wrapper from an rvalue reference
255/// @tparam T type of the reference
256template <typename T>
257auto ref(T&&) = delete;
258// parasoft-end-suppress CERT_C-EXP37-a
259
260// parasoft-begin-suppress AUTOSAR-M3_3_2-a "False positive: inline function used in multiple translation units"
261/// @brief Create a const type-deduced @c reference_wrapper
262/// @tparam T type of the reference
263/// @param to_wrap Object or function to wrap
264/// @return A @c reference_wrapper around the input of const-qualified type
265template <typename T>
266auto cref(T& to_wrap) noexcept -> reference_wrapper<T const> {
267 return reference_wrapper<T const>{to_wrap};
268}
269// parasoft-end-suppress AUTOSAR-M3_3_2-a
270
271/// @brief Create a const type-deduced copy of a @c reference_wrapper
272/// @tparam T Type wrapped by the input wrapper
273/// @param other @c reference_wrapper to copy
274/// @return A copy of the input @c reference_wrapper
275template <typename T>
276auto cref(reference_wrapper<T> other) noexcept -> reference_wrapper<T const> {
277 return other;
278}
279
280// parasoft-begin-suppress CERT_C-EXP37-a-3 "False positive: The rule does not mention naming all parameters"
281/// @brief Cannot create a @c reference_wrapper from an rvalue reference
282/// @tparam T type of the reference
283template <typename T>
284auto cref(T&&) = delete;
285// parasoft-end-suppress CERT_C-EXP37-a
286
287} // namespace std
288
289#endif // INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_REFERENCE_WRAPPER_HPP_
Add argument member types to reference wrapped functions and member function pointers....
Definition reference_wrapper.hpp:44
Forward member types first_argument_type and second_argument_type from a reference wrapped class cont...
Definition reference_wrapper.hpp:111
Forward member type result_type from a reference wrapped class containing that member type,...
Definition reference_wrapper.hpp:74
Forward member type argument_type from a reference wrapped class containing that member type,...
Definition reference_wrapper.hpp:92
Add member types to reference_wrapper based on the underlying wrapped type.
Definition reference_wrapper.hpp:134
wraps a reference in a copyable and assignable object
Definition reference_wrapper.hpp:178
reference_wrapper(T &&)=delete
Cannot be constructed from an rvalue reference.
auto get() const noexcept -> T &
Access the stored reference.
Definition reference_wrapper.hpp:208
reference_wrapper(T &to_wrap) noexcept
Construct a reference_wrapper around a given input. Wrapped object must have an lvalue reference addr...
Definition reference_wrapper.hpp:198
operator T&() const noexcept
Access the stored reference.
Definition reference_wrapper.hpp:215
Definition reference_wrapper.hpp:33
auto ref(T &&)=delete
Cannot create a reference_wrapper from an rvalue reference.
auto ref(T &to_wrap) noexcept -> reference_wrapper< T >
Create a type-deduced reference_wrapper.
Definition reference_wrapper.hpp:239
auto ref(reference_wrapper< T > other) noexcept -> reference_wrapper< T >
Create a type-deduced copy of a reference_wrapper.
Definition reference_wrapper.hpp:249
auto cref(T &&)=delete
Cannot create a reference_wrapper from an rvalue reference.
auto cref(reference_wrapper< T > other) noexcept -> reference_wrapper< T const >
Create a const type-deduced copy of a reference_wrapper.
Definition reference_wrapper.hpp:276
auto cref(T &to_wrap) noexcept -> reference_wrapper< T const >
Create a const type-deduced reference_wrapper.
Definition reference_wrapper.hpp:266
constexpr auto operator()(::arene::base::result< void, E > const &value) const noexcept(noexcept(hash< E >{}(std::declval< E const & >()))) -> std::size_t
Calculate the hash of a result.
Definition result.hpp:1827