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