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