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