cc_tools_qt
Common Environment for Protocol Analysis.
Loading...
Searching...
No Matches
FloatValueWrapper.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 <cmath>
25#include <limits>
26
27#include "comms/field/IntValue.h"
28#include "NumericValueWrapper.h"
29
30namespace cc_tools_qt
31{
32
33namespace field_wrapper
34{
35
36class CC_API FloatValueWrapper : public NumericValueWrapper<double>
37{
38 typedef NumericValueWrapper<double> Base;
39public:
40
41 typedef Base::UnderlyingType UnderlyingType;
42 typedef std::unique_ptr<FloatValueWrapper> Ptr;
43
44 FloatValueWrapper();
45 virtual ~FloatValueWrapper() noexcept;
46
47 Ptr clone();
48 bool isNan() const;
49 void setNan();
50 bool isInf() const;
51 void setInf();
52 bool isMinusInf() const;
53 void setMinusInf();
54 double getEpsilon() const;
55
56protected:
57 virtual Ptr cloneImpl() = 0;
58 virtual bool isNanImpl() const = 0;
59 virtual void setNanImpl() = 0;
60 virtual bool isInfImpl() const = 0;
61 virtual void setInfImpl() = 0;
62 virtual bool isMinusInfImpl() const = 0;
63 virtual void setMinusInfImpl() = 0;
64 virtual double getEpsilonImpl() const = 0;
65
66 virtual void dispatchImpl(FieldWrapperHandler& handler) override;
67};
68
69template <typename TField>
70class FloatValueWrapperT : public NumericValueWrapperT<FloatValueWrapper, TField>
71{
72 using Base = NumericValueWrapperT<FloatValueWrapper, TField>;
73 using Field = TField;
74 static_assert(comms::field::isFloatValue<Field>(), "Must be of FloatValueField type");
75
76public:
77
78 typedef typename Base::UnderlyingType UnderlyingType;
79 typedef typename Base::Ptr Ptr;
80
81 explicit FloatValueWrapperT(Field& fieldRef)
82 : Base(fieldRef)
83 {
84 }
85
86 FloatValueWrapperT(const FloatValueWrapperT&) = default;
87 FloatValueWrapperT(FloatValueWrapperT&&) = default;
88 virtual ~FloatValueWrapperT() noexcept = default;
89
90 FloatValueWrapperT& operator=(const FloatValueWrapperT&) = delete;
91
92protected:
93 virtual Ptr cloneImpl() override
94 {
95 return Ptr(new FloatValueWrapperT<TField>(Base::field()));
96 }
97
98 virtual bool isNanImpl() const override
99 {
100 return std::isnan(Base::field().getValue());
101 }
102
103 virtual void setNanImpl() override
104 {
105 Base::field().setValue(std::numeric_limits<typename TField::ValueType>::quiet_NaN());
106 }
107
108 virtual bool isInfImpl() const override
109 {
110 return std::isinf(Base::field().getValue()) && (0 < Base::field().getValue());
111 }
112
113 virtual void setInfImpl() override
114 {
115 Base::field().setValue(std::numeric_limits<typename TField::ValueType>::infinity());
116 }
117
118 virtual bool isMinusInfImpl() const override
119 {
120 return std::isinf(Base::field().getValue()) && (Base::field().getValue() < 0);
121 }
122
123 virtual void setMinusInfImpl() override
124 {
125 Base::field().setValue(-std::numeric_limits<typename TField::ValueType>::infinity());
126 }
127
128 virtual double getEpsilonImpl() const override
129 {
130 return static_cast<double>(std::numeric_limits<typename TField::ValueType>::epsilon());
131 }
132};
133
134using FloatValueWrapperPtr = FloatValueWrapper::Ptr;
135
136template <typename TField>
137FloatValueWrapperPtr
138makeFloatValueWrapper(TField& field)
139{
140 return
141 FloatValueWrapperPtr(
142 new FloatValueWrapperT<TField>(field));
143}
144
145} // namespace field_wrapper
146
147} // namespace cc_tools_qt
Main namespace for all classes / functions of the shared library.