Embedded Template Library 1.0
Loading...
Searching...
No Matches
not_null.h
Go to the documentation of this file.
1
2
3/******************************************************************************
4The MIT License(MIT)
5
6Embedded Template Library.
7https://github.com/ETLCPP/etl
8https://www.etlcpp.com
9
10Copyright(c) 2025 John Wellbelove
11
12Permission is hereby granted, free of charge, to any person obtaining a copy
13of this software and associated documentation files(the "Software"), to deal
14in the Software without restriction, including without limitation the rights
15to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16copies of the Software, and to permit persons to whom the Software is
17furnished to do so, subject to the following conditions :
18
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28SOFTWARE.
29******************************************************************************/
30
31#ifndef ETL_NOT_NULL_INCLUDED
32#define ETL_NOT_NULL_INCLUDED
33
34#include "platform.h"
35#include "error_handler.h"
36#include "exception.h"
37#include "static_assert.h"
38#include "memory.h"
39#include "type_traits.h"
40
41namespace etl
42{
43 //***************************************************************************
45 //***************************************************************************
46 class not_null_exception : public exception
47 {
48 public:
49
50 not_null_exception(string_type reason_, string_type file_name_, numeric_type line_number_) ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS)
51 : exception(reason_, file_name_, line_number_)
52 {
53 }
54 };
55
56 //***************************************************************************
58 //***************************************************************************
59 class not_null_contains_null : public not_null_exception
60 {
61 public:
62
63 not_null_contains_null(string_type file_name_, numeric_type line_number_) ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS)
64 : not_null_exception(ETL_ERROR_TEXT("not_null:contains null", ETL_NOT_NULL_FILE_ID"A"), file_name_, line_number_)
65 {
66 }
67 };
68
69 //***************************************************************************
70 // not_null
71 // Primary template
72 //***************************************************************************
73 template <typename T>
74 class not_null;
75
76 //***************************************************************************
77 // Specialisation for T*
78 // A container for pointers that are not allowed to be null.
79 //***************************************************************************
80 template <typename T>
81 class not_null<T*>
82 {
83 public:
84
85 typedef T value_type;
86 typedef T* pointer;
87 typedef const T* const_pointer;
88 typedef T& reference;
89 typedef const T& const_reference;
90 typedef pointer underlying_type;
91
92 //*********************************
95 //*********************************
96 ETL_CONSTEXPR14 explicit not_null(underlying_type ptr_) ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS)
97 : ptr(ptr_)
98 {
99 ETL_ASSERT(ptr_ != ETL_NULLPTR, ETL_ERROR(not_null_contains_null));
100 }
101
102 //*********************************
104 //*********************************
105 ETL_CONSTEXPR14 not_null(const etl::not_null<T*>& other) ETL_NOEXCEPT
106 : ptr(other.get())
107 {
108 }
109
110 //*********************************
113 //*********************************
114 ETL_CONSTEXPR14 not_null& operator =(underlying_type rhs) ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS)
115 {
116 ETL_ASSERT_OR_RETURN_VALUE(rhs != ETL_NULLPTR, ETL_ERROR(not_null_contains_null), *this);
117
118 ptr = rhs;
119
120 return *this;
121 }
122
123 //*********************************
125 //*********************************
126 ETL_CONSTEXPR14 not_null& operator =(const etl::not_null<T*>& rhs) ETL_NOEXCEPT
127 {
128 ptr = rhs.get();
129
130 return *this;
131 }
132
133 //*********************************
135 //*********************************
136 ETL_CONSTEXPR14 pointer get() const ETL_NOEXCEPT
137 {
138 return ptr;
139 }
140
141 //*********************************
143 //*********************************
144 ETL_CONSTEXPR14 operator pointer() const ETL_NOEXCEPT
145 {
146 return ptr;
147 }
148
149 //*********************************
151 //*********************************
152 ETL_CONSTEXPR14 reference operator*() const ETL_NOEXCEPT
153 {
154 return *ptr;
155 }
156
157 //*********************************
159 //*********************************
160 ETL_CONSTEXPR14 pointer operator->() const ETL_NOEXCEPT
161 {
162 return ptr;
163 }
164
165 private:
166
168 pointer ptr;
169 };
170
171 //***************************************************************************
172 // Partial specialisation for etl::unique_ptr
173 // A container for unique_ptr that are not allowed to be null.
174 //***************************************************************************
175 template <typename T, typename TDeleter>
176 class not_null<etl::unique_ptr<T, TDeleter> >
177 {
178 private:
179
181 typedef etl::unique_ptr<T, TDeleter> underlying_type;
182
183 public:
184
185 typedef T value_type;
186 typedef T* pointer;
187 typedef const T* const_pointer;
188 typedef T& reference;
189 typedef const T& const_reference;
190
191#if ETL_USING_CPP11
192 //*********************************
196 //*********************************
197 ETL_CONSTEXPR14 explicit not_null(underlying_type&& u_ptr_) ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS)
198 : u_ptr(etl::move(u_ptr_))
199 {
200 ETL_ASSERT(u_ptr.get() != ETL_NULLPTR, ETL_ERROR(not_null_contains_null));
201 }
202
203 //*********************************
207 //*********************************
208 ETL_CONSTEXPR14 not_null& operator =(underlying_type&& rhs) ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS)
209 {
210 ETL_ASSERT_OR_RETURN_VALUE(rhs.get() != ETL_NULLPTR, ETL_ERROR(not_null_contains_null), *this);
211
212 u_ptr = etl::move(rhs);
213
214 return *this;
215 }
216#endif
217
218 //*********************************
220 //*********************************
221 ETL_CONSTEXPR14 pointer get() const ETL_NOEXCEPT
222 {
223 return u_ptr.get();
224 }
225
226 //*********************************
228 //*********************************
229 ETL_CONSTEXPR14 operator pointer() const ETL_NOEXCEPT
230 {
231 return u_ptr.get();
232 }
233
234 //*********************************
236 //*********************************
237 ETL_CONSTEXPR14 reference operator*() const ETL_NOEXCEPT
238 {
239 return *u_ptr;
240 }
241
242 //*********************************
244 //*********************************
245 ETL_CONSTEXPR14 pointer operator->() const ETL_NOEXCEPT
246 {
247 return u_ptr.get();
248 }
249
250 private:
251
252 ETL_CONSTEXPR14 explicit not_null(const this_type& u_ptr_) ETL_NOEXCEPT ETL_DELETE;
253 ETL_CONSTEXPR14 not_null& operator=(const this_type& rhs) ETL_NOEXCEPT ETL_DELETE;
254
255#if ETL_USING_CPP11
256 ETL_CONSTEXPR14 explicit not_null(this_type&& u_ptr_) ETL_NOEXCEPT = delete;
257 ETL_CONSTEXPR14 not_null& operator=(this_type&& rhs) ETL_NOEXCEPT = delete;
258#endif
259
261 underlying_type u_ptr;
262 };
263}
264
265#endif
ETL_CONSTEXPR14 not_null(underlying_type ptr_) ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS)
Definition not_null.h:96
ETL_CONSTEXPR14 not_null(const etl::not_null< T * > &other) ETL_NOEXCEPT
Copy construct from a not_null pointer.
Definition not_null.h:105
ETL_CONSTEXPR14 pointer get() const ETL_NOEXCEPT
Gets the underlying pointer.
Definition not_null.h:136
ETL_CONSTEXPR14 pointer operator->() const ETL_NOEXCEPT
Arrow operator.
Definition not_null.h:160
ETL_CONSTEXPR14 reference operator*() const ETL_NOEXCEPT
Dereference operator.
Definition not_null.h:152
ETL_CONSTEXPR14 pointer operator->() const ETL_NOEXCEPT
Arrow operator.
Definition not_null.h:245
ETL_CONSTEXPR14 pointer get() const ETL_NOEXCEPT
Gets the underlying ptr.
Definition not_null.h:221
ETL_CONSTEXPR14 reference operator*() const ETL_NOEXCEPT
Dereference operator.
Definition not_null.h:237
The exception when the not_null contains a null.
Definition not_null.h:60
Definition not_null.h:74
#define ETL_ASSERT(b, e)
Definition error_handler.h:356
ETL_CONSTEXPR exception(string_type reason_, string_type, numeric_type line_)
Constructor.
Definition exception.h:69
Definition memory.h:1314
bitset_ext
Definition absolute.h:39
Primary template for etl::underlying_type Users must specialise this template for their enumerations.
Definition type_traits_generator.h:2472