Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
non_owning_ptr.hpp
Go to the documentation of this file.
1// Copyright 2023, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4///
5/// @file non_owning_ptr.hpp
6/// @brief Defines non_owning_ptr, a checked nullable pointer type.
7///
8
9// IWYU pragma: private, include "arene/base/pointer.hpp"
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#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_POINTER_NON_OWNING_PTR_HPP_
14#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_POINTER_NON_OWNING_PTR_HPP_
15
16// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
21#include "arene/base/stdlib_choice/cstddef.hpp"
22#include "arene/base/stdlib_choice/enable_if.hpp"
23#include "arene/base/stdlib_choice/hash.hpp"
24#include "arene/base/stdlib_choice/is_convertible.hpp"
25#include "arene/base/stdlib_choice/is_pointer.hpp"
26#include "arene/base/stdlib_choice/is_reference.hpp"
27#include "arene/base/stdlib_choice/remove_const.hpp"
28#include "arene/base/stdlib_choice/remove_pointer.hpp"
30// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
31
32// parasoft-begin-suppress CERT_C-EXP37-a-3 "False positive: The rule does not mention naming all parameters"
33
34namespace arene {
35namespace base {
36
37// forward declaration
38template <typename T>
39class non_owning_ptr;
40
41///
42/// @brief Trait that deduces if a type is an @c non_owning_ptr to any type
43///
44/// The test is after removing const and reference qualification
45///
46/// @tparam T The type to test if it is a @c non_owning_ptr
47///
48template <typename T>
50
51// parasoft-begin-suppress AUTOSAR-A14_5_1-a "False positive: non_owning_ptr has a copy constructor"
52// parasoft-begin-suppress AUTOSAR-A12_1_5-a-2 "False positive: Delegating constructors are used"
53///
54/// @brief A non-owning, nullable pointer with nullptr checks on dereference
55///
56/// @tparam T The type pointed to by the pointer.
57///
58/// A @c non_owning_ptr<T> models @c T* with the following differences:
59/// * Dereferencing @c non_owning_ptr<T> has defined behavior: an
60/// @c ARENE_PRECONDITION violation.
61/// * The arithmetic operations are deleted.
62/// * @c delete 'ing the held object through the @c non_owning_ptr is
63/// unsupported.
64///
65template <typename T>
67 // TODO: Decide if this constraint should be retained.
68 static_assert(
70 "Cannot create an object pointer to a raw pointer. Perhaps you "
71 "meant non_owning_ptr<T> for T* semantics, or "
72 "non_owning_ptr<non_owning_ptr<T>> for T** semantics?"
73 );
74 static_assert(
76 "Cannot create an object pointer to a reference. Perhaps you "
77 "meant non_owning_ptr<T> rather than non_owning_ptr<T&>?"
78 );
79
80 // parasoft-begin-suppress AUTOSAR-A2_10_1-e "False positive: Does not hide member of unrelated template"
81 /// @brief Type alias for base class
83 // parasoft-end-suppress AUTOSAR-A2_10_1-e
84
85 public:
86 // parasoft-begin-suppress AUTOSAR-A2_10_1-d "False positive: 'pointer' does not hide anything"
87 // parasoft-begin-suppress AUTOSAR-A2_10_1-e "False positive: Does not hide member of unrelated template"
88 /// @brief The type of a pointer to @c T.
89 using pointer = T*;
90 // parasoft-end-suppress AUTOSAR-A2_10_1-e
91 // parasoft-end-suppress AUTOSAR-A2_10_1-d
92 // parasoft-begin-suppress AUTOSAR-A2_10_1-e "False positive: Does not hide member of unrelated template"
93 /// @brief The type of the element pointed to.
94 using element_type = T;
95 // parasoft-end-suppress AUTOSAR-A2_10_1-e
96 // parasoft-begin-suppress AUTOSAR-A2_10_1-d "False positive: 'reference' does not hide anything"
97 /// @brief The type of a reference to @c T.
98 using reference = T&;
99 // parasoft-end-suppress AUTOSAR-A2_10_1-d
100
101 ///
102 /// @brief Default, trivial constructor.
103 /// @post Behavior is equivalent to constructing a raw pointer: If explicit,
104 /// @c get() will return nullptr, otherwise the state is undefined.
105 ///
106 constexpr non_owning_ptr() noexcept = default;
107
108 ///
109 /// @brief Constructor from nullptr.
110 /// @post @c get() returns nullptr.
111 ///
112 // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions)
113 constexpr non_owning_ptr(std::nullptr_t) noexcept
114 : ordering_base{},
115 ptr_(nullptr) {}
116
117 ///
118 /// @brief Constructor from a pointer to T
119 /// @param new_ptr The pointer to hold.
120 /// @post @c get() returns new_ptr
121 ///
122 // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions)
123 constexpr non_owning_ptr(pointer new_ptr) noexcept
124 : ordering_base{},
125 ptr_(new_ptr) {}
126
127 // parasoft-begin-suppress AUTOSAR-A12_1_1-a "False positive: Base class initialized by delegating constructor
128 ///
129 /// @brief Constructor from non_owning_ptr convertible to the held type.
130 ///
131 /// @tparam U the element type of the @c non_owning_ptr to convert from. Must
132 /// be pointer-convertible to @c pointer.
133 /// @param new_ptr The pointer to hold.
134 /// @post @c get() returns @c new_ptr converted to @c pointer.
135 ///
136 template <
137 typename U = element_type,
138 constraints<std::enable_if_t<std::is_convertible<typename non_owning_ptr<U>::pointer, pointer>::value>> = nullptr>
139 // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions)
140 constexpr non_owning_ptr(non_owning_ptr<U> new_ptr) noexcept
141 : non_owning_ptr(new_ptr.get()) {}
142
143 ///
144 /// @brief Constructor from a raw pointer convertible to the held type.
145 ///
146 /// @tparam U the pointer type to convert from. Must be convertible to
147 /// @c pointer .
148 /// @param new_ptr The pointer to hold.
149 /// @post @c get() returns @c new_ptr converted to @c pointer .
150 ///
151 template <
152 typename U = pointer,
156 // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions)
157 constexpr non_owning_ptr(U new_ptr) noexcept
158 : non_owning_ptr(static_cast<pointer>(new_ptr)) {}
159
160 // parasoft-end-suppress AUTOSAR-A12_1_1-a
161
162 ///
163 /// @brief Accesses the held pointer directly
164 ///
165 /// @return pointer The held pointer
166 ///
167 ARENE_NODISCARD constexpr auto get() const noexcept -> pointer { return ptr_; }
168
169 ///
170 /// @brief Resets the held pointer to a new value.
171 ///
172 /// @tparam U the pointer type to convert from. Must be convertible to
173 /// @c pointer .
174 /// @param new_ptr The pointer to reset from. Defaults to <c>nullptr</c>.
175 /// @post @c get() returns @c new_ptr converted to @c pointer .
176 ///
177 template <
178 typename U = pointer,
182 constexpr void reset(U new_ptr = nullptr) {
183 ptr_ = static_cast<pointer>(new_ptr);
184 }
185
186 ///
187 /// @brief Dereference operator
188 ///
189 /// @pre <c>get() != nullptr</c>, otherwise an @c ARENE_PRECONDITION
190 /// violation.
191 /// @return reference_type A reference to the object pointed to
192 ///
193 ARENE_NODISCARD constexpr auto operator*() const noexcept -> reference { return *checked_get(); }
194
195 ///
196 /// @brief Arrow operator
197 ///
198 /// @pre <c>get() != nullptr</c>, otherwise an @c ARENE_PRECONDITION
199 /// violation.
200 /// @return pointer The held pointer
201 ///
202 ARENE_NODISCARD constexpr auto operator->() const noexcept -> pointer { return checked_get(); }
203
204 ///
205 /// @brief Conversion operator to pointer-like type
206 ///
207 /// @tparam U the type to convert to. Must be convertible from
208 /// @c pointer .
209 /// @return U The held pointer, converted to @c U
210 ///
211 template <
212 typename U,
214 nullptr>
215 // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions)
216 constexpr explicit operator U() const noexcept {
217 return {get()};
218 }
219
220 ///
221 /// @brief Conversion to bool.
222 ///
223 /// @return true If <c>get() != nullptr</c>.
224 /// @return false If <c>get() == nullptr</c>.
225 ///
226 // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions)
227 ARENE_NODISCARD constexpr explicit operator bool() const noexcept { return get() != nullptr; }
228
229 ///
230 /// @brief Boolean negation operator.
231 ///
232 /// @return true If <c>get() == nullptr</c>
233 /// @return false If <c>get() != nullptr</c>
234 ///
235 ARENE_NODISCARD constexpr auto operator!() const noexcept -> bool { return !static_cast<bool>(*this); }
236
237 // parasoft-begin-suppress AUTOSAR-A0_1_3-a-2 "False positive: Function is namespace scope and used in other
238 // translation units"
239 // parasoft-begin-suppress AUTOSAR-A11_3_1-a-2 "False positive: comparison operators are permitted"
240 // parasoft-begin-suppress AUTOSAR-A2_7_3-a-2 "False positive: All overloads are fully are documented"
241 // parasoft-begin-suppress AUTOSAR-A2_7_3-b-2 "False positive: All overloads are fully are documented"
242 /// @brief 6-way comparison is equivalent to comparing raw pointers.
243 /// @param lhs The first value to compare
244 /// @param rhs The second value to compare
245 /// @return The result of the comparison of the underlying pointers
246 /// @{
247 ARENE_NODISCARD friend constexpr auto operator==(non_owning_ptr const& lhs, non_owning_ptr const& rhs) noexcept
248 -> bool {
249 return lhs.get() == rhs.get();
250 }
251
252 ARENE_NODISCARD friend constexpr auto operator<(non_owning_ptr const& lhs, non_owning_ptr const& rhs) noexcept
253 -> bool {
254 return lhs.get() < rhs.get();
255 }
256 /// @}
257 // parasoft-end-suppress AUTOSAR-A2_7_3-b-2
258 // parasoft-end-suppress AUTOSAR-A2_7_3-a-2
259 // parasoft-end-suppress AUTOSAR-A11_3_1-a-2
260 // parasoft-end-suppress AUTOSAR-A0_1_3-a-2
261
262 // parasoft-begin-suppress AUTOSAR-A2_7_3-a-2 "False positive: All overloads are fully are documented"
263 /// @brief Pointer arithmetic is explicitly deleted
264 /// @{
265 template <typename I>
266 constexpr auto operator[](I) = delete;
267 template <typename I>
268 constexpr auto operator[](I) const = delete;
269 template <typename I>
270 constexpr auto operator+(I const&) const -> non_owning_ptr = delete;
271 template <typename I>
272 constexpr auto operator-(I const&) const -> non_owning_ptr = delete;
273 constexpr auto operator++() -> non_owning_ptr& = delete;
274 // parasoft-begin-suppress AUTOSAR-A3_9_1-b-2 "False positive: postincrement requires an int parameter"
275 constexpr auto operator++(int) -> non_owning_ptr& = delete;
276 // parasoft-end-suppress AUTOSAR-A3_9_1-b-2
277 constexpr auto operator--() -> non_owning_ptr& = delete;
278 // parasoft-begin-suppress AUTOSAR-A3_9_1-b-2 "False positive: postdecrement requires an int parameter"
279 constexpr auto operator--(int) -> non_owning_ptr& = delete;
280 // parasoft-end-suppress AUTOSAR-A3_9_1-b-2
281 /// @}
282 // parasoft-end-suppress AUTOSAR-A2_7_3-a-2
283
284 private:
285 ///
286 /// @brief Equivalent to @c get() but with a precondition check
287 /// @pre <c>get() != nullptr</c>, otherwise an @c ARENE_PRECONDITION
288 /// violation.
289 /// @return pointer The held pointer
290 ///
291 ARENE_NODISCARD constexpr auto checked_get() const -> pointer {
292 ARENE_PRECONDITION(ptr_);
293 return ptr_;
294 }
295
296 /// @brief The held pointer
297 pointer ptr_;
298};
299// parasoft-end-suppress AUTOSAR-A12_1_5-a-2
300// parasoft-end-suppress AUTOSAR-A14_5_1-a
301
302///
303/// @brief @c non_owning_ptr factory to allow template deduction.
304///
305/// @tparam T The raw pointer type to construct the @c non_owning_ptr from
306/// @param ptr The pointer to construct the @c non_owning_ptr from.
307/// @return Equivalent to @c non_owning_ptr<std::remove_pointer_t<T>>{ptr}.
308///
309template <typename T, constraints<std::enable_if_t<std::is_pointer<T>::value>> = nullptr>
310ARENE_NODISCARD constexpr auto make_non_owning_ptr(T ptr) noexcept {
312}
313
314} // namespace base
315} // namespace arene
316
317// parasoft-begin-suppress AUTOSAR-A17_6_1-a-2 "False positive: specialization of standard templates is permitted"
318// parasoft-begin-suppress CERT_CPP-DCL58-a-2 "False positive: specialization of standard templates is permitted"
319namespace std {
320// parasoft-begin-suppress AUTOSAR-A11_0_2-a-2 "False positive: must follow the primary template"
321/// @brief @c std::hash specialization for @c non_owning_ptr.
322/// The hash is equivalent to the hash of the underlying pointer.
323///
324template <typename T>
326 /// @brief Calculate the hash of the pointer
327 /// @param ptr The pointer to calculate the hash of
328 /// @return The hash
329 constexpr auto operator()(::arene::base::non_owning_ptr<T> const& ptr) const noexcept -> size_t {
331 }
332};
333// parasoft-end-suppress AUTOSAR-A11_0_2-a-2
334
335} // namespace std
336// parasoft-end-suppress CERT_CPP-DCL58-a-2
337// parasoft-end-suppress AUTOSAR-A17_6_1-a-2
338
339#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_POINTER_NON_OWNING_PTR_HPP_
A non-owning, nullable pointer with nullptr checks on dereference.
Definition non_owning_ptr.hpp:66
constexpr auto operator+(I const &) const -> non_owning_ptr=delete
Pointer arithmetic is explicitly deleted.
ARENE_NODISCARD friend constexpr auto operator<(non_owning_ptr const &lhs, non_owning_ptr const &rhs) noexcept -> bool
6-way comparison is equivalent to comparing raw pointers.
Definition non_owning_ptr.hpp:252
ARENE_NODISCARD friend constexpr auto operator==(non_owning_ptr const &lhs, non_owning_ptr const &rhs) noexcept -> bool
6-way comparison is equivalent to comparing raw pointers.
Definition non_owning_ptr.hpp:247
ARENE_NODISCARD constexpr auto operator->() const noexcept -> pointer
Arrow operator.
Definition non_owning_ptr.hpp:202
constexpr non_owning_ptr(pointer new_ptr) noexcept
Constructor from a pointer to T.
Definition non_owning_ptr.hpp:123
ARENE_NODISCARD constexpr auto operator*() const noexcept -> reference
Dereference operator.
Definition non_owning_ptr.hpp:193
T & reference
The type of a reference to T.
Definition non_owning_ptr.hpp:98
constexpr auto operator[](I)=delete
Pointer arithmetic is explicitly deleted.
constexpr auto operator--() -> non_owning_ptr &=delete
Pointer arithmetic is explicitly deleted.
ARENE_NODISCARD constexpr auto get() const noexcept -> pointer
Accesses the held pointer directly.
Definition non_owning_ptr.hpp:167
constexpr non_owning_ptr(non_owning_ptr< U > new_ptr) noexcept
Constructor from non_owning_ptr convertible to the held type.
Definition non_owning_ptr.hpp:140
constexpr non_owning_ptr(U new_ptr) noexcept
Constructor from a raw pointer convertible to the held type.
Definition non_owning_ptr.hpp:157
T element_type
The type of the element pointed to.
Definition non_owning_ptr.hpp:94
constexpr auto operator++() -> non_owning_ptr &=delete
Pointer arithmetic is explicitly deleted.
T * pointer
The type of a pointer to T.
Definition non_owning_ptr.hpp:89
constexpr auto operator-(I const &) const -> non_owning_ptr=delete
Pointer arithmetic is explicitly deleted.
constexpr void reset(U new_ptr=nullptr)
Resets the held pointer to a new value.
Definition non_owning_ptr.hpp:182
constexpr auto operator[](I) const =delete
Pointer arithmetic is explicitly deleted.
ARENE_NODISCARD constexpr auto operator!() const noexcept -> bool
Boolean negation operator.
Definition non_owning_ptr.hpp:235
constexpr auto operator++(int) -> non_owning_ptr &=delete
Pointer arithmetic is explicitly deleted.
constexpr auto operator--(int) -> non_owning_ptr &=delete
Pointer arithmetic is explicitly deleted.
constexpr non_owning_ptr() noexcept=default
Default, trivial constructor.
static constexpr value_type value
Definition integral_constant.hpp:34
#define ARENE_PRECONDITION(condition)
Check that a precondition holds, and terminate the application if it does not.
Definition contract.hpp:150
Definition accumulate.hpp:37
constexpr bool is_instantiation_of_v
Definition is_instantiation_of.hpp:16
::std::nullptr_t nullptr_t
The type of nullptr.
Definition builtin_types.hpp:22
ARENE_NODISCARD constexpr auto make_non_owning_ptr(T ptr) noexcept
non_owning_ptr factory to allow template deduction.
Definition non_owning_ptr.hpp:310
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
constexpr bool is_non_owning_ptr_v
Trait that deduces if a type is an non_owning_ptr to any type.
Definition non_owning_ptr.hpp:49
Definition accumulate.hpp:36
STL namespace.
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
#define ARENE_NODISCARD
An alias for [[nodiscard]] , or a no-op if the attribute is not supported.
Definition nodiscard.hpp:26
constexpr auto operator()(::arene::base::non_owning_ptr< T > const &ptr) const noexcept -> size_t
Calculate the hash of the pointer.
Definition non_owning_ptr.hpp:329
hash function primary template
Definition hash.hpp:179