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"
36#include "message.h"
37#include "reference_counted_message.h"
38#include "static_assert.h"
39#include "type_traits.h"
40#include "utility.h"
41
42//*****************************************************************************
46//*****************************************************************************
47namespace etl
48{
50 {
51 public:
52
53#if ETL_USING_CPP11
54 //*************************************************************************
56 //*************************************************************************
57 template <typename TMessage, typename TPool, typename... TArgs>
58 static shared_message create(TPool& owner, TArgs&&... args)
59 {
60 return shared_message(owner, etl::in_place_type_t<TMessage>(), etl::forward<TArgs>(args)...);
61 }
62#endif
63
64 //*************************************************************************
66 //*************************************************************************
67 template <typename TPool, typename TMessage>
68 shared_message(TPool& owner, const TMessage& message)
69 {
70 ETL_STATIC_ASSERT((etl::is_base_of<etl::ireference_counted_message_pool, TPool>::value),
71 "TPool not derived from etl::ireference_counted_message_pool");
72 ETL_STATIC_ASSERT((etl::is_base_of<etl::imessage, TMessage>::value), "TMessage not derived from etl::imessage");
73
74 p_rcmessage = owner.allocate(message);
75
76 if (p_rcmessage != ETL_NULLPTR)
77 {
78 p_rcmessage->get_reference_counter().set_reference_count(1U);
79 }
80 }
81
82#if ETL_USING_CPP11
83 //*************************************************************************
85 //*************************************************************************
86 template <typename TPool, typename TMessage, typename... TArgs>
87 shared_message(TPool& owner, etl::in_place_type_t<TMessage>, TArgs&&... args)
88 {
89 ETL_STATIC_ASSERT((etl::is_base_of<etl::ireference_counted_message_pool, TPool>::value),
90 "TPool not derived from etl::ireference_counted_message_pool");
91 ETL_STATIC_ASSERT((etl::is_base_of<etl::imessage, TMessage>::value), "TMessage not derived from etl::imessage");
92
93 p_rcmessage = owner.template allocate<TMessage>(etl::forward<TArgs>(args)...);
94
95 if (p_rcmessage != ETL_NULLPTR)
96 {
97 p_rcmessage->get_reference_counter().set_reference_count(1U);
98 }
99 }
100#endif
101
102 //*************************************************************************
104 //*************************************************************************
106 {
107 p_rcmessage = &rcm;
108
109 p_rcmessage->get_reference_counter().set_reference_count(1U);
110 }
111
112 //*************************************************************************
114 //*************************************************************************
116 : p_rcmessage(other.p_rcmessage)
117 {
118 p_rcmessage->get_reference_counter().increment_reference_count();
119 }
120
121#if ETL_USING_CPP11
122 //*************************************************************************
124 //*************************************************************************
125 shared_message(etl::shared_message&& other) ETL_NOEXCEPT
126 : p_rcmessage(etl::move(other.p_rcmessage))
127 {
128 other.p_rcmessage = ETL_NULLPTR;
129 }
130#endif
131
132 //*************************************************************************
134 //*************************************************************************
136 {
137 if (&other != this)
138 {
139 // Deal with the current message.
140 if (p_rcmessage->get_reference_counter().decrement_reference_count() == 0U)
141 {
142 p_rcmessage->release();
143 }
144
145 // Copy over the new one.
146 p_rcmessage = other.p_rcmessage;
147 p_rcmessage->get_reference_counter().increment_reference_count();
148 }
149
150 return *this;
151 }
152
153#if ETL_USING_CPP11
154 //*************************************************************************
156 //*************************************************************************
157 shared_message& operator=(etl::shared_message&& other) ETL_NOEXCEPT
158 {
159 if (&other != this)
160 {
161 // Deal with the current message.
162 if (p_rcmessage->get_reference_counter().decrement_reference_count() == 0U)
163 {
164 p_rcmessage->release();
165 }
166
167 // Move over the new one.
168 p_rcmessage = etl::move(other.p_rcmessage);
169 other.p_rcmessage = ETL_NULLPTR;
170 }
171
172 return *this;
173 }
174#endif
175
176 //*************************************************************************
179 //*************************************************************************
181 {
182 if ((p_rcmessage != ETL_NULLPTR) && (p_rcmessage->get_reference_counter().decrement_reference_count() == 0U))
183 {
184 p_rcmessage->release();
185 }
186 }
187
188 //*************************************************************************
190 //***********************************************************************
191 ETL_NODISCARD
193 {
194 return p_rcmessage->get_message();
195 }
196
197 //*************************************************************************
199 //*************************************************************************
200 ETL_NODISCARD
202 {
203 return p_rcmessage->get_message();
204 }
205
206 //*************************************************************************
208 //*************************************************************************
209 ETL_NODISCARD
210 uint32_t get_reference_count() const
211 {
212 return static_cast<uint32_t>(p_rcmessage->get_reference_counter().get_reference_count());
213 }
214
215 //*************************************************************************
217 //*************************************************************************
218 ETL_NODISCARD
219 bool is_valid() const
220 {
221 return p_rcmessage != ETL_NULLPTR;
222 }
223
224 private:
225
226 shared_message() ETL_DELETE;
227
228 etl::ireference_counted_message* p_rcmessage;
229 };
230} // namespace etl
231
232#endif
Definition message.h:75
Definition reference_counted_message.h:48
virtual ETL_NODISCARD etl::ireference_counter & get_reference_counter()=0
virtual void release()=0
Release back to the owner.
Definition message.h:94
Definition shared_message.h:50
ETL_NODISCARD bool is_valid() const
Checks if the shared message is valid.
Definition shared_message.h:219
ETL_NODISCARD uint32_t get_reference_count() const
Get the current reference count for this shared message.
Definition shared_message.h:210
ETL_NODISCARD etl::imessage & get_message()
Get a reference to the contained message.
Definition shared_message.h:192
shared_message(const etl::shared_message &other)
Copy constructor.
Definition shared_message.h:115
~shared_message()
Definition shared_message.h:180
ETL_NODISCARD const etl::imessage & get_message() const
Get a const reference to the contained message.
Definition shared_message.h:201
shared_message(etl::ireference_counted_message &rcm)
Constructor.
Definition shared_message.h:105
shared_message(TPool &owner, const TMessage &message)
Constructor.
Definition shared_message.h:68
shared_message & operator=(const etl::shared_message &other)
Copy assignment operator.
Definition shared_message.h:135
bitset_ext
Definition absolute.h:40
Definition utility.h:938