cc_tools_qt
Common Environment for Protocol Analysis.
Loading...
Searching...
No Matches
BitmaskValueWrapper.h
1//
2// Copyright 2014 - 2024 (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 <cstdint>
22#include <cassert>
23#include <memory>
24#include <limits>
25
26#include "comms/field/BitmaskValue.h"
27#include "NumericValueWrapper.h"
28
29namespace cc_tools_qt
30{
31
32namespace field_wrapper
33{
34
35class CC_API BitmaskValueWrapper : public NumericValueWrapper<unsigned long long>
36{
37public:
38 typedef std::unique_ptr<BitmaskValueWrapper> Ptr;
39
40 virtual ~BitmaskValueWrapper() noexcept;
41
42 bool bitValue(unsigned idx) const;
43
44 void setBitValue(unsigned idx, bool value);
45
46 unsigned bitIdxLimit() const;
47
48 Ptr clone();
49
50protected:
51 virtual bool bitValueImpl(unsigned idx) const = 0;
52 virtual void setBitValueImpl(unsigned idx, bool value) = 0;
53 virtual unsigned bitIdxLimitImpl() const = 0;
54 virtual Ptr cloneImpl() = 0;
55
56 void dispatchImpl(FieldWrapperHandler& handler);
57};
58
59template <typename TField>
60class BitmaskValueWrapperT : public NumericValueWrapperT<BitmaskValueWrapper, TField>
61{
62 using Base = NumericValueWrapperT<BitmaskValueWrapper, TField>;
63 using Field = TField;
64 static_assert(comms::field::isBitmaskValue<Field>(), "Must be of BitmaskValueField type");
65
66 using ValueType = typename Field::ValueType;
67 using MaskType = typename Base::UnderlyingType;
68 static_assert(sizeof(ValueType) <= sizeof(MaskType), "This wrapper cannot handle provided field.");
69
70public:
71 typedef typename Base::Ptr Ptr;
72
73 explicit BitmaskValueWrapperT(Field& fieldRef)
74 : Base(fieldRef)
75 {
76 }
77
78 BitmaskValueWrapperT(const BitmaskValueWrapperT&) = default;
79 BitmaskValueWrapperT(BitmaskValueWrapperT&&) = default;
80 virtual ~BitmaskValueWrapperT() noexcept = default;
81
82 BitmaskValueWrapperT& operator=(const BitmaskValueWrapperT&) = delete;
83
84protected:
85 virtual bool bitValueImpl(unsigned idx) const override
86 {
87 return Base::field().getBitValue(idx);
88 }
89
90 virtual void setBitValueImpl(unsigned idx, bool value) override
91 {
92 Base::field().setBitValue(idx, value);
93 }
94
95 virtual unsigned bitIdxLimitImpl() const override
96 {
97 return std::numeric_limits<ValueType>::digits;
98 }
99
100 virtual Ptr cloneImpl() override
101 {
102 return Ptr(new BitmaskValueWrapperT<TField>(Base::field()));
103 }
104};
105
106using BitmaskValueWrapperPtr = BitmaskValueWrapper::Ptr;
107
108template <typename TField>
109BitmaskValueWrapperPtr
110makeBitmaskValueWrapper(TField& field)
111{
112 return
113 BitmaskValueWrapperPtr(
114 new BitmaskValueWrapperT<TField>(field));
115}
116
117} // namespace field_wrapper
118
119} // namespace cc_tools_qt
Main namespace for all classes / functions of the shared library.