cc_tools_qt
Common Environment for Protocol Analysis.
Loading...
Searching...
No Matches
MessageBase.h
1//
2// Copyright 2014 - 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 <cassert>
23#include <iterator>
24#include <algorithm>
25#include <vector>
26
27#include "comms/comms.h"
28
29#include "Message.h"
30#include "MessageHandler.h"
31#include "cc_tools_qt.h"
32
33namespace cc_tools_qt
34{
35
49template <template<typename...> class TMessageBase, typename... TOptions>
52 public TMessageBase<
53 comms::option::IdInfoInterface,
54 comms::option::ReadIterator<const std::uint8_t*>,
55 comms::option::WriteIterator<std::back_insert_iterator<std::vector<std::uint8_t> > >,
56 comms::option::Handler<MessageHandler>,
57 comms::option::ValidCheckInterface,
58 comms::option::LengthInfoInterface,
59 comms::option::RefreshInterface,
60 comms::option::NameInterface,
61 TOptions...>
62{
64 using CommsBase =
65 TMessageBase<
66 comms::option::IdInfoInterface,
67 comms::option::ReadIterator<const std::uint8_t*>,
68 comms::option::WriteIterator<std::back_insert_iterator<std::vector<std::uint8_t> > >,
69 comms::option::Handler<MessageHandler>,
70 comms::option::ValidCheckInterface,
71 comms::option::LengthInfoInterface,
72 comms::option::RefreshInterface,
73 comms::option::NameInterface,
74 TOptions...>;
75public:
78 using Handler = typename CommsBase::Handler;
79
81 MessageBase() = default;
82
84 MessageBase(const MessageBase&) = default;
85
88
90 ~MessageBase() noexcept = default;
91
93 MessageBase& operator=(const MessageBase& other)
94 {
95 CommsBase::operator=(other);
96 return *this;
97 }
98
101 {
102 CommsBase::operator=(std::move(other));
103 return *this;
104 }
105
106protected:
107
111 virtual bool refreshMsgImpl() override
112 {
113 return CommsBase::refresh();
114 }
115
119 virtual QString idAsStringImpl() const override
120 {
121 static const bool IsNumeric =
122 std::is_enum<typename CommsBase::MsgIdType>::value ||
123 std::is_integral<typename CommsBase::MsgIdType>::value;
124 using Tag =
125 typename std::conditional<
126 IsNumeric,
127 NumericIdTag,
128 NonNumericIdTag
129 >::type;
130 return idAsStringImplInternal(Tag());
131 }
132
136 virtual bool isValidImpl() const override
137 {
138 return CommsBase::valid();
139 }
140
144 virtual DataSeq encodeDataImpl() const override
145 {
146 typedef typename CommsBase::WriteIterator Iter;
147 typedef typename std::iterator_traits<Iter>::iterator_category Tag;
148
149 static_assert(
150 std::is_base_of<std::output_iterator_tag, Tag>::value,
151 "Iterator is expected to be output iterator.");
152
153 static_assert(std::is_same<std::back_insert_iterator<DataSeq>, Iter>::value,
154 "Write iterator is expected to back insert iterator");
155
156 DataSeq data;
157 data.reserve(CommsBase::length());
158 auto iter = std::back_inserter(data);
159 [[maybe_unused]] auto es = CommsBase::write(iter, data.max_size());
160 assert(es == comms::ErrorStatus::Success);
161 return data;
162 }
163
167 virtual bool decodeDataImpl(const DataSeq& data) override
168 {
169 typedef typename CommsBase::ReadIterator Iter;
170 typedef typename std::iterator_traits<Iter>::iterator_category Tag;
171 static_assert(
172 std::is_base_of<std::random_access_iterator_tag, Tag>::value,
173 "Only random access iterator is supported for data decoding.");
174
175 static_assert(std::is_pointer<Iter>::value, "Pointer as iterator is expected");
176
177 Iter iter = nullptr;
178 if (!data.empty()) {
179 iter = &data[0];
180 }
181
182 auto es = CommsBase::read(iter, data.size());
183 return es == comms::ErrorStatus::Success;
184 }
185
186private:
187 struct NumericIdTag {};
188 struct NonNumericIdTag {};
189
190 QString idAsStringImplInternal(NumericIdTag) const
191 {
192 return QString("%1").arg(static_cast<qlonglong>(CommsBase::getId()));
193 }
194
195 QString idAsStringImplInternal(NonNumericIdTag) const
196 {
197 return QString("%1").arg(CommsBase::getId());
198 }
199};
200
201} // namespace cc_tools_qt
202
203
Single "include all" file.
Helper class used to define protocol message interface class in CommsChampion Tools plugin environmen...
Definition MessageBase.h:62
virtual bool refreshMsgImpl() override
Overriding polymorphic refresh functionality.
Definition MessageBase.h:111
virtual DataSeq encodeDataImpl() const override
Overriding polymorphic serialisation functionaly.
Definition MessageBase.h:144
typename CommsBase::Handler Handler
Handler class.
Definition MessageBase.h:78
virtual bool decodeDataImpl(const DataSeq &data) override
Overriding polymorphic deserialisation functionaly.
Definition MessageBase.h:167
virtual bool isValidImpl() const override
Overriding polymorphic validity check.
Definition MessageBase.h:136
MessageBase(const MessageBase &)=default
Copy Constructor.
MessageBase & operator=(MessageBase &&other)
Move assignment operator.
Definition MessageBase.h:100
virtual QString idAsStringImpl() const override
Overriding polymorphic retrieval of the id string.
Definition MessageBase.h:119
MessageBase(MessageBase &&)=default
Move Constructor.
~MessageBase() noexcept=default
Destructor.
MessageBase()=default
Default constructor.
Main interface class used by CommsChampion Tools to display and manipulate messages.
Definition Message.h:41
std::vector< std::uint8_t > DataSeq
Type for sequence of raw bytes.
Definition Message.h:45
Main namespace for all classes / functions of the shared library.