cc_tools_qt
Common Environment for Protocol Analysis.
Loading...
Searching...
No Matches
message.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 <cassert>
22
23#include <QtCore/QObject>
24#include <QtCore/QString>
25#include <QtCore/QVariantMap>
26
27#include "cc_tools_qt/Api.h"
28#include "cc_tools_qt/Message.h"
29
30namespace cc_tools_qt
31{
32
33namespace property
34{
35
36namespace message
37{
38
39template <typename TValue>
40class PropBase
41{
42public:
43 typedef TValue ValueType;
44
45 PropBase(const QString& name, const QByteArray& propName)
46 : m_name(name),
47 m_propName(propName)
48 {
49 }
50
51 template <typename U>
52 void setTo(U&& val, QObject& obj) const
53 {
54 obj.setProperty(m_propName, QVariant::fromValue(std::forward<U>(val)));
55 assert(obj.property(m_propName).template canConvert<ValueType>());
56 }
57
58 template <typename U>
59 void setTo(U&& val, QVariantMap& map) const
60 {
61 map.insert(m_name, QVariant::fromValue(std::forward<U>(val)));
62 assert(map.value(m_name).template canConvert<ValueType>());
63 }
64
65 ValueType getFrom(const QObject& obj, const ValueType& defaultVal = ValueType()) const
66 {
67 auto var = obj.property(m_propName.constData());
68 if ((!var.isValid()) || (!var.template canConvert<ValueType>())) {
69 return defaultVal;
70 }
71
72 return var.template value<ValueType>();
73 }
74
75 ValueType getFrom(const QVariantMap& map, const ValueType& defaultVal = ValueType()) const
76 {
77 auto var = map.value(m_name);
78 if ((!var.isValid()) || (!var.template canConvert<ValueType>())) {
79 return defaultVal;
80 }
81
82 return var.template value<ValueType>();
83 }
84
85 void copyFromTo(const QObject& from, QObject& to) const
86 {
87 auto var = from.property(m_propName);
88 if (var.isValid()) {
89 to.setProperty(m_propName, std::move(var));
90 }
91 }
92
93private:
94 const QString& m_name;
95 const QByteArray& m_propName;
96};
97
98class CC_API Type : public PropBase<unsigned>
99{
100 typedef PropBase<unsigned> Base;
101public:
102 typedef Message::Type ValueType;
103
104 Type() : Base(Name, PropName) {}
105
106 template <typename TTo>
107 void setTo(ValueType val, TTo&& to)
108 {
109 Base::setTo(static_cast<Base::ValueType>(val), std::forward<TTo>(to));
110 }
111
112 template <typename TFrom>
113 ValueType getFrom(TFrom&& from)
114 {
115 return static_cast<ValueType>(Base::getFrom(std::forward<TFrom>(from)));
116 }
117
118private:
119 static const QString Name;
120 static const QByteArray PropName;
121};
122
123class CC_API MsgIdx : public PropBase<unsigned>
124{
125 typedef PropBase<unsigned> Base;
126public:
127 MsgIdx() : Base(Name, PropName) {}
128
129private:
130 static const QString Name;
131 static const QByteArray PropName;
132};
133
134
135class CC_API Timestamp : public PropBase<unsigned long long>
136{
137 typedef PropBase<unsigned long long> Base;
138public:
139 Timestamp() : Base(Name, PropName) {}
140
141private:
142 static const QString Name;
143 static const QByteArray PropName;
144};
145
146class CC_API ProtocolName : public PropBase<QString>
147{
148 typedef PropBase<QString> Base;
149public:
150 ProtocolName() : Base(Name, PropName) {}
151
152private:
153 static const QString Name;
154 static const QByteArray PropName;
155};
156
157class CC_API TransportMsg : public PropBase<MessagePtr>
158{
159 typedef PropBase<MessagePtr> Base;
160public:
161 TransportMsg() : Base(Name, PropName) {}
162
163private:
164 static const QString Name;
165 static const QByteArray PropName;
166};
167
168class CC_API RawDataMsg : public PropBase<MessagePtr>
169{
170 typedef PropBase<MessagePtr> Base;
171public:
172 RawDataMsg() : Base(Name, PropName) {}
173
174private:
175 static const QString Name;
176 static const QByteArray PropName;
177};
178
179class CC_API ExtraInfoMsg : public PropBase<MessagePtr>
180{
181 typedef PropBase<MessagePtr> Base;
182public:
183 ExtraInfoMsg() : Base(Name, PropName) {}
184
185private:
186 static const QString Name;
187 static const QByteArray PropName;
188};
189
190class CC_API ExtraInfo : public PropBase<QVariantMap>
191{
192 typedef PropBase<QVariantMap> Base;
193public:
194 ExtraInfo() : Base(Name, PropName) {}
195
196private:
197 static const QString Name;
198 static const QByteArray PropName;
199};
200
201class CC_API ForceExtraInfoExistence : public PropBase<bool>
202{
203 typedef PropBase<bool> Base;
204public:
205 ForceExtraInfoExistence() : Base(Name, PropName) {}
206
207private:
208 static const QString Name;
209 static const QByteArray PropName;
210};
211
212
213class CC_API Delay : public PropBase<unsigned long long>
214{
215 typedef PropBase<unsigned long long> Base;
216public:
217 Delay() : Base(Name, PropName) {}
218
219private:
220 static const QString Name;
221 static const QByteArray PropName;
222};
223
224class CC_API DelayUnits : public PropBase<QString>
225{
226 typedef PropBase<QString> Base;
227public:
228 DelayUnits() : Base(Name, PropName) {}
229
230private:
231 static const QString Name;
232 static const QByteArray PropName;
233};
234
235
236class CC_API RepeatDuration : public PropBase<unsigned long long>
237{
238 typedef PropBase<unsigned long long> Base;
239public:
240 RepeatDuration() : Base(Name, PropName) {}
241
242private:
243 static const QString Name;
244 static const QByteArray PropName;
245};
246
247class CC_API RepeatDurationUnits : public PropBase<QString>
248{
249 typedef PropBase<QString> Base;
250public:
251 RepeatDurationUnits() : Base(Name, PropName) {}
252
253private:
254 static const QString Name;
255 static const QByteArray PropName;
256};
257
258class CC_API RepeatCount : public PropBase<unsigned>
259{
260 typedef PropBase<unsigned> Base;
261public:
262 RepeatCount() : Base(Name, PropName) {}
263
264private:
265 static const QString Name;
266 static const QByteArray PropName;
267};
268
269class CC_API ScrollPos : public PropBase<int>
270{
271 typedef PropBase<int> Base;
272public:
273 ScrollPos() : Base(Name, PropName) {}
274
275private:
276 static const QString Name;
277 static const QByteArray PropName;
278
279};
280
281class CC_API Comment : public PropBase<QString>
282{
283 typedef PropBase<QString> Base;
284public:
285 Comment() : Base(Name, PropName) {}
286
287private:
288 static const QString Name;
289 static const QByteArray PropName;
290
291};
292
293} // namespace message
294
295} // namespace property
296
297} // namespace cc_tools_qt
298
299
Main namespace for all classes / functions of the shared library.