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 "message.h"
34#include "atomic.h"
35#include "reference_counted_object.h"
36#include "static_assert.h"
37#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 virtual etl::imessage& get_message() = 0;
53 ETL_NODISCARD virtual const etl::imessage& get_message() const = 0;
54 ETL_NODISCARD virtual etl::ireference_counter& get_reference_counter() = 0;
55 ETL_NODISCARD virtual const etl::ireference_counter& get_reference_counter() const = 0;
56 virtual void release() = 0;
57 };
58
59 //***************************************************************************
60 // Reference counted message with a counter and owner.
61 //***************************************************************************
62 template <typename TMessage, typename TCounter>
64 {
65 public:
66
67 ETL_STATIC_ASSERT((etl::is_base_of<etl::imessage, TMessage>::value), "Not a message type");
68
69 typedef TMessage message_type;
70 typedef TCounter counter_type;
71
72#if ETL_USING_CPP11
73 //***************************************************************************
77 //***************************************************************************
78 template <typename... TArgs>
80 : rc_object(etl::forward<TArgs>(args)...)
81 , owner(owner_)
82 {
83 }
84#endif
85
86 //***************************************************************************
90 //***************************************************************************
96
97 //***************************************************************************
100 //***************************************************************************
101 ETL_NODISCARD virtual TMessage& get_message() ETL_OVERRIDE
102 {
103 return rc_object.get_object();
104 }
105
106 //***************************************************************************
109 //***************************************************************************
110 ETL_NODISCARD virtual const TMessage& get_message() const ETL_OVERRIDE
111 {
112 return rc_object.get_object();
113 }
114
115 //***************************************************************************
118 //***************************************************************************
119 ETL_NODISCARD virtual etl::ireference_counter& get_reference_counter() ETL_OVERRIDE
120 {
121 return rc_object.get_reference_counter();
122 }
123
124 //***************************************************************************
127 //***************************************************************************
128 ETL_NODISCARD virtual const etl::ireference_counter& get_reference_counter() const ETL_OVERRIDE
129 {
130 return rc_object.get_reference_counter();
131 }
132
133 //***************************************************************************
136 //***************************************************************************
137 virtual void release() ETL_OVERRIDE
138 {
139 owner.release(*this);
140 }
141
142 private:
143
146 };
147
148 //***************************************************************************
149 // A persistent message with no counter and owner.
150 //***************************************************************************
151 template <typename TMessage>
153 {
154 public:
155
156 ETL_STATIC_ASSERT((etl::is_base_of<etl::imessage, TMessage>::value), "Not a message type");
157
158 typedef TMessage message_type;
159 typedef void counter_type;
160
161 //***************************************************************************
164 //***************************************************************************
166 : rc_object(msg_)
167 {
168 }
169
170 //***************************************************************************
173 //***************************************************************************
174 ETL_NODISCARD virtual TMessage& get_message() ETL_OVERRIDE
175 {
176 return rc_object.get_object();
177 }
178
179 //***************************************************************************
182 //***************************************************************************
183 ETL_NODISCARD virtual const TMessage& get_message() const ETL_OVERRIDE
184 {
185 return rc_object.get_object();
186 }
187
188 //***************************************************************************
191 //***************************************************************************
192 ETL_NODISCARD virtual etl::ireference_counter& get_reference_counter() ETL_OVERRIDE
193 {
194 return rc_object.get_reference_counter();
195 }
196
197 //***************************************************************************
200 //***************************************************************************
201 ETL_NODISCARD virtual const etl::ireference_counter& get_reference_counter() const ETL_OVERRIDE
202 {
203 return rc_object.get_reference_counter();
204 }
205
206 //***************************************************************************
209 //***************************************************************************
210 virtual void release() ETL_OVERRIDE
211 {
212 // Do nothing.
213 }
214
215 private:
216
218 };
219
220#if ETL_USING_CPP11 && ETL_HAS_ATOMIC
221 //***************************************************************************
224 //***************************************************************************
225 template <typename TMessage>
227#endif
228}
229
230#endif
Definition message.h:73
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
Get a reference to the reference counter.
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:73
Definition reference_counted_message.h:153
virtual ETL_NODISCARD etl::ireference_counter & get_reference_counter() ETL_OVERRIDE
Definition reference_counted_message.h:192
virtual ETL_NODISCARD const etl::ireference_counter & get_reference_counter() const ETL_OVERRIDE
Definition reference_counted_message.h:201
virtual ETL_NODISCARD TMessage & get_message() ETL_OVERRIDE
Definition reference_counted_message.h:174
virtual void release() ETL_OVERRIDE
Definition reference_counted_message.h:210
virtual ETL_NODISCARD const TMessage & get_message() const ETL_OVERRIDE
Definition reference_counted_message.h:183
persistent_message(const TMessage &msg_)
Definition reference_counted_message.h:165
Definition reference_counted_message.h:64
virtual ETL_NODISCARD etl::ireference_counter & get_reference_counter() ETL_OVERRIDE
Definition reference_counted_message.h:119
virtual ETL_NODISCARD const TMessage & get_message() const ETL_OVERRIDE
Definition reference_counted_message.h:110
virtual ETL_NODISCARD const etl::ireference_counter & get_reference_counter() const ETL_OVERRIDE
Definition reference_counted_message.h:128
virtual ETL_NODISCARD TMessage & get_message() ETL_OVERRIDE
Definition reference_counted_message.h:101
reference_counted_message(const TMessage &msg_, etl::ireference_counted_message_pool &owner_)
Definition reference_counted_message.h:91
virtual void release() ETL_OVERRIDE
Definition reference_counted_message.h:137
virtual ETL_NODISCARD ireference_counter & get_reference_counter() ETL_OVERRIDE
Get a reference to the reference counter.
Definition reference_counted_object.h:258
ETL_NODISCARD value_type & get_object()
Get a reference to the counted object.
Definition reference_counted_object.h:241
is_base_of
Definition type_traits_generator.h:1252
bitset_ext
Definition absolute.h:38
pair holds two objects of arbitrary type
Definition utility.h:164