cc_tools_qt
Common Environment for Protocol Analysis.
Loading...
Searching...
No Matches
IntValueWrapper.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
25#include "comms/field/IntValue.h"
26#include "NumericValueWrapper.h"
27
28namespace cc_tools_qt
29{
30
31namespace field_wrapper
32{
33
34class CC_API IntValueWrapper : public NumericValueWrapper<long long int>
35{
36 typedef NumericValueWrapper<long long int> Base;
37public:
38
39 typedef Base::UnderlyingType UnderlyingType;
40 typedef std::unique_ptr<IntValueWrapper> Ptr;
41
42 IntValueWrapper();
43 virtual ~IntValueWrapper() noexcept;
44
45 UnderlyingType minValue() const;
46
47 UnderlyingType maxValue() const;
48
49 double getScaled() const;
50
51 void setScaled(double value);
52
53 double scaleValue(UnderlyingType value) const;
54
55 bool isSigned() const;
56
57 std::size_t valueTypeSize() const;
58
59 Ptr clone();
60
61protected:
62 virtual UnderlyingType minValueImpl() const = 0;
63 virtual UnderlyingType maxValueImpl() const = 0;
64 virtual double getScaledImpl() const = 0;
65 virtual void setScaledImpl(double value) = 0;
66 virtual double scaleValueImpl(UnderlyingType value) const = 0;
67 virtual bool isSignedImpl() const = 0;
68 virtual std::size_t valueTypeSizeImpl() const = 0;
69 virtual Ptr cloneImpl() = 0;
70
71 void dispatchImpl(FieldWrapperHandler& handler);
72};
73
74template <typename TField>
75class IntValueWrapperT : public NumericValueWrapperT<IntValueWrapper, TField>
76{
77 using Base = NumericValueWrapperT<IntValueWrapper, TField>;
78 using Field = TField;
79 static_assert(comms::field::isIntValue<Field>(), "Must be of IntValueField type");
80
81public:
82
83 typedef typename Base::UnderlyingType UnderlyingType;
84 typedef typename Base::Ptr Ptr;
85
86 explicit IntValueWrapperT(Field& fieldRef)
87 : Base(fieldRef)
88 {
89 }
90
91 IntValueWrapperT(const IntValueWrapperT&) = default;
92 IntValueWrapperT(IntValueWrapperT&&) = default;
93 virtual ~IntValueWrapperT() noexcept = default;
94
95 IntValueWrapperT& operator=(const IntValueWrapperT&) = delete;
96
97protected:
98 virtual UnderlyingType minValueImpl() const override
99 {
100 return std::numeric_limits<typename Field::ValueType>::min();
101 }
102
103 virtual UnderlyingType maxValueImpl() const override
104 {
105 return std::numeric_limits<typename Field::ValueType>::max();
106 }
107
108 virtual double getScaledImpl() const override
109 {
110 return Base::field().template scaleAs<double>();
111 }
112
113 virtual void setScaledImpl(double value) override
114 {
115 Base::field().setScaled(value);
116 }
117
118 virtual double scaleValueImpl(UnderlyingType value) const override
119 {
120 Field fieldTmp;
121 fieldTmp.setValue(value);
122 return fieldTmp.template scaleAs<double>();
123 }
124
125 virtual bool isSignedImpl() const override
126 {
127 return std::is_signed<typename Field::ValueType>::value;
128 }
129
130 virtual std::size_t valueTypeSizeImpl() const override
131 {
132 return sizeof(typename Field::ValueType);
133 }
134
135 virtual Ptr cloneImpl() override
136 {
137 return Ptr(new IntValueWrapperT<TField>(Base::field()));
138 }
139};
140
141using IntValueWrapperPtr = IntValueWrapper::Ptr;
142
143template <typename TField>
144IntValueWrapperPtr
145makeIntValueWrapper(TField& field)
146{
147 return
148 IntValueWrapperPtr(
149 new IntValueWrapperT<TField>(field));
150}
151
152} // namespace field_wrapper
153
154} // namespace cc_tools_qt
Main namespace for all classes / functions of the shared library.