cc_tools_qt
Common Environment for Protocol Analysis.
Loading...
Searching...
No Matches
MessageHandler.h
1//
2// Copyright 2016 - 2024 (C). Alex Robenko. All rights reserved.
3//
4
5// This file is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18
19#pragma once
20
21#include <functional>
22
23#include "Api.h"
24#include "Message.h"
25#include "field_wrapper/FieldWrapper.h"
26#include "details/FieldWrapperCreator.h"
27
28namespace cc_tools_qt
29{
30
35class CC_API MessageHandler
36{
37public:
39 using FieldWrapperPtr = field_wrapper::FieldWrapperPtr;
40
42 virtual ~MessageHandler() noexcept;
43
52 template <typename TMessage>
53 void handle(TMessage& msg)
54 {
55 beginMsgHandlingImpl(msg);
56 addExtraTransportFieldsInternal(msg);
57 auto& fields = msg.fields();
58 comms::util::tupleForEach(
59 fields,
60 FieldsWrapperCreateHelper(
61 [this](FieldWrapperPtr wrapper)
62 {
63 addFieldImpl(std::move(wrapper));
64 }));
65 endMsgHandlingImpl();
66 }
67
68protected:
71 virtual void beginMsgHandlingImpl(Message& msg);
72
76
79 virtual void addFieldImpl(FieldWrapperPtr wrapper);
80
82 virtual void endMsgHandlingImpl();
83
84private:
85 class FieldsWrapperCreateHelper
86 {
87 public:
88 typedef std::function <void (FieldWrapperPtr)> WrapperDispatchFunc;
89 FieldsWrapperCreateHelper(WrapperDispatchFunc&& dispatchOp)
90 : m_dispatchOp(std::move(dispatchOp))
91 {
92 }
93
94 template <typename TField>
95 void operator()(TField&& field)
96 {
97 auto wraper =
98 details::FieldWrapperCreator::createWrapper(
99 std::forward<TField>(field));
100 m_dispatchOp(std::move(wraper));
101 }
102
103 private:
104 WrapperDispatchFunc m_dispatchOp;
105 };
106
107 struct HasExtraTransportFieldsTag {};
108 struct NoExtraTransportFieldsTag {};
109
110 template <typename TMessage>
111 void addExtraTransportFieldsInternal(TMessage& msg)
112 {
113 using MessageType = typename std::decay<decltype(msg)>::type;
114 using Tag =
115 typename std::conditional<
116 MessageType::hasTransportFields(),
117 HasExtraTransportFieldsTag,
118 NoExtraTransportFieldsTag
119 >::type;
120
121 addExtraTransportFieldsInternal(msg, Tag());
122 }
123
124 template <typename TMessage>
125 void addExtraTransportFieldsInternal(TMessage&, NoExtraTransportFieldsTag)
126 {
127 // do nothing
128 }
129
130 template <typename TMessage>
131 void addExtraTransportFieldsInternal(TMessage& msg, HasExtraTransportFieldsTag)
132 {
133 auto& fields = msg.transportFields();
134 comms::util::tupleForEach(
135 fields,
136 FieldsWrapperCreateHelper(
137 [this](FieldWrapperPtr wrapper)
138 {
139 addExtraTransportFieldImpl(std::move(wrapper));
140 }));
141 }
142
143};
144
145} // namespace cc_tools_qt
146
147
Generic message handler used by CommsChampion Tools.
Definition MessageHandler.h:36
virtual ~MessageHandler() noexcept
Destructor.
field_wrapper::FieldWrapperPtr FieldWrapperPtr
Pinter to field_wrapper::FieldWrapper object.
Definition MessageHandler.h:39
virtual void beginMsgHandlingImpl(Message &msg)
Polymorphic report about starting message handling.
virtual void endMsgHandlingImpl()
Polymorphic report about ending message handling.
virtual void addFieldImpl(FieldWrapperPtr wrapper)
Polymorphic request to add handling of the message field.
virtual void addExtraTransportFieldImpl(FieldWrapperPtr wrapper)
Polymorphic request to add handling of the extra transport field.
Main interface class used by CommsChampion Tools to display and manipulate messages.
Definition Message.h:41
Main namespace for all classes / functions of the shared library.