mirror of
https://github.com/ETLCPP/etl.git
synced 2026-11-09 14:49:53 +08:00
Added ishared_message_processor
This commit is contained in:
parent
7d47be6d75
commit
013598da60
51
include/etl/ishared_message_processor.h
Normal file
51
include/etl/ishared_message_processor.h
Normal file
@ -0,0 +1,51 @@
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
https://www.etlcpp.com
|
||||
|
||||
Copyright(c) 2020 jwellbelove
|
||||
|
||||
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_PROCESSOR_INCLUDED
|
||||
#define ETL_SHARED_MESSAGE_PROCESSOR_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "shared_message.h"
|
||||
#include "message_router.h"
|
||||
|
||||
namespace etl
|
||||
{
|
||||
//***************************************************************************
|
||||
/// This is the base of all shared message processors.
|
||||
//***************************************************************************
|
||||
class ishared_message_processor
|
||||
{
|
||||
public:
|
||||
|
||||
virtual ~ishared_message_processor() {}
|
||||
virtual void receive(etl::shared_message message) = 0;
|
||||
virtual void receive(imessage_router& source, etl::shared_message message) = 0;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
@ -85,6 +85,13 @@ namespace etl
|
||||
template <typename TCounter>
|
||||
class reference_counted_message : public etl::ireference_counted_message
|
||||
{
|
||||
public:
|
||||
|
||||
reference_counted_message()
|
||||
: reference_count(TCounter())
|
||||
{
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
//***************************************************************************
|
||||
@ -111,8 +118,6 @@ namespace etl
|
||||
return uint32_t(reference_count);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
/// The reference counter.
|
||||
TCounter reference_count;
|
||||
};
|
||||
|
||||
@ -35,12 +35,76 @@ SOFTWARE.
|
||||
#include "etl/message.h"
|
||||
#include "etl/atomic.h"
|
||||
|
||||
#include "etl/queue.h"
|
||||
#include "etl/ishared_message_processor.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
//*************************************************************************
|
||||
struct Message1 : public etl::message<1, uint32_t>
|
||||
{
|
||||
};
|
||||
|
||||
//*************************************************************************
|
||||
struct Router : public etl::message_router<Router, Message1>
|
||||
{
|
||||
Router()
|
||||
: message_router(1)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void on_receive(const Message1& message)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void on_receive(etl::imessage_router& source, const Message1& message)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void on_receive_unknown(etl::imessage_router& source, const etl::imessage& message)
|
||||
{
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
//*************************************************************************
|
||||
struct Processor : public etl::ishared_message_processor
|
||||
{
|
||||
Processor(etl::imessage_router& router_)
|
||||
: router(router_)
|
||||
{
|
||||
}
|
||||
|
||||
void receive(etl::shared_message message)
|
||||
{
|
||||
uint32_t count1 = message.get_reference_count();
|
||||
|
||||
smqueue.push(message);
|
||||
|
||||
uint32_t count2 = message.get_reference_count();
|
||||
}
|
||||
|
||||
void receive(etl::imessage_router& source, etl::shared_message message)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void Process()
|
||||
{
|
||||
router.receive(smqueue.front().get_message());
|
||||
smqueue.pop();
|
||||
}
|
||||
|
||||
etl::imessage_router& router;
|
||||
etl::queue<etl::shared_message, 10> smqueue;
|
||||
};
|
||||
|
||||
Router router;
|
||||
Processor processor(router);
|
||||
|
||||
SUITE(test_shared_message)
|
||||
{
|
||||
constexpr size_t POOL_SIZE = 2;
|
||||
@ -61,6 +125,14 @@ namespace
|
||||
etl::shared_message sm(mpool, *mpool.create<Message1>());
|
||||
uint32_t count1 = sm.get_reference_count();
|
||||
|
||||
processor.receive(sm);
|
||||
|
||||
uint32_t count1b = sm.get_reference_count();
|
||||
|
||||
processor.Process();
|
||||
|
||||
uint32_t count1c = sm.get_reference_count();
|
||||
|
||||
pbuffered_sm = new etl::shared_message(sm);
|
||||
//uint32_t count2 = sm.get_reference_count();
|
||||
//uint32_t count3 = pbuffered_sm->get_reference_count();
|
||||
|
||||
@ -1083,6 +1083,7 @@
|
||||
<ClInclude Include="..\..\include\etl\queue_spsc_locked.h" />
|
||||
<ClInclude Include="..\..\include\etl\scaled_rounding.h" />
|
||||
<ClInclude Include="..\..\include\etl\shared_message.h" />
|
||||
<ClInclude Include="..\..\include\etl\ishared_message_processor.h" />
|
||||
<ClInclude Include="..\..\include\etl\span.h" />
|
||||
<ClInclude Include="..\..\include\etl\state_chart.h" />
|
||||
<ClInclude Include="..\..\include\etl\math_constants.h" />
|
||||
|
||||
@ -864,6 +864,9 @@
|
||||
<ClInclude Include="..\..\include\etl\message_pool.h">
|
||||
<Filter>ETL\Frameworks</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\include\etl\ishared_message_processor.h">
|
||||
<Filter>ETL\Frameworks</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="..\main.cpp">
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user