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