cc_tools_qt
Common Environment for Protocol Analysis.
Loading...
Searching...
No Matches
NumericValueWrapper.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 <type_traits>
25#include <limits>
26#include <algorithm>
27#include <iterator>
28
29#include "FieldWrapper.h"
30
31namespace cc_tools_qt
32{
33
34namespace field_wrapper
35{
36
37template <typename TUnderlyingType>
38class NumericValueWrapper : public FieldWrapper
39{
40 static_assert(std::is_integral<TUnderlyingType>::value || std::is_floating_point<TUnderlyingType>::value,
41 "Underlying type is expected to be integral or floating point.");
42 typedef FieldWrapper Base;
43public:
44 typedef TUnderlyingType UnderlyingType;
45 typedef Base::SerialisedSeq SerialisedSeq;
46
47 NumericValueWrapper() {}
48
49 virtual ~NumericValueWrapper() noexcept = default;
50
51 UnderlyingType getValue() const
52 {
53 return getValueImpl();
54 }
55
56 void setValue(UnderlyingType value)
57 {
58 setValueImpl(value);
59 }
60
61 std::size_t minLength() const
62 {
63 return minLengthImpl();
64 }
65
66 std::size_t maxLength() const
67 {
68 return maxLengthImpl();
69 }
70
71 int minWidth() const
72 {
73 return static_cast<int>(minLength()) * 2;
74 }
75
76 int maxWidth() const
77 {
78 return static_cast<int>(maxLength()) * 2;
79 }
80
81private:
82 virtual UnderlyingType getValueImpl() const = 0;
83 virtual void setValueImpl(UnderlyingType value) = 0;
84 virtual std::size_t minLengthImpl() const = 0;
85 virtual std::size_t maxLengthImpl() const = 0;
86};
87
88template <typename TBase, typename TField>
89class NumericValueWrapperT : public FieldWrapperT<TBase, TField>
90{
91 using Base = FieldWrapperT<TBase, TField>;
92
93public:
94 using UnderlyingType = typename Base::UnderlyingType;
95 using SerialisedSeq = typename Base::SerialisedSeq;
96
97protected:
98 using Field = TField;
99 using ValueType = typename Field::ValueType;
100
101 static_assert(sizeof(ValueType) <= sizeof(UnderlyingType), "This wrapper cannot handle provided field.");
102// static_assert(
103// std::is_signed<ValueType>::value == std::is_signed<UnderlyingType>::value ||
104// (sizeof(ValueType) < sizeof(UnderlyingType)),
105// "This wrapper cannot handle provided field.");
106
107public:
108 explicit NumericValueWrapperT(Field& fieldRef)
109 : Base(fieldRef)
110 {
111 static_assert(std::is_base_of<NumericValueWrapper<UnderlyingType>, NumericValueWrapperT<TBase, TField> >::value,
112 "Must inherit from NumericValueWrapper");
113 }
114
115 NumericValueWrapperT(const NumericValueWrapperT&) = default;
116 NumericValueWrapperT(NumericValueWrapperT&&) = default;
117 virtual ~NumericValueWrapperT() noexcept = default;
118
119 NumericValueWrapperT& operator=(const NumericValueWrapperT&) = delete;
120
121protected:
122
123 virtual UnderlyingType getValueImpl() const override
124 {
125 return static_cast<UnderlyingType>(Base::field().getValue());
126 }
127
128 virtual void setValueImpl(UnderlyingType value) override
129 {
130 Base::field().setValue(value);
131 }
132
133 virtual std::size_t minLengthImpl() const override
134 {
135 return Base::field().minLength();
136 }
137
138 virtual std::size_t maxLengthImpl() const override
139 {
140 return Base::field().maxLength();
141 }
142
143};
144
145} // namespace field_wrapper
146
147} // namespace cc_tools_qt
Main namespace for all classes / functions of the shared library.