Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
pointer_traits.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_STDLIB_INCLUDE_STDLIB_DETAIL_POINTER_TRAITS_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_POINTER_TRAITS_HPP_
7
8#include "arene/base/constraints.hpp"
9#include "stdlib/include/stdlib_detail/addressof.hpp"
10#include "stdlib/include/stdlib_detail/conditional.hpp"
11#include "stdlib/include/stdlib_detail/cstddef.hpp"
12#include "stdlib/include/stdlib_detail/enable_if.hpp"
13#include "stdlib/include/stdlib_detail/integral_constant.hpp"
14#include "stdlib/include/stdlib_detail/is_void.hpp"
15
16// parasoft-begin-suppress CERT_CPP-DCL58-a-2 "Part of a standard library implementation"
17// parasoft-begin-suppress AUTOSAR-A17_6_1-a-2 "Part of a standard library implementation"
18
19// IWYU pragma: private, include <memory>
20// IWYU pragma: friend "stdlib_detail/.*"
21
22namespace std {
23
25
26/// @brief Require that @c PointerLike has a type member named @c element_type
27template <class PointerLike>
28using require_element_type = typename PointerLike::element_type;
29
30/// @brief @c true if @c PointerLike has a type member named @c element_type
31template <class PointerLike>
32constexpr extern bool has_element_type_v = arene::base::substitution_succeeds<require_element_type, PointerLike>;
33
34/// @brief Determine the @c element_type to expose in @c pointer_traits
35template <class PointerLike, class = arene::base::constraints<>>
37
38/// @brief Determine the @c element_type to expose in the @c pointer_traits
39///
40/// Use @c PointerLike::element_type if defined
41template <class PointerLike>
45 /// @brief The resulting type
46 using type = typename PointerLike::element_type;
47};
48
49/// @brief Determine the @c element_type to expose in the @c pointer_traits
50///
51/// Otherwise, use the first template parameter if @c PointerLike is a class template and @c element_type is not defined
52template <template <class, class...> class PointerLike, class T, class... Args>
54 PointerLike<T, Args...>,
56 /// @brief The resulting type
57 using type = T;
58};
59
60/// @brief Require that @c PointerTraitsElementType has a type member named @c type
61template <class PointerTraitsElementType>
62using require_type = typename PointerTraitsElementType::type;
63
64/// @brief @c true if an @c element_type was determined for @c PointerLike
65///
66/// If no @c element_type is determined, then the resulting @c pointer_traits will define no members
67template <class PointerLike>
68constexpr extern bool is_element_type_defined_v =
70
71/// @brief Require that @c PointerLike has a type member named @c difference_type
72template <class PointerLike>
73using require_difference_type = typename PointerLike::difference_type;
74
75/// @brief Determine the @c difference_type to expose in the @c pointer_traits
76///
77/// Default to using @c std::ptrdiff_t
78template <class PointerLike, class = arene::base::constraints<>>
80 /// @brief The resulting type
81 using type = ptrdiff_t;
82};
83
84/// @brief Determine the @c difference_type to expose in the @c pointer_traits
85///
86/// Use @c PointerLike::difference_type if defined
87template <class PointerLike>
94
95/// @brief Require that @c PointerLike has an alias template member named @c rebind
96template <class PointerLike, class U>
97using require_rebind = typename PointerLike::template rebind<U>;
98
99/// @brief @c true if @c PointerLike has an alias template member named @c rebind
100template <class PointerLike, class U>
101constexpr extern bool has_rebind_v = arene::base::substitution_succeeds<require_rebind, PointerLike, U>;
102
103/// @brief Determine the @c rebind to expose in the @c pointer_traits
104template <class T, class U, class = arene::base::constraints<>>
106
107/// @brief Determine the @c rebind to expose in the @c pointer_traits
108///
109/// Use <c> template<class U>PointerLike::rebind<U> </c> if defined
110template <class T, class U>
112 /// @brief The resulting type
113 using type = typename T::template rebind<U>;
114};
115
116/// @brief Determine the @c rebind to expose in the @c pointer_traits
117///
118/// Otherwise, use <c> template<class U>PointerLike<U, Args...> </c> if @c PointerLike is a class template
119template <template <class, class...> class PointerLike, class T, class U, class... Args>
121 PointerLike<T, Args...>,
122 U,
124 /// @brief The resulting type
125 using type = PointerLike<U, Args...>;
126};
127
128/// @brief An unspecified type to use as the input type for @c pointer_to
130 public:
131 /// @brief Deleted constructor to prevent any accidental use
132 explicit unspecified() = delete;
133};
134
135/// @brief Determine the input argument type of @c pointer_traits<PointerLike>::pointer_to
136///
137/// If @c PointerLike::element_type is @c void, then the argument type is unspecified
138/// Otherwise, the argument type is @c element_type&
139template <class ElementType>
141
142/// @brief Define @c pointer_traits as empty when @c is_element_type_defined_v<PointerLike> is false
143template <class PointerLike, class = arene::base::constraints<>>
145
146/// @brief Define @c pointer_traits for the base case when @c is_element_type_defined_v is true
147template <class PointerLike>
149 public:
150 /// @brief The type of the @c PointerLike
152
153 /// @brief The type of the element that is pointed to
155
156 /// @brief The type used to represent the difference between two @c PointerLike instances
158
159 /// @brief The @c pointer type rebound to hold an element of type @c U
160 /// @tparam The new type of the element to bind
161 template <class U>
163
164 /// @brief Return a @c pointer to the given element
165 /// @param element The element to get a pointer to
166 /// @return A @c pointer to the input element
168 ) noexcept(noexcept(PointerLike::pointer_to(element))) -> pointer {
170 }
171};
172} // namespace pointer_traits_detail
173
174/// @brief provides a uniform interface to pointer-like types
175/// @tparam T pointer type to retrieve properties for
176///
177/// @c pointer_traits provides a uniform interface to retrieve properties of pointers and other types that behave like
178/// pointers. This template can be specialized for user-defined pointer-like types so that the information about the
179/// pointer can be retrieved even if type does not provide the usual typedefs.
180///
181/// @note this implementation includes the C++23 implementation change to make the traits SFINAE-friendly for the case
182/// where @c element_type is not defined.
183/// @see https://cplusplus.github.io/LWG/issue3545
184template <class T>
186
187/// @brief @c pointer_traits specialization to handle raw pointer types.
188template <class T>
189class pointer_traits<T*> {
190 public:
191 /// @brief The type of the pointer.
192 using pointer = T*;
193
194 /// @brief The type of the element that is pointed to
195 using element_type = T;
196
197 /// @brief The type used to represent the difference between two pointers
199
200 /// @brief A pointer to @c U
201 /// @tparam U The new element type
202 template <class U>
203 using rebind = U*;
204
205 // parasoft-begin-suppress AUTOSAR-A5_0_3-a "False positive: there is only 1 level of pointer indirection"
206 /// @brief Return a @c pointer to the given element
207 /// @param element The element to get a pointer to
208 /// @return A @c pointer to the input element
209 static constexpr auto pointer_to(pointer_traits_detail::pointer_to_arg_t<element_type> element) noexcept -> pointer {
210 return addressof(element);
211 }
212 // parasoft-end-suppress AUTOSAR-A5_0_3-a
213};
214
215} // namespace std
216
217#endif // INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_POINTER_TRAITS_HPP_
static constexpr auto pointer_to(pointer_traits_detail::pointer_to_arg_t< element_type > element) noexcept -> pointer
Return a pointer to the given element.
Definition pointer_traits.hpp:209
Define pointer_traits as empty when is_element_type_defined_v<PointerLike> is false.
Definition pointer_traits.hpp:144
An unspecified type to use as the input type for pointer_to.
Definition pointer_traits.hpp:129
unspecified()=delete
Deleted constructor to prevent any accidental use.
provides a uniform interface to pointer-like types
Definition pointer_traits.hpp:185
Definition pointer_traits.hpp:24
constexpr bool is_element_type_defined_v
true if an element_type was determined for PointerLike
constexpr bool has_rebind_v
true if PointerLike has an alias template member named rebind
constexpr bool has_element_type_v
true if PointerLike has a type member named element_type
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
Determine the difference_type to expose in the pointer_traits.
Definition pointer_traits.hpp:79
Determine the element_type to expose in pointer_traits.
Definition pointer_traits.hpp:36
Determine the rebind to expose in the pointer_traits.
Definition pointer_traits.hpp:105