cc_tools_qt
Common Environment for Protocol Analysis.
Loading...
Searching...
No Matches
ToolsVariantFieldImpl.h
1//
2// Copyright 2017 - 2026 (C). Alex Robenko. All rights reserved.
3//
4// SPDX-License-Identifier: GPL-3.0-or-later
5//
6
7// This file is free software: you can redistribute it and/or modify
8// it under the terms of the GNU General Public License as published by
9// the Free Software Foundation, either version 3 of the License, or
10// (at your option) any later version.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20#pragma once
21
22#include "cc_tools_qt/details/ToolsFieldBase.h"
23#include "cc_tools_qt/field/ToolsVariantField.h"
24
25#include "comms/field/Variant.h"
26
27#include <cassert>
28#include <cstdint>
29#include <functional>
30#include <memory>
31#include <vector>
32#include <string>
33
34namespace cc_tools_qt
35{
36
37namespace details
38{
39
40template <typename TField>
41class ToolsVariantFieldImpl : public ToolsFieldBase<cc_tools_qt::field::ToolsVariantField, TField>
42{
43 using Base = ToolsFieldBase<cc_tools_qt::field::ToolsVariantField, TField>;
44 using Field = TField;
45 static_assert(comms::field::isVariant<Field>(), "Must be of Variant field type");
46 static_assert(!Field::hasFixedValue(), "Fixed value variants are not supported");
47
48public:
49 using Ptr = typename Base::Ptr;
50 using ActPtr = typename Base::ActPtr;
51
52 explicit ToolsVariantFieldImpl(Field& fieldRef)
53 : Base(fieldRef)
54 {
55 }
56
57 ToolsVariantFieldImpl(const ToolsVariantFieldImpl&) = default;
58 ToolsVariantFieldImpl(ToolsVariantFieldImpl&&) = default;
59 virtual ~ToolsVariantFieldImpl() noexcept = default;
60
61 ToolsVariantFieldImpl& operator=(const ToolsVariantFieldImpl&) = delete;
62
63protected:
64 virtual Ptr cloneImpl() override
65 {
66 return ActPtr(new ToolsVariantFieldImpl(Base::field()));
67 }
68
69 virtual int getCurrentIndexImpl() const override
70 {
71 if (!Base::field().currentFieldValid()) {
72 return -1;
73 }
74
75 return static_cast<int>(Base::field().currentField());
76 }
77
78 virtual void setCurrentIndexImpl(int index) override
79 {
80 if (index < 0) {
81 Base::field().reset();
82 return;
83 }
84
85 Base::field().selectField(static_cast<std::size_t>(index));
86 }
87
88 virtual int getMembersCountImpl() const override
89 {
90 return
91 static_cast<int>(
92 std::tuple_size<typename Base::Field::Members>::value);
93 }
94
95 virtual QStringList membersNamesImpl() const override
96 {
97 QStringList names;
98 comms::util::tupleForEachType<typename Field::Members>(MembersNameHelper(names));
99 return names;
100 }
101
102private:
103 class MembersNameHelper
104 {
105 public:
106 explicit MembersNameHelper(QStringList& names) : m_names(names) {}
107
108 template <typename TFieldType>
109 void operator()()
110 {
111 m_names.append(TFieldType::name());
112 }
113
114 private:
115 QStringList& m_names;
116 };
117
118};
119
120template <typename TField>
121auto makeVariantField(TField& field)
122{
123 return std::make_unique<ToolsVariantFieldImpl<TField>>(field);
124}
125
126} // namespace details
127
128} // namespace cc_tools_qt
Main namespace for all classes / functions of the shared library.