Embedded Template Library 1.0
Loading...
Searching...
No Matches
reference_counted_object.h
1
2// The MIT License(MIT)
3//
4// Embedded Template Library.
5// https://github.com/ETLCPP/etl
6// https://www.etlcpp.com
7//
8// Copyright(c) 2021 John Wellbelove
9//
10// Permission is hereby granted, free of charge, to any person obtaining a copy
11// of this software and associated documentation files(the "Software"), to deal
12// in the Software without restriction, including without limitation the rights
13// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
14// copies of the Software, and to permit persons to whom the Software is
15// furnished to do so, subject to the following conditions :
16//
17// The above copyright notice and this permission notice shall be included in
18// all copies or substantial portions of the Software.
19//
20// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
23// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26// SOFTWARE.
27//******************************************************************************/
28
29#ifndef ETL_REFERENCE_COUNTED_OBJECT_INCLUDED
30#define ETL_REFERENCE_COUNTED_OBJECT_INCLUDED
31
32#include "platform.h"
33#include "atomic.h"
34#include "error_handler.h"
35#include "exception.h"
36#include "utility.h"
37
38#include <stdint.h>
39
40namespace etl
41{
42
43 //***************************************************************************
46 //***************************************************************************
47 class reference_counting_exception : public etl::exception
48 {
49 public:
50
51 reference_counting_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
52 : exception(reason_, file_name_, line_number_)
53 {
54 }
55 };
56
57 //***************************************************************************
60 //***************************************************************************
61 class reference_count_overrun : public etl::reference_counting_exception
62 {
63 public:
64
65 reference_count_overrun(string_type file_name_, numeric_type line_number_)
66 : etl::reference_counting_exception(ETL_ERROR_TEXT("reference_counting:overrun", ETL_REFERENCE_COUNTED_OBJECT_FILE_ID"A"), file_name_,
67 line_number_)
68 {
69 }
70 };
71
72 //***************************************************************************
74 //***************************************************************************
76 {
77 public:
78
79 virtual ~ireference_counter() {}
80 virtual void set_reference_count(int32_t value) = 0;
81 virtual void increment_reference_count() = 0;
82 ETL_NODISCARD
83 virtual int32_t decrement_reference_count() = 0;
84 ETL_NODISCARD
85 virtual int32_t get_reference_count() const = 0;
86 };
87
88 //***************************************************************************
90 //***************************************************************************
91 template <typename TCounter>
93 {
94 public:
95
96 //***************************************************************************
98 //***************************************************************************
100 : reference_count(0)
101 {
102 }
103
104 //***************************************************************************
106 //***************************************************************************
107 virtual void set_reference_count(int32_t value) ETL_OVERRIDE
108 {
109 reference_count = value;
110 }
111
112 //***************************************************************************
114 //***************************************************************************
115 virtual void increment_reference_count() ETL_OVERRIDE
116 {
117 ++reference_count;
118 }
119
120 //***************************************************************************
122 //***************************************************************************
123 ETL_NODISCARD
124 virtual int32_t decrement_reference_count() ETL_OVERRIDE
125 {
126 ETL_ASSERT(reference_count > 0, ETL_ERROR(reference_count_overrun));
127
128 return int32_t(--reference_count);
129 }
130
131 //***************************************************************************
133 //***************************************************************************
134 ETL_NODISCARD
135 virtual int32_t get_reference_count() const ETL_OVERRIDE
136 {
137 return int32_t(reference_count);
138 }
139
140 private:
141
142 TCounter reference_count; // The reference count object.
143 };
144
145 //***************************************************************************
147 //***************************************************************************
148 template <>
150 {
151 public:
152
153 //***************************************************************************
155 //***************************************************************************
157 {
158 // Do nothing.
159 }
160
161 //***************************************************************************
163 //***************************************************************************
164 virtual void set_reference_count(int32_t /*value*/) ETL_OVERRIDE
165 {
166 // Do nothing.
167 }
168
169 //***************************************************************************
171 //***************************************************************************
172 virtual void increment_reference_count() ETL_OVERRIDE
173 {
174 // Do nothing.
175 }
176
177 //***************************************************************************
179 //***************************************************************************
180 ETL_NODISCARD
181 virtual int32_t decrement_reference_count() ETL_OVERRIDE
182 {
183 return 1;
184 }
185
186 //***************************************************************************
188 //***************************************************************************
189 ETL_NODISCARD
190 virtual int32_t get_reference_count() const ETL_OVERRIDE
191 {
192 return 1;
193 }
194 };
195
196 //***************************************************************************
198 //***************************************************************************
200 {
201 public:
202
203 virtual ~ireference_counted_object() {}
204 ETL_NODISCARD
205 virtual ireference_counter& get_reference_counter() = 0;
206 ETL_NODISCARD
207 virtual const ireference_counter& get_reference_counter() const = 0;
208 };
209
210 //***************************************************************************
214 //***************************************************************************
215 template <typename TObject, typename TCounter>
217 {
218 public:
219
220 typedef TObject value_type;
221 typedef TCounter counter_type;
222
223 //***************************************************************************
225 //***************************************************************************
227
228 //***************************************************************************
230 //***************************************************************************
231 reference_counted_object(const TObject& object_)
232 : object(object_)
233 {
234 }
235
236#if ETL_USING_CPP11
237 //***************************************************************************
239 //***************************************************************************
240 template <typename... TArgs>
241 reference_counted_object(TArgs&&... args)
242 : object(etl::forward<TArgs>(args)...)
243 {
244 }
245#endif
246
247 //***************************************************************************
249 //***************************************************************************
250 ETL_NODISCARD
251 value_type& get_object()
252 {
253 return object;
254 }
255
256 //***************************************************************************
258 //***************************************************************************
259 ETL_NODISCARD
260 const value_type& get_object() const
261 {
262 return object;
263 }
264
265 //***************************************************************************
267 //***************************************************************************
268 ETL_NODISCARD
270 {
271 return reference_counter;
272 }
273
274 //***************************************************************************
276 //***************************************************************************
277 ETL_NODISCARD
278 virtual const ireference_counter& get_reference_counter() const ETL_OVERRIDE
279 {
280 return reference_counter;
281 }
282
283 private:
284
285 // This class must not be copy constructed or assigned.
287 reference_counted_object& operator=(const reference_counted_object&) ETL_DELETE;
288
289 TObject object;
291 };
292
293#if ETL_USING_CPP11 && ETL_HAS_ATOMIC
294 //***************************************************************************
297 //***************************************************************************
298 template <typename TObject>
300#endif
301} // namespace etl
302
303#endif
Base for all reference counted objects.
Definition reference_counted_object.h:200
The base of all reference counters.
Definition reference_counted_object.h:76
Definition reference_counted_object.h:62
Definition reference_counted_object.h:217
virtual ETL_NODISCARD ireference_counter & get_reference_counter() ETL_OVERRIDE
Get a reference to the reference counter.
Definition reference_counted_object.h:269
reference_counted_object()
Constructor.
Definition reference_counted_object.h:226
ETL_NODISCARD value_type & get_object()
Get a reference to the counted object.
Definition reference_counted_object.h:251
reference_counted_object(const TObject &object_)
Constructor.
Definition reference_counted_object.h:231
virtual ETL_NODISCARD const ireference_counter & get_reference_counter() const ETL_OVERRIDE
Get a const reference to the reference counter.
Definition reference_counted_object.h:278
ETL_NODISCARD const value_type & get_object() const
Get a const reference to the counted object.
Definition reference_counted_object.h:260
reference_counter()
Constructor.
Definition reference_counted_object.h:156
virtual void set_reference_count(int32_t) ETL_OVERRIDE
Set the reference count.
Definition reference_counted_object.h:164
virtual ETL_NODISCARD int32_t get_reference_count() const ETL_OVERRIDE
Get the current reference count.
Definition reference_counted_object.h:190
virtual void increment_reference_count() ETL_OVERRIDE
Increment the reference count.
Definition reference_counted_object.h:172
virtual ETL_NODISCARD int32_t decrement_reference_count() ETL_OVERRIDE
Decrement the reference count.
Definition reference_counted_object.h:181
A specific type of reference counter.
Definition reference_counted_object.h:93
virtual ETL_NODISCARD int32_t get_reference_count() const ETL_OVERRIDE
Get the current reference count.
Definition reference_counted_object.h:135
virtual void increment_reference_count() ETL_OVERRIDE
Increment the reference count.
Definition reference_counted_object.h:115
reference_counter()
Constructor.
Definition reference_counted_object.h:99
virtual void set_reference_count(int32_t value) ETL_OVERRIDE
Set the reference count.
Definition reference_counted_object.h:107
virtual ETL_NODISCARD int32_t decrement_reference_count() ETL_OVERRIDE
Decrement the reference count.
Definition reference_counted_object.h:124
Definition reference_counted_object.h:48
#define ETL_ASSERT(b, e)
Definition error_handler.h:511
ETL_EXCEPTION_CONSTEXPR exception(string_type reason_, string_type, numeric_type)
Constructor.
Definition exception.h:81
Definition exception.h:59
bitset_ext
Definition absolute.h:40