nmealib 0.0.4
NMEA 0183/NMEA 2000 parsing library
Loading...
Searching...
No Matches
dataTypes.h
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <cmath>
5#include <cstdint>
6#include <iomanip>
7#include <limits>
8#include <sstream>
9#include <string>
10#include <type_traits>
11
12namespace nmealib {
13namespace nmea2000 {
14
55template<typename Traits>
56class DataType {
57public:
59 static constexpr double MIN = Traits::MIN;
61 static constexpr double MAX = Traits::MAX;
63 static constexpr double RESOLUTION = Traits::RESOLUTION;
65 using RawType = typename Traits::RawType;
67 using TargetType = typename Traits::TargetType;
68
78 const double physicalValue = static_cast<double>(value);
79 const double clampedValue = std::clamp(physicalValue, MIN, MAX);
80
81 double scaled = clampedValue - MIN;
82 scaled /= (MAX - MIN);
83 scaled *= (static_cast<double>(Traits::RAW_MAX) - static_cast<double>(Traits::RAW_MIN));
84 scaled += static_cast<double>(Traits::RAW_MIN);
85 scaled = std::round(scaled / RESOLUTION) * RESOLUTION;
86
87 return DataType(static_cast<RawType>(scaled));
88 }
89
97 static constexpr DataType fromRaw(RawType raw) noexcept {
98 return DataType(raw);
99 }
100
105 constexpr bool isValid() const noexcept {
106 return value_ >= Traits::RAW_MIN && value_ <= Traits::RAW_MAX;
107 }
108
113 constexpr TargetType getValue() const noexcept {
114 double scaled = static_cast<double>(value_) - static_cast<double>(Traits::RAW_MIN);
115 scaled /= (static_cast<double>(Traits::RAW_MAX) - static_cast<double>(Traits::RAW_MIN));
116 scaled *= (MAX - MIN);
117 scaled += MIN;
118 return static_cast<TargetType>(scaled);
119 }
120
128 RawType getRaw() const noexcept {
129 return value_;
130 }
131
139 std::string toString() const {
140 std::ostringstream oss;
141 if constexpr (std::is_integral_v<TargetType>) {
142 oss << static_cast<long long>(getValue());
143 } else {
144 oss << std::fixed << std::setprecision(getPrecision()) << getValue();
145 }
146 return oss.str();
147 }
148
149 // Operators
150 constexpr bool operator==(const DataType& other) const noexcept {
151 return value_ == other.value_;
152 }
153 constexpr bool operator!=(const DataType& other) const noexcept {
154 return value_ != other.value_;
155 }
156 constexpr bool operator<(const DataType& other) const noexcept {
157 return value_ < other.value_;
158 }
159 constexpr bool operator<=(const DataType& other) const noexcept {
160 return value_ <= other.value_;
161 }
162 constexpr bool operator>(const DataType& other) const noexcept {
163 return value_ > other.value_;
164 }
165 constexpr bool operator>=(const DataType& other) const noexcept {
166 return value_ >= other.value_;
167 }
168
169private:
171 RawType value_;
172
173 // Block default construction without parameters to enforce explicit initialization
174 constexpr explicit DataType(RawType value) noexcept : value_(value) {}
175
180 static constexpr int getPrecision() noexcept {
181 if constexpr (RESOLUTION >= 1.0) return 0;
182 int p = 0;
183 double r = RESOLUTION;
184 while (r < 1.0 && p < 15) { r *= 10; p++; }
185 return p;
186 }
187};
188
190 static constexpr double MIN = 0;
191 static constexpr double MAX = 4.295e7;
192 static constexpr double RESOLUTION = 1e-2;
193 using RawType = uint32_t;
194 using TargetType = float;
195 static constexpr RawType RAW_MIN = std::numeric_limits<RawType>::min();
196 static constexpr RawType RAW_MAX = std::numeric_limits<RawType>::max();
197};
199 static constexpr double MIN = 0;
200 static constexpr double MAX = 655.32;
201 static constexpr double RESOLUTION = 1e-2;
202 using RawType = uint16_t;
203 using TargetType = float;
204 static constexpr RawType RAW_MIN = std::numeric_limits<RawType>::min();
205 static constexpr RawType RAW_MAX = std::numeric_limits<RawType>::max();
206};
208 static constexpr double MIN = 0;
209 static constexpr double MAX = 2 * M_PI;
210 static constexpr double RESOLUTION = 1e-4;
211 using RawType = uint16_t;
212 using TargetType = float;
213 static constexpr RawType RAW_MIN = std::numeric_limits<RawType>::min();
214 static constexpr RawType RAW_MAX = std::numeric_limits<RawType>::max();
215};
217 static constexpr double MIN = -M_PI;
218 static constexpr double MAX = M_PI;
219 static constexpr double RESOLUTION = 1e-4;
220 using RawType = uint16_t;
221 using TargetType = float;
222 static constexpr RawType RAW_MIN = std::numeric_limits<RawType>::min();
223 static constexpr RawType RAW_MAX = std::numeric_limits<RawType>::max();
224};
226 static constexpr double MIN = 0;
227 static constexpr double MAX = 15;
228 static constexpr double RESOLUTION = 1;
229 using RawType = uint8_t;
230 using TargetType = uint8_t;
231 static constexpr RawType RAW_MIN = 0;
232 static constexpr RawType RAW_MAX = 15;
233};
235 static constexpr double MIN = 0;
236 static constexpr double MAX = 255;
237 static constexpr double RESOLUTION = 1;
238 using RawType = uint8_t;
239 using TargetType = uint8_t;
240 static constexpr RawType RAW_MIN = std::numeric_limits<RawType>::min();
241 static constexpr RawType RAW_MAX = std::numeric_limits<RawType>::max();
242};
244 static constexpr double MIN = -90;
245 static constexpr double MAX = 90;
246 static constexpr double RESOLUTION = 1e-7;
247 using RawType = int32_t;
248 using TargetType = float;
249 static constexpr RawType RAW_MIN = -90 * static_cast<RawType>(1e7);
250 static constexpr RawType RAW_MAX = 90 * static_cast<RawType>(1e7);
251};
253 static constexpr double MIN = -180;
254 static constexpr double MAX = 180;
255 static constexpr double RESOLUTION = 1e-7;
256 using RawType = int32_t;
257 using TargetType = float;
258 static constexpr RawType RAW_MIN = -180 * static_cast<RawType>(1e7);
259 static constexpr RawType RAW_MAX = 180 * static_cast<RawType>(1e7);
260};
262 static constexpr double MIN = -67;
263 static constexpr double MAX = 67;
264 static constexpr double RESOLUTION = (1 / 32) * 1e-6;
265 using RawType = int32_t;
266 using TargetType = double;
267 static constexpr RawType RAW_MIN = std::numeric_limits<RawType>::min();
268 static constexpr RawType RAW_MAX = std::numeric_limits<RawType>::max();
269};
271 static constexpr double MIN = -327.64;
272 static constexpr double MAX = 327.64;
273 static constexpr double RESOLUTION = 1e-2;
274 using RawType = int16_t;
275 using TargetType = float;
276 static constexpr RawType RAW_MIN = std::numeric_limits<RawType>::min();
277 static constexpr RawType RAW_MAX = std::numeric_limits<RawType>::max();
278};
279
292
305
318
331
344
357
370
383
396
409
410} // namespace nmea2000
411} // namespace nmealib
Strongly typed numeric wrapper for NMEA 2000 custom data types.
Definition dataTypes.h:56
constexpr bool operator!=(const DataType &other) const noexcept
Definition dataTypes.h:153
typename Traits::RawType RawType
Raw external type.
Definition dataTypes.h:65
constexpr bool operator>(const DataType &other) const noexcept
Definition dataTypes.h:162
RawType getRaw() const noexcept
Converts physical value to raw integer representation.
Definition dataTypes.h:128
constexpr bool operator>=(const DataType &other) const noexcept
Definition dataTypes.h:165
static constexpr double MIN
Minimum representable value for this type.
Definition dataTypes.h:59
static constexpr double RESOLUTION
Resolution (least significant physical unit step).
Definition dataTypes.h:63
constexpr bool operator<=(const DataType &other) const noexcept
Definition dataTypes.h:159
constexpr bool isValid() const noexcept
Checks whether the stored value is within configured bounds.
Definition dataTypes.h:105
constexpr bool operator<(const DataType &other) const noexcept
Definition dataTypes.h:156
static constexpr DataType fromRaw(RawType raw) noexcept
Constructs from a raw value.
Definition dataTypes.h:97
static constexpr double MAX
Maximum representable value for this type.
Definition dataTypes.h:61
typename Traits::TargetType TargetType
Target type for higher-level fields that expect a specific physical type.
Definition dataTypes.h:67
constexpr bool operator==(const DataType &other) const noexcept
Definition dataTypes.h:150
static DataType fromValue(TargetType value)
Constructs from a physical value.
Definition dataTypes.h:77
constexpr TargetType getValue() const noexcept
Returns the normalized physical value.
Definition dataTypes.h:113
std::string toString() const
Converts the value to a fixed-point string.
Definition dataTypes.h:139
static constexpr RawType RAW_MAX
Definition dataTypes.h:277
static constexpr double MIN
Definition dataTypes.h:271
static constexpr double RESOLUTION
Definition dataTypes.h:273
static constexpr RawType RAW_MIN
Definition dataTypes.h:276
static constexpr double MAX
Definition dataTypes.h:272
static constexpr double MIN
Definition dataTypes.h:208
static constexpr RawType RAW_MIN
Definition dataTypes.h:213
static constexpr RawType RAW_MAX
Definition dataTypes.h:214
static constexpr double MAX
Definition dataTypes.h:209
static constexpr double RESOLUTION
Definition dataTypes.h:210
static constexpr double RESOLUTION
Definition dataTypes.h:264
static constexpr double MAX
Definition dataTypes.h:263
static constexpr double MIN
Definition dataTypes.h:262
static constexpr RawType RAW_MIN
Definition dataTypes.h:267
static constexpr RawType RAW_MAX
Definition dataTypes.h:268
static constexpr double MAX
Definition dataTypes.h:236
static constexpr RawType RAW_MAX
Definition dataTypes.h:241
static constexpr double MIN
Definition dataTypes.h:235
static constexpr double RESOLUTION
Definition dataTypes.h:237
static constexpr RawType RAW_MIN
Definition dataTypes.h:240
static constexpr double RESOLUTION
Definition dataTypes.h:192
static constexpr RawType RAW_MAX
Definition dataTypes.h:196
static constexpr double MIN
Definition dataTypes.h:190
static constexpr double MAX
Definition dataTypes.h:191
static constexpr RawType RAW_MIN
Definition dataTypes.h:195
static constexpr RawType RAW_MIN
Definition dataTypes.h:231
static constexpr RawType RAW_MAX
Definition dataTypes.h:232
static constexpr double MIN
Definition dataTypes.h:226
static constexpr double RESOLUTION
Definition dataTypes.h:228
static constexpr double MAX
Definition dataTypes.h:227
static constexpr double MAX
Definition dataTypes.h:245
static constexpr RawType RAW_MIN
Definition dataTypes.h:249
static constexpr double MIN
Definition dataTypes.h:244
static constexpr RawType RAW_MAX
Definition dataTypes.h:250
static constexpr double RESOLUTION
Definition dataTypes.h:246
static constexpr RawType RAW_MIN
Definition dataTypes.h:258
static constexpr double MAX
Definition dataTypes.h:254
static constexpr RawType RAW_MAX
Definition dataTypes.h:259
static constexpr double RESOLUTION
Definition dataTypes.h:255
static constexpr double MIN
Definition dataTypes.h:253
static constexpr RawType RAW_MAX
Definition dataTypes.h:223
static constexpr double MIN
Definition dataTypes.h:217
static constexpr double RESOLUTION
Definition dataTypes.h:219
static constexpr RawType RAW_MIN
Definition dataTypes.h:222
static constexpr double MAX
Definition dataTypes.h:218
static constexpr double MAX
Definition dataTypes.h:200
static constexpr RawType RAW_MIN
Definition dataTypes.h:204
static constexpr RawType RAW_MAX
Definition dataTypes.h:205
static constexpr double RESOLUTION
Definition dataTypes.h:201
static constexpr double MIN
Definition dataTypes.h:199