Embedded Template Library 1.0
Loading...
Searching...
No Matches
shared_message.h
Go to the documentation of this file.
1
2
3/******************************************************************************
4The MIT License(MIT)
5
6Embedded Template Library.
7https://github.com/ETLCPP/etl
8https://www.etlcpp.com
9
10Copyright(c) 2020 John Wellbelove
11
12Permission is hereby granted, free of charge, to any person obtaining a copy
13of this software and associated documentation files(the "Software"), to deal
14in the Software without restriction, including without limitation the rights
15to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16copies of the Software, and to permit persons to whom the Software is
17furnished to do so, subject to the following conditions :
18
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28SOFTWARE.
29******************************************************************************/
30
31#ifndef ETL_SHARED_MESSAGE_INCLUDED
32#define ETL_SHARED_MESSAGE_INCLUDED
33
34#include "platform.h"
35#include "utility.h"
36#include "reference_counted_message.h"
38#include "message.h"
39#include "type_traits.h"
40#include "static_assert.h"
41
42//*****************************************************************************
45//*****************************************************************************
46namespace etl
47{
49 {
50 public:
51
52#if ETL_USING_CPP11
53 //*************************************************************************
55 //*************************************************************************
56 template <typename TMessage, typename TPool, typename... TArgs>
57 static shared_message create(TPool& owner, TArgs&&... args)
58 {
60 }
61#endif
62
63 //*************************************************************************
65 //*************************************************************************
66 template <typename TPool, typename TMessage>
68 {
69 ETL_STATIC_ASSERT((etl::is_base_of<etl::ireference_counted_message_pool, TPool>::value), "TPool not derived from etl::ireference_counted_message_pool");
70 ETL_STATIC_ASSERT((etl::is_base_of<etl::imessage, TMessage>::value), "TMessage not derived from etl::imessage");
71
72 p_rcmessage = owner.allocate(message);
73
74 if (p_rcmessage != ETL_NULLPTR)
75 {
76 p_rcmessage->get_reference_counter().set_reference_count(1U);
77 }
78 }
79
80#if ETL_USING_CPP11
81 //*************************************************************************
83 //*************************************************************************
84 template <typename TPool, typename TMessage, typename... TArgs>
86 {
87 ETL_STATIC_ASSERT((etl::is_base_of<etl::ireference_counted_message_pool, TPool>::value), "TPool not derived from etl::ireference_counted_message_pool");
88 ETL_STATIC_ASSERT((etl::is_base_of<etl::imessage, TMessage>::value), "TMessage not derived from etl::imessage");
89
90 p_rcmessage = owner.template allocate<TMessage>(etl::forward<TArgs>(args)...);
91
92 if (p_rcmessage != ETL_NULLPTR)
93 {
94 p_rcmessage->get_reference_counter().set_reference_count(1U);
95 }
96 }
97#endif
98
99 //*************************************************************************
101 //*************************************************************************
103 {
104 p_rcmessage = &rcm;
105
106 p_rcmessage->get_reference_counter().set_reference_count(1U);
107 }
108
109 //*************************************************************************
111 //*************************************************************************
113 : p_rcmessage(other.p_rcmessage)
114 {
115 p_rcmessage->get_reference_counter().increment_reference_count();
116 }
117
118#if ETL_USING_CPP11
119 //*************************************************************************
121 //*************************************************************************
123 : p_rcmessage(etl::move(other.p_rcmessage))
124 {
125 other.p_rcmessage = ETL_NULLPTR;
126 }
127#endif
128
129 //*************************************************************************
131 //*************************************************************************
133 {
134 if (&other != this)
135 {
136 // Deal with the current message.
137 if (p_rcmessage->get_reference_counter().decrement_reference_count() == 0U)
138 {
139 p_rcmessage->release();
140 }
141
142 // Copy over the new one.
143 p_rcmessage = other.p_rcmessage;
144 p_rcmessage->get_reference_counter().increment_reference_count();
145 }
146
147 return *this;
148 }
149
150#if ETL_USING_CPP11
151 //*************************************************************************
153 //*************************************************************************
155 {
156 if (&other != this)
157 {
158 // Deal with the current message.
159 if (p_rcmessage->get_reference_counter().decrement_reference_count() == 0U)
160 {
161 p_rcmessage->release();
162 }
163
164 // Move over the new one.
165 p_rcmessage = etl::move(other.p_rcmessage);
166 other.p_rcmessage = ETL_NULLPTR;
167 }
168
169 return *this;
170 }
171#endif
172
173 //*************************************************************************
176 //*************************************************************************
178 {
179 if ((p_rcmessage != ETL_NULLPTR) &&
180 (p_rcmessage->get_reference_counter().decrement_reference_count() == 0U))
181 {
182 p_rcmessage->release();
183 }
184 }
185
186 //*************************************************************************
188 //***********************************************************************
189 ETL_NODISCARD etl::imessage& get_message()
190 {
191 return p_rcmessage->get_message();
192 }
193
194 //*************************************************************************
196 //*************************************************************************
197 ETL_NODISCARD const etl::imessage& get_message() const
198 {
199 return p_rcmessage->get_message();
200 }
201
202 //*************************************************************************
204 //*************************************************************************
205 ETL_NODISCARD uint32_t get_reference_count() const
206 {
207 return p_rcmessage->get_reference_counter().get_reference_count();
208 }
209
210 //*************************************************************************
212 //*************************************************************************
213 ETL_NODISCARD bool is_valid() const
214 {
215 return p_rcmessage != ETL_NULLPTR;
216 }
217
218 private:
219
220 shared_message() ETL_DELETE;
221
222 etl::ireference_counted_message* p_rcmessage;
223 };
224}
225
226#endif
Definition message.h:73
Definition reference_counted_message.h:48
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 void release()=0
Release back to the owner.
Definition message.h:91
Definition shared_message.h:49
ETL_NODISCARD bool is_valid() const
Checks if the shared message is valid.
Definition shared_message.h:213
ETL_NODISCARD uint32_t get_reference_count() const
Get the current reference count for this shared message.
Definition shared_message.h:205
ETL_NODISCARD etl::imessage & get_message()
Get a reference to the contained message.
Definition shared_message.h:189
shared_message(const etl::shared_message &other)
Copy constructor.
Definition shared_message.h:112
~shared_message()
Definition shared_message.h:177
ETL_NODISCARD const etl::imessage & get_message() const
Get a const reference to the contained message.
Definition shared_message.h:197
shared_message(etl::ireference_counted_message &rcm)
Constructor.
Definition shared_message.h:102
shared_message(TPool &owner, const TMessage &message)
Constructor.
Definition shared_message.h:67
shared_message & operator=(const etl::shared_message &other)
Copy assignment operator.
Definition shared_message.h:132
is_base_of
Definition type_traits_generator.h:1247
bitset_ext
Definition absolute.h:38
T * create(Args &&... args)
Creates the object from a type. Variadic parameter constructor.
Definition variant_pool_generator.h:348
pair holds two objects of arbitrary type
Definition utility.h:164