cc_tools_qt
Common Environment for Protocol Analysis.
Loading...
Searching...
No Matches
field.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 <QtCore/QString>
22#include <QtCore/QVariantMap>
23#include <QtCore/QVariantList>
24#include <QtCore/QList>
25#include <QtCore/QPair>
26
27#include "comms/comms.h"
28#include "cc_tools_qt/Api.h"
29
30namespace cc_tools_qt
31{
32
33namespace property
34{
35
36namespace field
37{
38
42class CC_API Common
43{
44public:
47
49 Common(const Common&);
50
53
55 explicit Common(const QVariantMap& props);
56
58 explicit Common(const QVariant& props);
59
61 ~Common() noexcept;
62
64 Common& operator=(const Common&);
65
67 Common& operator=(Common&&);
68
70 const QString& name() const;
71
73 void setName(const QString& value);
74
76 void setName(const char* value);
77
79 bool isHidden() const;
80
82 Common& hidden(bool value = true);
83
85 bool isSerialisedHidden() const;
86
88 Common& serialisedHidden(bool value = true);
89
91 bool isReadOnly() const;
92
94 Common& readOnly(bool value = true);
95
97 bool isHiddenWhenReadOnly() const;
98
100 Common& hiddenWhenReadOnly(bool value = true);
101
103 void setTo(QVariantMap& props) const;
104
106 void getFrom(const QVariantMap& props);
107
108protected:
109
114 template <typename U>
115 static void setElemTo(U&& val, const QString& name, QVariantMap& props)
116 {
117 props.insert(name, QVariant::fromValue(std::forward<U>(val)));
118 }
119
125 template <typename TValueType>
126 static TValueType getElemFrom(
127 const QVariantMap& props,
128 const QString& name,
129 const TValueType& defaultVal = TValueType())
130 {
131 auto var = props.value(name);
132 if ((!var.isValid()) || (!var.canConvert<TValueType>())) {
133 return defaultVal;
134 }
135
136 return var.value<TValueType>();
137 }
138
139private:
140 QString m_name;
141 bool m_hidden = false;
142 bool m_serialisedHidden = false;
143 bool m_readOnly = false;
144 bool m_hiddenWhenReadOnly = false;
145};
146
150template <typename TDerived>
151class CommonBase : public Common
152{
153 using Base = Common;
154public:
156 CommonBase() = default;
157
159 CommonBase(const CommonBase&) = default;
160
162 CommonBase(CommonBase&&) = default;
163
165 explicit CommonBase(const QVariantMap& props) : Base(props) {}
166
168 explicit CommonBase(const QVariant& props) : Base(props) {}
169
171 ~CommonBase() noexcept = default;
172
174 CommonBase& operator=(const CommonBase&) = default;
175
177 CommonBase& operator=(CommonBase&&) = default;
178
179 using Base::name;
180
183 TDerived& name(const QString& value)
184 {
185 Base::setName(value);
186 return static_cast<TDerived&>(*this);
187 }
188
191 TDerived& name(const char* value)
192 {
193 Base::setName(value);
194 return static_cast<TDerived&>(*this);
195 }
196
199 TDerived& hidden(bool value = true)
200 {
201 return static_cast<TDerived&>(Base::hidden(value));
202 }
203
206 TDerived& serialisedHidden(bool value = true)
207 {
208 return static_cast<TDerived&>(Base::serialisedHidden(value));
209 }
210
213 TDerived& readOnly(bool value = true)
214 {
215 return static_cast<TDerived&>(Base::readOnly(value));
216 }
217
220 TDerived& hiddenWhenReadOnly(bool value = true)
221 {
222 return static_cast<TDerived&>(Base::hiddenWhenReadOnly(value));
223 }
224};
225
229class CC_API IntValue : public CommonBase<IntValue>
230{
232public:
235 using SpecialType = QPair<QString, long long>;
236
238 using SpecialsList = QList<SpecialType>;
239
242
245
248
250 IntValue(const QVariantMap& props);
251
253 IntValue(const QVariant& props);
254
256 ~IntValue() noexcept;
257
259 IntValue& operator=(const IntValue&);
260
262 IntValue& operator=(IntValue&&);
263
265 long long displayOffset() const;
266
268 IntValue& displayOffset(long long value);
269
271 bool hasScaledDecimals() const;
272
275 int scaledDecimals() const;
276
279 IntValue& scaledDecimals(int value);
280
282 const SpecialsList& specials() const;
283
287 IntValue& addSpecial(const QString& specialName, long long value);
288
290 QVariantMap asMap() const;
291
292private:
293 void getFrom(const QVariantMap& props);
294
295 long long m_displayOffset = 0;
296 int m_scaledDecimals = 0;
297 SpecialsList m_specials;
298};
299
303class CC_API EnumValue : public CommonBase<EnumValue>
304{
306public:
307
310 using ElemType = QPair<QString, long long>;
311
313 using ElemsList = QList<ElemType>;
314
317
320
323
325 EnumValue(const QVariantMap& props);
326
328 EnumValue(const QVariant& props);
329
331 ~EnumValue() noexcept;
332
334 EnumValue& operator=(const EnumValue&);
335
337 EnumValue& operator=(EnumValue&&);
338
340 const ElemsList& values() const;
341
345 EnumValue& add(const QString& elemName, long long value);
346
352 EnumValue& add(const QString& elemName);
353
355 QVariantMap asMap() const;
356private:
357 void getFrom(const QVariantMap& props);
358
359 ElemsList m_elems;
360};
361
366{
368public:
369
371 using BitsList = QVariantList;
372
375
378
381
383 BitmaskValue(const QVariantMap& props);
384
386 BitmaskValue(const QVariant& props);
387
389 ~BitmaskValue() noexcept;
390
392 BitmaskValue& operator=(const BitmaskValue&);
393
396
398 const BitsList& bits() const;
399
403 BitmaskValue& add(int idx, const QString& bitName);
404
409 BitmaskValue& add(const QString& bitName);
410
412 QVariantMap asMap() const;
413private:
414 void getFrom(const QVariantMap& props);
415
416 BitsList m_bits;
417};
418
422class CC_API Bitfield : public CommonBase<Bitfield>
423{
425public:
426
428 using MembersList = QList<QVariantMap>;
429
432
435
438
440 Bitfield(const QVariantMap& props);
441
443 Bitfield(const QVariant& props);
444
446 ~Bitfield() noexcept;
447
449 Bitfield& operator=(const Bitfield&);
450
452 Bitfield& operator=(Bitfield&&);
453
455 const MembersList& members() const;
456
458 Bitfield& add(QVariantMap&& memberProps);
459
461 Bitfield& add(const QVariantMap& memberProps);
462
464 QVariantMap asMap() const;
465private:
466 void getFrom(const QVariantMap& props);
467
468 MembersList m_members;
469};
470
474class CC_API Bundle : public CommonBase<Bundle>
475{
476 using Base = CommonBase<Bundle>;
477public:
478
480 using MembersList = QList<QVariantMap>;
481
484
486 Bundle(const Bundle&);
487
490
492 Bundle(const QVariantMap& props);
493
495 Bundle(const QVariant& props);
496
498 ~Bundle() noexcept;
499
501 Bundle& operator=(const Bundle&);
502
504 Bundle& operator=(Bundle&&);
505
507 const MembersList& members() const;
508
510 Bundle& add(QVariantMap&& memberProps);
511
513 Bundle& add(const QVariantMap& memberProps);
514
516 QVariantMap asMap() const;
517private:
518 void getFrom(const QVariantMap& props);
519
520 MembersList m_members;
521};
522
526class CC_API String : public CommonBase<String>
527{
528 typedef CommonBase<String> Base;
529public:
532
534 String(const String&);
535
538
540 String(const QVariantMap& props);
541
543 String(const QVariant& props);
544
546 ~String() noexcept;
547
549 String& operator=(const String&);
550
552 String& operator=(String&&);
553
555 QVariantMap asMap() const;
556};
557
561class CC_API ArrayList : public CommonBase<ArrayList>
562{
564public:
566 using ElemsList = QList<QVariantMap>;
567
570
573
576
578 ArrayList(const QVariantMap& props);
579
581 ArrayList(const QVariant& props);
582
584 ~ArrayList() noexcept;
585
587 ArrayList& operator=(const ArrayList&);
588
590 ArrayList& operator=(ArrayList&&);
591
593 const ElemsList& elements() const;
594
596 ArrayList& add(QVariantMap&& elemProps);
597
599 ArrayList& add(const QVariantMap& elemProps);
600
603 bool isPrefixVisible() const;
604
607 ArrayList& showPrefix(bool value = true);
608
610 const QString& prefixName() const;
611
613 ArrayList& prefixName(const QString& nameParam);
614
616 bool isIndexAppendedToElementName() const;
617
619 ArrayList& appendIndexToElementName(bool value = true);
620
622 QVariantMap asMap() const;
623
624private:
625 void getFrom(const QVariantMap& props);
626
627 ElemsList m_elems;
628 QString m_prefixName;
629 bool m_showPrefix = false;
630 bool m_appendIndexToElementName = false;
631};
632
636class CC_API Optional : public CommonBase<Optional>
637{
639public:
640
643
646
649
651 Optional(const QVariantMap& props);
652
654 Optional(const QVariant& props);
655
657 ~Optional() noexcept;
658
660 Optional& operator=(const Optional&);
661
663 Optional& operator=(Optional&&);
664
666 const QVariantMap& field() const;
667
669 Optional& field(QVariantMap&& fieldProps);
670
672 Optional& field(const QVariantMap& fieldProps);
673
677 bool isUncheckable() const;
678
680 Optional& uncheckable(bool value = true);
681
683 QVariantMap asMap() const;
684
685private:
686 void getFrom(const QVariantMap& props);
687
688 QVariantMap m_field;
689 bool m_uncheckable = false;
690};
691
695class CC_API FloatValue : public CommonBase<FloatValue>
696{
698public:
701 using SpecialType = QPair<QString, double>;
702
704 using SpecialsList = QList<SpecialType>;
705
708
711
714
716 FloatValue(const QVariantMap& props);
717
719 FloatValue(const QVariant& props);
720
722 ~FloatValue() noexcept;
723
725 FloatValue& operator=(const FloatValue&);
726
728 FloatValue& operator=(FloatValue&&);
729
731 int decimals() const;
732
734 FloatValue& decimals(int value);
735
737 const SpecialsList& specials() const;
738
742 FloatValue& addSpecial(const QString& specialName, double value);
743
744
746 QVariantMap asMap() const;
747
748private:
749 void getFrom(const QVariantMap& props);
750
751 int m_decimals = 0;
752 SpecialsList m_specials;
753};
754
758class CC_API Variant : public CommonBase<Variant>
759{
761public:
762
764 using MembersList = QList<QVariantMap>;
765
768
771
774
776 Variant(const QVariantMap& props);
777
779 Variant(const QVariant& props);
780
782 ~Variant() noexcept;
783
785 Variant& operator=(const Variant&);
786
788 Variant& operator=(Variant&&);
789
791 const MembersList& members() const;
792
794 Variant& add(QVariantMap&& memberProps);
795
797 Variant& add(const QVariantMap& memberProps);
798
800 bool isIndexHidden() const;
801
803 Variant& setIndexHidden(bool hiddenVal = true);
804
806 QVariantMap asMap() const;
807private:
808 void getFrom(const QVariantMap& props);
809
810 MembersList m_members;
811 bool m_indexHidden = false;
812};
813
814namespace details
815{
816
817template <typename TField>
818struct ForTag;
819
820template <>
821struct ForTag<comms::field::tag::Int>
822{
824};
825
826template <>
827struct ForTag<comms::field::tag::Enum>
828{
830};
831
832template <>
833struct ForTag<comms::field::tag::Bitmask>
834{
836};
837
838template <>
839struct ForTag<comms::field::tag::Bitfield>
840{
842};
843
844template <>
845struct ForTag<comms::field::tag::Bundle>
846{
848};
849
850template <>
851struct ForTag<comms::field::tag::String>
852{
854};
855
856template <>
857struct ForTag<comms::field::tag::RawArrayList>
858{
860};
861
862template <>
863struct ForTag<comms::field::tag::ArrayList>
864{
866};
867
868template <>
869struct ForTag<comms::field::tag::Optional>
870{
872};
873
874template <>
875struct ForTag<comms::field::tag::Float>
876{
878};
879
880template <>
881struct ForTag<comms::field::tag::Variant>
882{
884};
885
886} // namespace details
887
891template <typename TField>
892using ForField = typename details::ForTag<typename TField::CommsTag>::Type;
893
894} // namespace field
895
896} // namespace property
897
898} // namespace cc_tools_qt
899
Class to contain all the properties relevant to comms::field::ArrayList fields.
Definition field.h:562
QList< QVariantMap > ElemsList
List of data elements' properties.
Definition field.h:566
ArrayList(const ArrayList &)
Copy constructor.
ArrayList(const QVariant &props)
Construct from QVariant containing QVariantMap.
ArrayList(ArrayList &&)
Move constructor.
ArrayList(const QVariantMap &props)
Construct from QVariantMap.
Class to contain all the properties relevant to comms::field::Bitfield fields.
Definition field.h:423
Bitfield(const QVariant &props)
Construct from QVariant containing QVariantMap.
Bitfield(Bitfield &&)
Move constructor.
Bitfield(const QVariantMap &props)
Construct from QVariantMap.
QList< QVariantMap > MembersList
Properties of contained fields.
Definition field.h:428
Bitfield(const Bitfield &)
Copy constructor.
Class to contain all the properties relevant to comms::field::BitmaskValue fields.
Definition field.h:366
BitmaskValue(const QVariant &props)
Construct from QVariant containing QVariantMap.
BitmaskValue(const BitmaskValue &)
Copy constructor.
QVariantList BitsList
List of bits descriptions.
Definition field.h:371
BitmaskValue(BitmaskValue &&)
Move constructor.
BitmaskValue(const QVariantMap &props)
Construct from QVariantMap.
Class to contain all the properties relevant to comms::field::Bundle fields.
Definition field.h:475
Bundle(Bundle &&)
Move constructor.
Bundle(const Bundle &)
Copy constructor.
Bundle(const QVariant &props)
Construct from QVariant containing QVariantMap.
QList< QVariantMap > MembersList
Properties of contained fields.
Definition field.h:480
Bundle(const QVariantMap &props)
Construct from QVariantMap.
Intermediate helper class to define properties describing one.
Definition field.h:152
CommonBase(const QVariant &props)
Construct from QVariant that contains QVariantMap.
Definition field.h:168
CommonBase(const QVariantMap &props)
Construct from QVariantMap.
Definition field.h:165
CommonBase()=default
Default constructor.
TDerived & name(const char *value)
Set name value.
Definition field.h:191
CommonBase(CommonBase &&)=default
Move constructor.
TDerived & hiddenWhenReadOnly(bool value=true)
Set whether the field must be hidden when cannot be modified.
Definition field.h:220
~CommonBase() noexcept=default
Destructor.
TDerived & hidden(bool value=true)
Set whether the field is hidden.
Definition field.h:199
TDerived & readOnly(bool value=true)
Set whether the field cannot be modified.
Definition field.h:213
CommonBase(const CommonBase &)=default
Copy constructor.
TDerived & serialisedHidden(bool value=true)
Set whether the serialised part is hidden.
Definition field.h:206
const QString & name() const
Get name of the field.
Common set of properties.
Definition field.h:43
void setName(const QString &value)
Update the name value.
Common(const QVariant &props)
Construct from QVariant that contains QVariantMap.
static TValueType getElemFrom(const QVariantMap &props, const QString &name, const TValueType &defaultVal=TValueType())
read element value from the map
Definition field.h:126
Common & hiddenWhenReadOnly(bool value=true)
Set whether the field must be hidden when cannot be modified.
Common(const QVariantMap &props)
Construct from QVariantMap.
Common(Common &&)
Move constructor.
Common & hidden(bool value=true)
Set whether the field is hidden.
Common & readOnly(bool value=true)
Set whether the field cannot be modified.
Common & serialisedHidden(bool value=true)
Set whether the serialised part is hidden.
Common(const Common &)
Copy constructor.
Class to contain all the properties relevant to comms::field::EnumValue fields.
Definition field.h:304
EnumValue(const QVariantMap &props)
Construct from QVariantMap.
EnumValue(EnumValue &&)
Move constructor.
QList< ElemType > ElemsList
List of properties describing enum value.
Definition field.h:313
EnumValue(const QVariant &props)
Construct from QVariant containing QVariantMap.
QPair< QString, long long > ElemType
The enum value is described as string containing name and the actual numeric value.
Definition field.h:310
EnumValue(const EnumValue &)
Copy constructor.
Class to contain all the properties relevant to comms::field::FloatValue fields.
Definition field.h:696
QList< SpecialType > SpecialsList
List of special values.
Definition field.h:704
FloatValue(const QVariant &props)
Construct from QVariant containing QVariantMap.
FloatValue(const QVariantMap &props)
Construct from QVariantMap.
QPair< QString, double > SpecialType
The special value is described as string containing name and the actual numeric value.
Definition field.h:701
FloatValue(FloatValue &&)
Move constructor.
FloatValue(const FloatValue &)
Copy constructor.
Class to contain all the properties relevant to comms::field::IntValue fields.
Definition field.h:230
IntValue(const QVariantMap &props)
Construct from QVariantMap.
QList< SpecialType > SpecialsList
List of special values.
Definition field.h:238
QPair< QString, long long > SpecialType
The special value is described as string containing name and the actual numeric value.
Definition field.h:235
IntValue(const IntValue &)
Copy constructor.
IntValue(const QVariant &props)
Construct from QVariant that contains QVariantMap.
IntValue(IntValue &&)
Move constructor.
Class to contain all the properties relevant to comms::field::Optional fields.
Definition field.h:637
Optional(const QVariant &props)
Construct from QVariant containing QVariantMap.
Optional(const Optional &)
Copy constructor.
Optional(Optional &&)
Move constructor.
Optional(const QVariantMap &props)
Construct from QVariantMap.
Class to contain all the properties relevant to comms::field::String fields.
Definition field.h:527
String(const QVariantMap &props)
Construct from QVariantMap.
String(const String &)
Copy constructor.
String(const QVariant &props)
Construct from QVariant containing QVariantMap.
String(String &&)
Move constructor.
Class to contain all the properties relevant to comms::field::Variant fields.
Definition field.h:759
Variant(const QVariant &props)
Construct from QVariant containing QVariantMap.
Variant(const Variant &)
Copy constructor.
QList< QVariantMap > MembersList
List of properties of the contained fields.
Definition field.h:764
Variant(Variant &&)
Move constructor.
Variant(const QVariantMap &props)
Construct from QVariantMap.
typename details::ForTag< typename TField::CommsTag >::Type ForField
Get proper properties management class for field.
Definition field.h:892
Main namespace for all classes / functions of the shared library.