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