Loading...
Searching...
No Matches
shared_message.h
Go to the documentation of this file.
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2020 John Wellbelove
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/
#ifndef ETL_SHARED_MESSAGE_INCLUDED
#define ETL_SHARED_MESSAGE_INCLUDED
#include "platform.h"
#include "ireference_counted_message_pool.h"
#include "message.h"
#include "reference_counted_message.h"
#include "static_assert.h"
#include "type_traits.h"
#include "utility.h"
//*****************************************************************************
//*****************************************************************************
namespace etl
{
class shared_message
{
public:
#if ETL_USING_CPP11
//*************************************************************************
//*************************************************************************
template <typename TMessage, typename TPool, typename... TArgs>
static shared_message create(TPool& owner, TArgs&&... args)
{
return shared_message(owner, etl::in_place_type_t<TMessage>(), etl::forward<TArgs>(args)...);
}
#endif
//*************************************************************************
//*************************************************************************
template <typename TPool, typename TMessage>
shared_message(TPool& owner, const TMessage& message)
{
ETL_STATIC_ASSERT((etl::is_base_of<etl::ireference_counted_message_pool, TPool>::value),
"TPool not derived from etl::ireference_counted_message_pool");
ETL_STATIC_ASSERT((etl::is_base_of<etl::imessage, TMessage>::value), "TMessage not derived from etl::imessage");
p_rcmessage = owner.allocate(message);
if (p_rcmessage != ETL_NULLPTR)
{
p_rcmessage->get_reference_counter().set_reference_count(1U);
}
}
#if ETL_USING_CPP11
//*************************************************************************
//*************************************************************************
template <typename TPool, typename TMessage, typename... TArgs>
shared_message(TPool& owner, etl::in_place_type_t<TMessage>, TArgs&&... args)
{
ETL_STATIC_ASSERT((etl::is_base_of<etl::ireference_counted_message_pool, TPool>::value),
"TPool not derived from etl::ireference_counted_message_pool");
ETL_STATIC_ASSERT((etl::is_base_of<etl::imessage, TMessage>::value), "TMessage not derived from etl::imessage");
p_rcmessage = owner.template allocate<TMessage>(etl::forward<TArgs>(args)...);
if (p_rcmessage != ETL_NULLPTR)
{
p_rcmessage->get_reference_counter().set_reference_count(1U);
}
}
#endif
//*************************************************************************
//*************************************************************************
shared_message(etl::ireference_counted_message& rcm)
{
p_rcmessage = &rcm;
p_rcmessage->get_reference_counter().set_reference_count(1U);
}
//*************************************************************************
//*************************************************************************
shared_message(const etl::shared_message& other)
: p_rcmessage(other.p_rcmessage)
{
p_rcmessage->get_reference_counter().increment_reference_count();
}
#if ETL_USING_CPP11
//*************************************************************************
//*************************************************************************
shared_message(etl::shared_message&& other) ETL_NOEXCEPT
: p_rcmessage(etl::move(other.p_rcmessage))
{
other.p_rcmessage = ETL_NULLPTR;
}
#endif
//*************************************************************************
//*************************************************************************
shared_message& operator=(const etl::shared_message& other)
{
if (&other != this)
{
// Deal with the current message.
if (p_rcmessage->get_reference_counter().decrement_reference_count() == 0U)
{
p_rcmessage->release();
}
// Copy over the new one.
p_rcmessage = other.p_rcmessage;
p_rcmessage->get_reference_counter().increment_reference_count();
}
return *this;
}
#if ETL_USING_CPP11
//*************************************************************************
//*************************************************************************
shared_message& operator=(etl::shared_message&& other) ETL_NOEXCEPT
{
if (&other != this)
{
// Deal with the current message.
if (p_rcmessage->get_reference_counter().decrement_reference_count() == 0U)
{
p_rcmessage->release();
}
// Move over the new one.
p_rcmessage = etl::move(other.p_rcmessage);
other.p_rcmessage = ETL_NULLPTR;
}
return *this;
}
#endif
//*************************************************************************
//*************************************************************************
~shared_message()
{
if ((p_rcmessage != ETL_NULLPTR) && (p_rcmessage->get_reference_counter().decrement_reference_count() == 0U))
{
p_rcmessage->release();
}
}
//*************************************************************************
//***********************************************************************
ETL_NODISCARD
etl::imessage& get_message()
{
return p_rcmessage->get_message();
}
//*************************************************************************
//*************************************************************************
ETL_NODISCARD
const etl::imessage& get_message() const
{
return p_rcmessage->get_message();
}
//*************************************************************************
//*************************************************************************
ETL_NODISCARD
uint32_t get_reference_count() const
{
return static_cast<uint32_t>(p_rcmessage->get_reference_counter().get_reference_count());
}
//*************************************************************************
//*************************************************************************
ETL_NODISCARD
bool is_valid() const
{
return p_rcmessage != ETL_NULLPTR;
}
private:
shared_message() ETL_DELETE;
etl::ireference_counted_message* p_rcmessage;
};
} // namespace etl
#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