cc_tools_qt
Common Environment for Protocol Analysis.
Loading...
Searching...
No Matches
ProtocolMessageBase.h
1//
2// Copyright 2015 - 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 <type_traits>
22#include <string>
23#include <cassert>
24
25#include "cc_tools_qt/Message.h"
26#include "comms/MessageBase.h"
27
28namespace cc_tools_qt
29{
30
42template <typename TMsgBase, typename TActualMsg>
43class ProtocolMessageBase : public TMsgBase
44{
45 using Base = TMsgBase;
46 using ActualMsg = TActualMsg;
47
48protected:
49
52
55
58
60 ~ProtocolMessageBase() noexcept = default;
61
63 ProtocolMessageBase& operator=(const ProtocolMessageBase&) = default;
64
67
69 virtual void resetImpl() override
70 {
71 auto& actObj = static_cast<ActualMsg&>(*this);
72 actObj = ActualMsg();
73 }
74
76 virtual bool assignImpl(const cc_tools_qt::Message& other) override
77 {
78 auto* castedOther = dynamic_cast<const ActualMsg*>(&other);
79 if (castedOther == nullptr) {
80 return false;
81 }
82
83 assert(other.idAsString() == Base::idAsString());
84 auto& actObj = static_cast<ActualMsg&>(*this);
85 actObj = *castedOther;
86 return true;
87 }
88
90 virtual const char* nameImpl() const override
91 {
92 static_assert(comms::isMessageBase<TMsgBase>(), "TMsgBase is expected to be proper message");
93
94 using Tag = typename std::conditional<
95 TMsgBase::hasCustomName(),
96 HasNameTag,
97 NoNameTag
98 >::type;
99 return nameInternal(Tag());
100 }
101
102private:
103 struct HasNameTag {};
104 struct NoNameTag {};
105
106 const char* nameInternal(HasNameTag) const
107 {
108 return TMsgBase::doName();
109 }
110
111 const char* nameInternal(NoNameTag) const
112 {
113 [[maybe_unused]] static constexpr bool Must_be_overriden = false;
114 assert(Must_be_overriden);
115 return nullptr;
116 }
117
118};
119
120} // namespace cc_tools_qt
121
122
Main interface class used by CommsChampion Tools to display and manipulate messages.
Definition Message.h:41
QString idAsString() const
Get string representation of message ID.
Helper class used to implement several pure virtual functions defined in cc_tools_qt::Message interfa...
Definition ProtocolMessageBase.h:44
virtual bool assignImpl(const cc_tools_qt::Message &other) override
Overriding implementation to cc_tools_qt::Message::assignImpl()
Definition ProtocolMessageBase.h:76
virtual void resetImpl() override
Overriding implementation to cc_tools_qt::Message::resetImpl()
Definition ProtocolMessageBase.h:69
ProtocolMessageBase(const ProtocolMessageBase &)=default
Copy constructor.
ProtocolMessageBase()=default
Default constructor.
virtual const char * nameImpl() const override
Overriding implementation to cc_tools_qt::Message::nameImpl()
Definition ProtocolMessageBase.h:90
ProtocolMessageBase(ProtocolMessageBase &&)=default
Move constructor.
~ProtocolMessageBase() noexcept=default
Destructor.
Main namespace for all classes / functions of the shared library.