Embedded Template Library 1.0
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 //*************************************************************************
54 //*************************************************************************
55 template <typename TPool, typename TMessage>
56 shared_message(TPool& owner, const TMessage& message)
57 {
58 ETL_STATIC_ASSERT((etl::is_base_of<etl::ireference_counted_message_pool, TPool>::value), "TPool not derived from etl::ireference_counted_message_pool");
59 ETL_STATIC_ASSERT((etl::is_base_of<etl::imessage, TMessage>::value), "TMessage not derived from etl::imessage");
60
61 p_rcmessage = owner.allocate(message);
62
63 if (p_rcmessage != ETL_NULLPTR)
64 {
65 p_rcmessage->get_reference_counter().set_reference_count(1U);
66 }
67 }
68
69 //*************************************************************************
71 //*************************************************************************
73 {
74 p_rcmessage = &rcm;
75
76 p_rcmessage->get_reference_counter().set_reference_count(1U);
77 }
78
79 //*************************************************************************
81 //*************************************************************************
83 : p_rcmessage(other.p_rcmessage)
84 {
85 p_rcmessage->get_reference_counter().increment_reference_count();
86 }
87
88#if ETL_USING_CPP11
89 //*************************************************************************
91 //*************************************************************************
92 shared_message(etl::shared_message&& other) ETL_NOEXCEPT
93 : p_rcmessage(etl::move(other.p_rcmessage))
94 {
95 other.p_rcmessage = ETL_NULLPTR;
96 }
97#endif
98
99 //*************************************************************************
101 //*************************************************************************
103 {
104 if (&other != this)
105 {
106 // Deal with the current message.
107 if (p_rcmessage->get_reference_counter().decrement_reference_count() == 0U)
108 {
109 p_rcmessage->release();
110 }
111
112 // Copy over the new one.
113 p_rcmessage = other.p_rcmessage;
114 p_rcmessage->get_reference_counter().increment_reference_count();
115 }
116
117 return *this;
118 }
119
120#if ETL_USING_CPP11
121 //*************************************************************************
123 //*************************************************************************
124 shared_message& operator =(etl::shared_message&& other) ETL_NOEXCEPT
125 {
126 if (&other != this)
127 {
128 // Deal with the current message.
129 if (p_rcmessage->get_reference_counter().decrement_reference_count() == 0U)
130 {
131 p_rcmessage->release();
132 }
133
134 // Move over the new one.
135 p_rcmessage = etl::move(other.p_rcmessage);
136 other.p_rcmessage = ETL_NULLPTR;
137 }
138
139 return *this;
140 }
141#endif
142
143 //*************************************************************************
146 //*************************************************************************
148 {
149 if ((p_rcmessage != ETL_NULLPTR) &&
150 (p_rcmessage->get_reference_counter().decrement_reference_count() == 0U))
151 {
152 p_rcmessage->release();
153 }
154 }
155
156 //*************************************************************************
158 //***********************************************************************
159 ETL_NODISCARD etl::imessage& get_message()
160 {
161 return p_rcmessage->get_message();
162 }
163
164 //*************************************************************************
166 //*************************************************************************
167 ETL_NODISCARD const etl::imessage& get_message() const
168 {
169 return p_rcmessage->get_message();
170 }
171
172 //*************************************************************************
174 //*************************************************************************
175 ETL_NODISCARD uint32_t get_reference_count() const
176 {
177 return p_rcmessage->get_reference_counter().get_reference_count();
178 }
179
180 //*************************************************************************
182 //*************************************************************************
183 ETL_NODISCARD bool is_valid() const
184 {
185 return p_rcmessage != ETL_NULLPTR;
186 }
187
188 private:
189
190 shared_message() ETL_DELETE;
191
192 etl::ireference_counted_message* p_rcmessage;
193 };
194}
195
196#endif
197
Definition: message.h:69
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:84
Definition: shared_message.h:49
ETL_NODISCARD bool is_valid() const
Checks if the shared message is valid.
Definition: shared_message.h:183
ETL_NODISCARD uint32_t get_reference_count() const
Get the current reference count for this shared message.
Definition: shared_message.h:175
ETL_NODISCARD etl::imessage & get_message()
Get a reference to the contained message.
Definition: shared_message.h:159
shared_message(const etl::shared_message &other)
Copy constructor.
Definition: shared_message.h:82
~shared_message()
Definition: shared_message.h:147
shared_message(etl::ireference_counted_message &rcm)
Constructor.
Definition: shared_message.h:72
shared_message(TPool &owner, const TMessage &message)
Constructor.
Definition: shared_message.h:56
shared_message & operator=(const etl::shared_message &other)
Copy assignment operator.
Definition: shared_message.h:102
is_base_of
Definition: type_traits_generator.h:1252
bitset_ext
Definition: absolute.h:38