cc_tools_qt
Common Environment for Protocol Analysis.
Loading...
Searching...
No Matches
ToolsEnumFieldImpl.h
1//
2// Copyright 2014 - 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/ToolsNumericFieldImpl.h"
23#include "cc_tools_qt/field/ToolsEnumField.h"
24
25#include "comms/field/EnumValue.h"
26
27#include <cstdint>
28#include <cassert>
29#include <limits>
30#include <memory>
31
32namespace cc_tools_qt
33{
34
35namespace details
36{
37
38template <typename TField>
39class ToolsEnumFieldImpl : public ToolsNumericFieldImpl<cc_tools_qt::field::ToolsEnumField, TField>
40{
41 using Base = ToolsNumericFieldImpl<cc_tools_qt::field::ToolsEnumField, TField>;
42 using Field = TField;
43
44 static_assert(comms::field::isEnumValue<Field>(), "Must be of EnumValueField type");
45
46 using ValueType = typename Field::ValueType;
47 using UnderlyingType = typename Base::UnderlyingType;
48 static_assert(sizeof(ValueType) <= sizeof(UnderlyingType), "This class cannot handle provided field.");
49// static_assert(std::is_signed<ValueType>::value || (sizeof(ValueType) < sizeof(UnderlyingType)),
50// "This class cannot handle provided field.");
51
52public:
53 using ValueInfosList = typename Base::ValueInfosList;
54 using Ptr = typename Base::Ptr;
55 using ActPtr = typename Base::ActPtr;
56
57 explicit ToolsEnumFieldImpl(Field& fieldRef)
58 : Base(fieldRef)
59 {
60 }
61
62 ToolsEnumFieldImpl(const ToolsEnumFieldImpl&) = default;
63 ToolsEnumFieldImpl(ToolsEnumFieldImpl&&) = default;
64 virtual ~ToolsEnumFieldImpl() noexcept = default;
65
66 ToolsEnumFieldImpl& operator=(const ToolsEnumFieldImpl&) = delete;
67
68protected:
69 virtual Ptr cloneImpl() override
70 {
71 return ActPtr(new ToolsEnumFieldImpl<TField>(Base::field()));
72 }
73
74 const ValueInfosList& valuesImpl() const override
75 {
76 using Tag =
77 std::conditional_t<
78 std::is_same_v<typename Field::ValueNameInfo, const char*>,
79 SeqValuesTag,
80 SparseValuesTag
81 >;
82
83 static const ValueInfosList List(createValues(Tag()));
84 return List;
85 }
86
87private:
88 struct SeqValuesTag{};
89 struct SparseValuesTag{};
90
91 static ValueInfosList createValues(SeqValuesTag)
92 {
93 ValueInfosList result;
94 auto namesMap = Field::valueNamesMap();
95 for (auto idx = 0U; idx < namesMap.second; ++idx) {
96 auto* name = namesMap.first[idx];
97 if (name == nullptr) {
98 continue;
99 }
100
101 result.append(qMakePair(name, static_cast<long long>(idx)));
102 }
103 return result;
104 }
105
106 static ValueInfosList createValues(SparseValuesTag)
107 {
108 ValueInfosList result;
109 auto namesMap = Field::valueNamesMap();
110 for (auto idx = 0U; idx < namesMap.second; ++idx) {
111 auto& info = namesMap.first[idx];
112 result.append(qMakePair(info.second, static_cast<long long>(info.first)));
113 }
114 return result;
115 }
116};
117
118template <typename TField>
119auto makeEnumField(TField& field)
120{
121 return std::make_unique<ToolsEnumFieldImpl<TField>>(field);
122}
123
124} // namespace details
125
126} // namespace cc_tools_qt
Main namespace for all classes / functions of the shared library.