cc_tools_qt
Common Environment for Protocol Analysis.
Loading...
Searching...
No Matches
ToolsVariantFieldImpl.h
1//
2// Copyright 2017 - 2025 (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 "cc_tools_qt/details/ToolsFieldBase.h"
22#include "cc_tools_qt/field/ToolsVariantField.h"
23
24#include "comms/field/Variant.h"
25
26#include <cassert>
27#include <cstdint>
28#include <functional>
29#include <memory>
30#include <vector>
31#include <string>
32
33namespace cc_tools_qt
34{
35
36namespace details
37{
38
39template <typename TField>
40class ToolsVariantFieldImpl : public ToolsFieldBase<cc_tools_qt::field::ToolsVariantField, TField>
41{
42 using Base = ToolsFieldBase<cc_tools_qt::field::ToolsVariantField, TField>;
43 using Field = TField;
44 static_assert(comms::field::isVariant<Field>(), "Must be of Variant field type");
45 static_assert(!Field::hasFixedValue(), "Fixed value variants are not supported");
46
47public:
48 using Ptr = typename Base::Ptr;
49 using ActPtr = typename Base::ActPtr;
50
51 explicit ToolsVariantFieldImpl(Field& fieldRef)
52 : Base(fieldRef)
53 {
54 }
55
56 ToolsVariantFieldImpl(const ToolsVariantFieldImpl&) = default;
57 ToolsVariantFieldImpl(ToolsVariantFieldImpl&&) = default;
58 virtual ~ToolsVariantFieldImpl() noexcept = default;
59
60 ToolsVariantFieldImpl& operator=(const ToolsVariantFieldImpl&) = delete;
61
62protected:
63 virtual Ptr cloneImpl() override
64 {
65 return ActPtr(new ToolsVariantFieldImpl(Base::field()));
66 }
67
68 virtual int getCurrentIndexImpl() const override
69 {
70 if (!Base::field().currentFieldValid()) {
71 return -1;
72 }
73
74 return static_cast<int>(Base::field().currentField());
75 }
76
77 virtual void setCurrentIndexImpl(int index) override
78 {
79 if (index < 0) {
80 Base::field().reset();
81 return;
82 }
83
84 Base::field().selectField(static_cast<std::size_t>(index));
85 }
86
87 virtual int getMembersCountImpl() const override
88 {
89 return
90 static_cast<int>(
91 std::tuple_size<typename Base::Field::Members>::value);
92 }
93
94 virtual QStringList membersNamesImpl() const override
95 {
96 QStringList names;
97 comms::util::tupleForEachType<typename Field::Members>(MembersNameHelper(names));
98 return names;
99 }
100
101private:
102 class MembersNameHelper
103 {
104 public:
105 explicit MembersNameHelper(QStringList& names) : m_names(names) {}
106
107 template <typename TFieldType>
108 void operator()()
109 {
110 m_names.append(TFieldType::name());
111 }
112
113 private:
114 QStringList& m_names;
115 };
116
117};
118
119template <typename TField>
120auto makeVariantField(TField& field)
121{
122 return std::make_unique<ToolsVariantFieldImpl<TField>>(field);
123}
124
125} // namespace details
126
127} // namespace cc_tools_qt
Main namespace for all classes / functions of the shared library.