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