cc_tools_qt
Common Environment for Protocol Analysis.
Loading...
Searching...
No Matches
ToolsBitmaskFieldImpl.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/ToolsBitmaskField.h"
23
24#include "comms/field/BitmaskValue.h"
25
26#include <cassert>
27#include <cstdint>
28#include <limits>
29#include <memory>
30#include <type_traits>
31
32namespace cc_tools_qt
33{
34
35namespace details
36{
37
38template <typename TField>
39class ToolsBitmaskFieldImpl : public ToolsNumericFieldImpl<cc_tools_qt::field::ToolsBitmaskField, TField>
40{
41 using Base = ToolsNumericFieldImpl<cc_tools_qt::field::ToolsBitmaskField, TField>;
42 using Field = TField;
43 static_assert(comms::field::isBitmaskValue<Field>(), "Must be of BitmaskValueField type");
44
45 using ValueType = typename Field::ValueType;
46 using MaskType = typename Base::UnderlyingType;
47 static_assert(sizeof(ValueType) <= sizeof(MaskType), "This class cannot handle provided field.");
48
49public:
50 using Ptr = typename Base::Ptr;
51 using ActPtr = typename Base::ActPtr;
52
53 explicit ToolsBitmaskFieldImpl(Field& fieldRef)
54 : Base(fieldRef)
55 {
56 }
57
58 ToolsBitmaskFieldImpl(const ToolsBitmaskFieldImpl&) = default;
59 ToolsBitmaskFieldImpl(ToolsBitmaskFieldImpl&&) = default;
60 virtual ~ToolsBitmaskFieldImpl() noexcept = default;
61
62 ToolsBitmaskFieldImpl& operator=(const ToolsBitmaskFieldImpl&) = delete;
63
64protected:
65 virtual bool bitValueImpl(unsigned idx) const override
66 {
67 return Base::field().getBitValue(idx);
68 }
69
70 virtual void setBitValueImpl(unsigned idx, bool value) override
71 {
72 using Tag =
73 std::conditional_t<
74 Field::hasFixedValue(),
75 NoFeatureTag,
76 HasFeatureTag
77 >;
78
79 setBitValueInternal(idx, value, Tag());
80 }
81
82 virtual unsigned bitIdxLimitImpl() const override
83 {
84 return std::numeric_limits<ValueType>::digits;
85 }
86
87 virtual Ptr cloneImpl() override
88 {
89 return ActPtr(new ToolsBitmaskFieldImpl<TField>(Base::field()));
90 }
91
92 virtual const QStringList& bitsImpl() const override
93 {
94 static const QStringList Bits = createBitsList();
95 return Bits;
96 }
97
98private:
99 struct HasFeatureTag {};
100 struct NoFeatureTag {};
101
102 static QStringList createBitsList()
103 {
104 QStringList result;
105 for (auto idx = 0U; idx < Field::BitIdx_numOfValues; ++idx) {
106 auto* name = Field::bitName(idx);
107 if (name == nullptr) {
108 result.append(QString());
109 continue;
110 }
111
112 result.append(name);
113 }
114 return result;
115 }
116
117 void setBitValueInternal(unsigned idx, bool value, HasFeatureTag)
118 {
119 Base::field().setBitValue(idx, value);
120 }
121
122 void setBitValueInternal([[maybe_unused]] unsigned idx, [[maybe_unused]] bool value, NoFeatureTag)
123 {
124 [[maybe_unused]] static constexpr bool Must_not_be_called = false;
125 assert(Must_not_be_called);
126 }
127};
128template <typename TField>
129auto makeBitmaskField(TField& field)
130{
131 return std::make_unique<ToolsBitmaskFieldImpl<TField>>(field);
132}
133
134} // namespace details
135
136} // namespace cc_tools_qt
Main namespace for all classes / functions of the shared library.