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 all
18//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 "exception.h"
35#include "error_handler.h"
36#include "utility.h"
37
38#include <stdint.h>
39
40namespace etl
41{
42
43 //***************************************************************************
46 //***************************************************************************
55
56 //***************************************************************************
59 //***************************************************************************
61 {
62 public:
64 : etl::reference_counting_exception(ETL_ERROR_TEXT("reference_counting:overrun", ETL_REFERENCE_COUNTED_OBJECT_FILE_ID"A"), file_name_, line_number_)
65 {
66 }
67 };
68
69 //***************************************************************************
71 //***************************************************************************
73 {
74 public:
75
76 virtual ~ireference_counter() {};
77 virtual void set_reference_count(int32_t value) = 0;
78 virtual void increment_reference_count() = 0;
79 ETL_NODISCARD virtual int32_t decrement_reference_count() = 0;
80 ETL_NODISCARD virtual int32_t get_reference_count() const = 0;
81 };
82
83 //***************************************************************************
85 //***************************************************************************
86 template <typename TCounter>
88 {
89 public:
90
91 //***************************************************************************
93 //***************************************************************************
95 : reference_count(0)
96 {
97 }
98
99 //***************************************************************************
101 //***************************************************************************
102 virtual void set_reference_count(int32_t value) ETL_OVERRIDE
103 {
104 reference_count = value;
105 }
106
107 //***************************************************************************
109 //***************************************************************************
110 virtual void increment_reference_count() ETL_OVERRIDE
111 {
112 ++reference_count;
113 }
114
115 //***************************************************************************
117 //***************************************************************************
118 ETL_NODISCARD virtual int32_t decrement_reference_count() ETL_OVERRIDE
119 {
120 ETL_ASSERT(reference_count > 0, ETL_ERROR(reference_count_overrun));
121
122 return int32_t(--reference_count);
123 }
124
125 //***************************************************************************
127 //***************************************************************************
128 ETL_NODISCARD virtual int32_t get_reference_count() const ETL_OVERRIDE
129 {
130 return int32_t(reference_count);
131 }
132
133 private:
134
135 TCounter reference_count; // The reference count object.
136 };
137
138 //***************************************************************************
140 //***************************************************************************
141 template <>
143 {
144 public:
145
146 //***************************************************************************
148 //***************************************************************************
150 {
151 // Do nothing.
152 }
153
154 //***************************************************************************
156 //***************************************************************************
157 virtual void set_reference_count(int32_t /*value*/) ETL_OVERRIDE
158 {
159 // Do nothing.
160 }
161
162 //***************************************************************************
164 //***************************************************************************
165 virtual void increment_reference_count() ETL_OVERRIDE
166 {
167 // Do nothing.
168 }
169
170 //***************************************************************************
172 //***************************************************************************
173 ETL_NODISCARD virtual int32_t decrement_reference_count() ETL_OVERRIDE
174 {
175 return 1;
176 }
177
178 //***************************************************************************
180 //***************************************************************************
181 ETL_NODISCARD virtual int32_t get_reference_count() const ETL_OVERRIDE
182 {
183 return 1;
184 }
185 };
186
187 //***************************************************************************
189 //***************************************************************************
191 {
192 public:
193
194 virtual ~ireference_counted_object() {}
195 ETL_NODISCARD virtual ireference_counter& get_reference_counter() = 0;
196 ETL_NODISCARD virtual const ireference_counter& get_reference_counter() const = 0;
197 };
198
199 //***************************************************************************
203 //***************************************************************************
204 template <typename TObject, typename TCounter>
206 {
207 public:
208
209 typedef TObject value_type;
210 typedef TCounter counter_type;
211
212 //***************************************************************************
214 //***************************************************************************
218
219 //***************************************************************************
221 //***************************************************************************
223 : object(object_)
224 {
225 }
226
227#if ETL_USING_CPP11
228 //***************************************************************************
230 //***************************************************************************
231 template <typename... TArgs>
233 : object(etl::forward<TArgs>(args)...)
234 {
235 }
236#endif
237
238 //***************************************************************************
240 //***************************************************************************
241 ETL_NODISCARD value_type& get_object()
242 {
243 return object;
244 }
245
246
247 //***************************************************************************
249 //***************************************************************************
250 ETL_NODISCARD const value_type& get_object() const
251 {
252 return object;
253 }
254
255 //***************************************************************************
257 //***************************************************************************
258 ETL_NODISCARD virtual ireference_counter& get_reference_counter() ETL_OVERRIDE
259 {
260 return reference_counter;
261 }
262
263 //***************************************************************************
265 //***************************************************************************
266 ETL_NODISCARD virtual const ireference_counter& get_reference_counter() const ETL_OVERRIDE
267 {
268 return reference_counter;
269 }
270
271 private:
272
273 // This class must not be copy constructed or assigned.
276
277 TObject object;
279 };
280
281#if ETL_USING_CPP11 && ETL_HAS_ATOMIC
282 //***************************************************************************
285 //***************************************************************************
286 template <typename TObject>
288#endif
289}
290
291#endif
Base for all reference counted objects.
Definition reference_counted_object.h:191
The base of all reference counters.
Definition reference_counted_object.h:73
Definition reference_counted_object.h:61
Definition reference_counted_object.h:206
virtual ETL_NODISCARD ireference_counter & get_reference_counter() ETL_OVERRIDE
Get a reference to the reference counter.
Definition reference_counted_object.h:258
reference_counted_object()
Constructor.
Definition reference_counted_object.h:215
ETL_NODISCARD value_type & get_object()
Get a reference to the counted object.
Definition reference_counted_object.h:241
reference_counted_object(const TObject &object_)
Constructor.
Definition reference_counted_object.h:222
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:266
ETL_NODISCARD const value_type & get_object() const
Get a const reference to the counted object.
Definition reference_counted_object.h:250
reference_counter()
Constructor.
Definition reference_counted_object.h:149
virtual void set_reference_count(int32_t) ETL_OVERRIDE
Set the reference count.
Definition reference_counted_object.h:157
virtual ETL_NODISCARD int32_t get_reference_count() const ETL_OVERRIDE
Get the current reference count.
Definition reference_counted_object.h:181
virtual void increment_reference_count() ETL_OVERRIDE
Increment the reference count.
Definition reference_counted_object.h:165
virtual ETL_NODISCARD int32_t decrement_reference_count() ETL_OVERRIDE
Decrement the reference count.
Definition reference_counted_object.h:173
A specific type of reference counter.
Definition reference_counted_object.h:88
virtual ETL_NODISCARD int32_t get_reference_count() const ETL_OVERRIDE
Get the current reference count.
Definition reference_counted_object.h:128
virtual void increment_reference_count() ETL_OVERRIDE
Increment the reference count.
Definition reference_counted_object.h:110
reference_counter()
Constructor.
Definition reference_counted_object.h:94
virtual void set_reference_count(int32_t value) ETL_OVERRIDE
Set the reference count.
Definition reference_counted_object.h:102
virtual ETL_NODISCARD int32_t decrement_reference_count() ETL_OVERRIDE
Decrement the reference count.
Definition reference_counted_object.h:118
Definition reference_counted_object.h:48
#define ETL_ASSERT(b, e)
Definition error_handler.h:356
Definition exception.h:47
bitset_ext
Definition absolute.h:38
pair holds two objects of arbitrary type
Definition utility.h:164