Embedded Template Library
1.0
Toggle main menu visibility
Loading...
Searching...
No Matches
shared_message.h
Go to the documentation of this file.
1
2
3
/******************************************************************************
4
The MIT License(MIT)
5
6
Embedded Template Library.
7
https://github.com/ETLCPP/etl
8
https://www.etlcpp.com
9
10
Copyright(c) 2020 John Wellbelove
11
12
Permission is hereby granted, free of charge, to any person obtaining a copy
13
of this software and associated documentation files(the "Software"), to deal
14
in the Software without restriction, including without limitation the rights
15
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16
copies of the Software, and to permit persons to whom the Software is
17
furnished to do so, subject to the following conditions :
18
19
The above copyright notice and this permission notice shall be included in all
20
copies or substantial portions of the Software.
21
22
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
SOFTWARE.
29
******************************************************************************/
30
31
#ifndef ETL_SHARED_MESSAGE_INCLUDED
32
#define ETL_SHARED_MESSAGE_INCLUDED
33
34
#include "
platform.h
"
35
#include "
ireference_counted_message_pool.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
//*****************************************************************************
47
namespace
etl
48
{
49
class
shared_message
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
//*************************************************************************
105
shared_message
(
etl::ireference_counted_message
& rcm)
106
{
107
p_rcmessage = &rcm;
108
109
p_rcmessage->
get_reference_counter
().set_reference_count(1U);
110
}
111
112
//*************************************************************************
114
//*************************************************************************
115
shared_message
(
const
etl::shared_message
& other)
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
//*************************************************************************
135
shared_message
&
operator=
(
const
etl::shared_message
& other)
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
//*************************************************************************
180
~shared_message
()
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
192
etl::imessage
&
get_message
()
193
{
194
return
p_rcmessage->get_message();
195
}
196
197
//*************************************************************************
199
//*************************************************************************
200
ETL_NODISCARD
201
const
etl::imessage
&
get_message
()
const
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
etl::imessage
Definition
message.h:73
etl::ireference_counted_message
Definition
reference_counted_message.h:48
etl::ireference_counted_message::get_reference_counter
virtual ETL_NODISCARD etl::ireference_counter & get_reference_counter()=0
etl::ireference_counted_message::release
virtual void release()=0
Release back to the owner.
etl::message
Definition
message.h:92
etl::shared_message
Definition
shared_message.h:50
etl::shared_message::is_valid
ETL_NODISCARD bool is_valid() const
Checks if the shared message is valid.
Definition
shared_message.h:219
etl::shared_message::get_reference_count
ETL_NODISCARD uint32_t get_reference_count() const
Get the current reference count for this shared message.
Definition
shared_message.h:210
etl::shared_message::get_message
ETL_NODISCARD etl::imessage & get_message()
Get a reference to the contained message.
Definition
shared_message.h:192
etl::shared_message::shared_message
shared_message(const etl::shared_message &other)
Copy constructor.
Definition
shared_message.h:115
etl::shared_message::~shared_message
~shared_message()
Definition
shared_message.h:180
etl::shared_message::get_message
ETL_NODISCARD const etl::imessage & get_message() const
Get a const reference to the contained message.
Definition
shared_message.h:201
etl::shared_message::shared_message
shared_message(etl::ireference_counted_message &rcm)
Constructor.
Definition
shared_message.h:105
etl::shared_message::shared_message
shared_message(TPool &owner, const TMessage &message)
Constructor.
Definition
shared_message.h:68
etl::shared_message::operator=
shared_message & operator=(const etl::shared_message &other)
Copy assignment operator.
Definition
shared_message.h:135
ireference_counted_message_pool.h
etl
Definition
absolute.h:40
platform.h
etl::in_place_type_t
Definition
utility.h:952
type_traits.h
utility.h
include
etl
shared_message.h
Generated on
for Embedded Template Library by
1.17.0