Embedded Template Library 1.0
Loading...
Searching...
No Matches
reference_counted_message.h
1/******************************************************************************
2The MIT License(MIT)
3
4Embedded Template Library.
5https://github.com/ETLCPP/etl
6https://www.etlcpp.com
7
8Copyright(c) 2021 John Wellbelove
9
10Permission is hereby granted, free of charge, to any person obtaining a copy
11of this software and associated documentation files(the "Software"), to deal
12in the Software without restriction, including without limitation the rights
13to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
14copies of the Software, and to permit persons to whom the Software is
15furnished to do so, subject to the following conditions :
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
23AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26SOFTWARE.
27******************************************************************************/
28
29#ifndef ETL_REFERENCE_COUNTED_MESSAGE_INCLUDED
30#define ETL_REFERENCE_COUNTED_MESSAGE_INCLUDED
31
32#include "platform.h"
33#include "atomic.h"
35#include "message.h"
36#include "reference_counted_object.h"
37#include "static_assert.h"
38#include "type_traits.h"
39
40#include <stdint.h>
41
42namespace etl
43{
44 //***************************************************************************
45 // Base class for all reference counted messages.
46 //***************************************************************************
48 {
49 public:
50
52 ETL_NODISCARD
53 virtual etl::imessage& get_message() = 0;
54 ETL_NODISCARD
55 virtual const etl::imessage& get_message() const = 0;
56 ETL_NODISCARD
59 ETL_NODISCARD
61 virtual void release() = 0;
62 };
63
64 //***************************************************************************
65 // Reference counted message with a counter and owner.
66 //***************************************************************************
67 template <typename TMessage, typename TCounter>
69 {
70 public:
71
72 ETL_STATIC_ASSERT((etl::is_base_of<etl::imessage, TMessage>::value), "Not a message type");
73
74 typedef TMessage message_type;
75 typedef TCounter counter_type;
76
77#if ETL_USING_CPP11
78 //***************************************************************************
82 //***************************************************************************
83 template <typename... TArgs>
85 : rc_object(etl::forward<TArgs>(args)...)
86 , owner(owner_)
87 {
88 }
89#endif
90
91 //***************************************************************************
95 //***************************************************************************
97 : rc_object(msg_)
98 , owner(owner_)
99 {
100 }
101
102 //***************************************************************************
105 //***************************************************************************
106 ETL_NODISCARD
107 virtual TMessage& get_message() ETL_OVERRIDE
108 {
109 return rc_object.get_object();
110 }
111
112 //***************************************************************************
115 //***************************************************************************
116 ETL_NODISCARD
117 virtual const TMessage& get_message() const ETL_OVERRIDE
118 {
119 return rc_object.get_object();
120 }
121
122 //***************************************************************************
125 //***************************************************************************
126 ETL_NODISCARD
128 {
129 return rc_object.get_reference_counter();
130 }
131
132 //***************************************************************************
135 //***************************************************************************
136 ETL_NODISCARD
137 virtual const etl::ireference_counter& get_reference_counter() const ETL_OVERRIDE
138 {
139 return rc_object.get_reference_counter();
140 }
141
142 //***************************************************************************
145 //***************************************************************************
146 virtual void release() ETL_OVERRIDE
147 {
148 owner.release(*this);
149 }
150
151 private:
152
155 };
156
157 //***************************************************************************
158 // A persistent message with no counter and owner.
159 //***************************************************************************
160 template <typename TMessage>
162 {
163 public:
164
165 ETL_STATIC_ASSERT((etl::is_base_of<etl::imessage, TMessage>::value), "Not a message type");
166
167 typedef TMessage message_type;
168 typedef void counter_type;
169
170 //***************************************************************************
173 //***************************************************************************
174 explicit persistent_message(const TMessage& msg_)
175 : rc_object(msg_)
176 {
177 }
178
179 //***************************************************************************
182 //***************************************************************************
183 ETL_NODISCARD
184 virtual TMessage& get_message() ETL_OVERRIDE
185 {
186 return rc_object.get_object();
187 }
188
189 //***************************************************************************
192 //***************************************************************************
193 ETL_NODISCARD
194 virtual const TMessage& get_message() const ETL_OVERRIDE
195 {
196 return rc_object.get_object();
197 }
198
199 //***************************************************************************
202 //***************************************************************************
203 ETL_NODISCARD
205 {
206 return rc_object.get_reference_counter();
207 }
208
209 //***************************************************************************
212 //***************************************************************************
213 ETL_NODISCARD
214 virtual const etl::ireference_counter& get_reference_counter() const ETL_OVERRIDE
215 {
216 return rc_object.get_reference_counter();
217 }
218
219 //***************************************************************************
222 //***************************************************************************
223 virtual void release() ETL_OVERRIDE
224 {
225 // Do nothing.
226 }
227
228 private:
229
231 };
232
233#if ETL_USING_CPP11 && ETL_HAS_ATOMIC
234 //***************************************************************************
237 //***************************************************************************
238 template <typename TMessage>
240#endif
241} // namespace etl
242
243#endif
Definition message.h:75
Interface for a reference counted message pool.
Definition ireference_counted_message_pool.h:44
Definition reference_counted_message.h:48
virtual ETL_NODISCARD const etl::imessage & get_message() const =0
Get a const reference to the message.
virtual ETL_NODISCARD etl::ireference_counter & get_reference_counter()=0
virtual ETL_NODISCARD etl::imessage & get_message()=0
Get a reference to the message.
virtual ETL_NODISCARD const etl::ireference_counter & get_reference_counter() const =0
Get a const reference to the reference counter.
virtual void release()=0
Release back to the owner.
The base of all reference counters.
Definition reference_counted_object.h:76
virtual ETL_NODISCARD etl::ireference_counter & get_reference_counter() ETL_OVERRIDE
Definition reference_counted_message.h:204
virtual ETL_NODISCARD const etl::ireference_counter & get_reference_counter() const ETL_OVERRIDE
Definition reference_counted_message.h:214
virtual ETL_NODISCARD TMessage & get_message() ETL_OVERRIDE
Definition reference_counted_message.h:184
virtual void release() ETL_OVERRIDE
Definition reference_counted_message.h:223
virtual ETL_NODISCARD const TMessage & get_message() const ETL_OVERRIDE
Definition reference_counted_message.h:194
persistent_message(const TMessage &msg_)
Definition reference_counted_message.h:174
Definition reference_counted_message.h:69
virtual ETL_NODISCARD etl::ireference_counter & get_reference_counter() ETL_OVERRIDE
Definition reference_counted_message.h:127
virtual ETL_NODISCARD const TMessage & get_message() const ETL_OVERRIDE
Definition reference_counted_message.h:117
virtual ETL_NODISCARD const etl::ireference_counter & get_reference_counter() const ETL_OVERRIDE
Definition reference_counted_message.h:137
virtual ETL_NODISCARD TMessage & get_message() ETL_OVERRIDE
Definition reference_counted_message.h:107
reference_counted_message(const TMessage &msg_, etl::ireference_counted_message_pool &owner_)
Definition reference_counted_message.h:96
virtual void release() ETL_OVERRIDE
Definition reference_counted_message.h:146
Definition reference_counted_object.h:217
bitset_ext
Definition absolute.h:40