INSTINCT Code Coverage Report


Directory: src/
File: Navigation/Time/TimeSystem.hpp
Date: 2025-02-07 16:54:41
Exec Total Coverage
Lines: 18 79 22.8%
Functions: 7 12 58.3%
Branches: 11 55 20.0%

Line Branch Exec Source
1 // This file is part of INSTINCT, the INS Toolkit for Integrated
2 // Navigation Concepts and Training by the Institute of Navigation of
3 // the University of Stuttgart, Germany.
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
8
9 /// @file TimeSystem.hpp
10 /// @brief Time System defintions
11 /// @author T. Topp (topp@ins.uni-stuttgart.de)
12 /// @date 2022-04-26
13
14 #pragma once
15
16 #include <cstdint>
17 #include <string>
18 #include <fmt/format.h>
19
20 #include <nlohmann/json.hpp>
21 using json = nlohmann::json; ///< json namespace
22
23 namespace NAV
24 {
25 /// @brief List of all time systems
26 enum TimeSystem_ : uint8_t
27 {
28 TimeSys_None = 0x00, ///< No Time system
29 UTC = 0x01, ///< Coordinated Universal Time
30 GPST = 0x02, ///< GPS Time
31 GLNT = 0x04, ///< GLONASS Time (GLONASST)
32 GST = 0x08, ///< Galileo System Time
33 BDT = 0x10, ///< BeiDou Time
34 QZSST = 0x20, ///< Quasi-Zenith Satellite System Time
35 IRNSST = 0x40, ///< Indian Regional Navigation Satellite System Time
36 };
37
38 /// @brief Time System defintions
39 class TimeSystem
40 {
41 public:
42 /// @brief Default Constructor
43 constexpr TimeSystem() = default;
44
45 /// @brief Implicit Constructor from Value type
46 /// @param[in] type Value type to construct from
47 941842 constexpr TimeSystem(TimeSystem_ type) // NOLINT(hicpp-explicit-conversions, google-explicit-constructor)
48 941842 : value(type)
49 941842 {}
50
51 /// @brief Construct new object from std::string
52 /// @param[in] typeString String representation of the TimeSystem
53 60 static TimeSystem fromString(const std::string& typeString)
54 {
55
2/2
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 54 times.
60 if (typeString == "UTC")
56 {
57 6 return UTC;
58 }
59
6/6
✓ Branch 1 taken 48 times.
✓ Branch 2 taken 6 times.
✓ Branch 4 taken 45 times.
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 51 times.
✓ Branch 7 taken 3 times.
54 if (typeString == "GPST" || typeString == "GPS")
60 {
61 51 return GPST;
62 }
63
3/6
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3 times.
✗ Branch 7 not taken.
3 if (typeString == "GLNT" || typeString == "GLO")
64 {
65 3 return GLNT;
66 }
67 if (typeString == "GST" || typeString == "GAL")
68 {
69 return GST;
70 }
71 if (typeString == "BDT")
72 {
73 return BDT;
74 }
75 if (typeString == "QZSST" || typeString == "QZS")
76 {
77 return QZSST;
78 }
79 if (typeString == "IRNSST" || typeString == "IRN")
80 {
81 return IRNSST;
82 }
83
84 return TimeSys_None;
85 }
86
87 /// @brief Assignment operator from Value type
88 /// @param[in] v Value type to construct from
89 /// @return The Type type from the value type
90 431 constexpr TimeSystem& operator=(TimeSystem_ v)
91 {
92 431 value = v;
93 431 return *this;
94 }
95
96 friend constexpr bool operator==(const TimeSystem& lhs, const TimeSystem& rhs);
97
98 /// @brief Allow switch(TimeSystem_(type)) and comparisons
99 968564 constexpr explicit operator TimeSystem_() const { return value; }
100 /// @brief Prevent usage: if(...)
101 constexpr explicit operator bool() = delete;
102
103 /// @brief int conversion operator
104 /// @return A int representation of the type
105 constexpr explicit operator int() const { return int(value); }
106
107 /// @brief std::string conversion operator
108 /// @return A std::string representation of the type
109 explicit operator std::string() const { return toString(); }
110
111 /// @brief Converts the time system into a string
112 [[nodiscard]] constexpr const char* toString() const
113 {
114 switch (value)
115 {
116 case UTC:
117 return "UTC";
118 case GPST:
119 return "GPST";
120 case GLNT:
121 return "GLNT";
122 case GST:
123 return "GST";
124 case BDT:
125 return "BDT";
126 case QZSST:
127 return "QZSST";
128 case IRNSST:
129 return "IRNSST";
130 case TimeSys_None:
131 return "None";
132 }
133
134 return "None";
135 }
136
137 /// Continuous enum for the time systems
138 enum class TimeSystemEnum : uint8_t
139 {
140 TimeSys_None, ///< No Time system
141 UTC, ///< Coordinated Universal Time
142 GPST, ///< GPS Time
143 GLNT, ///< GLONASS Time (GLONASST)
144 GST, ///< Galileo System Time
145 BDT, ///< BeiDou Time
146 QZSST, ///< Quasi-Zenith Satellite System Time
147 IRNSST, ///< Indian Regional Navigation Satellite System Time
148 COUNT, ///< Amount of items in the enum
149 };
150
151 /// @brief Returns the enum value (only one must be set)
152 /// @param timeSystem Time system
153 static TimeSystemEnum GetTimeSystemEnumValue(TimeSystem timeSystem);
154
155 /// @brief Returns the enum value (only one must be set)
156 [[nodiscard]] TimeSystemEnum getEnumValue() const;
157
158 /// Constructor
159 /// @param[in] timeSystem Time system enum value
160 constexpr TimeSystem(TimeSystemEnum timeSystem) // NOLINT(hicpp-explicit-conversions, google-explicit-constructor)
161 {
162 switch (timeSystem)
163 {
164 case TimeSystemEnum::UTC:
165 value = UTC;
166 break;
167 case TimeSystemEnum::GPST:
168 value = GPST;
169 break;
170 case TimeSystemEnum::GLNT:
171 value = GLNT;
172 break;
173 case TimeSystemEnum::GST:
174 value = GST;
175 break;
176 case TimeSystemEnum::BDT:
177 value = BDT;
178 break;
179 case TimeSystemEnum::QZSST:
180 value = QZSST;
181 break;
182 case TimeSystemEnum::IRNSST:
183 value = IRNSST;
184 break;
185 default:
186 value = TimeSys_None;
187 break;
188 }
189 }
190
191 private:
192 /// @brief Internal value
193 TimeSystem_ value = TimeSystem_::TimeSys_None;
194 };
195
196 /// @brief Shows a ComboBox to select the time system
197 /// @param[in] label Label to show beside the combo box. This has to be a unique id for ImGui.
198 /// @param[in] timeSystem Reference to the time system to select
199 bool ComboTimeSystem(const char* label, TimeSystem& timeSystem);
200
201 /// @brief Converts the enum to a string
202 /// @param[in] timeSystem Enum value to convert into text
203 /// @return String representation of the enum
204 const char* to_string(TimeSystem::TimeSystemEnum timeSystem);
205
206 /// @brief Allows combining flags of the TimeSystem enum.
207 /// @param[in] lhs Left-hand side enum value.
208 /// @param[in] rhs Right-hand side enum value.
209 /// @return The binary ORed value.
210 constexpr TimeSystem_ operator|(TimeSystem_ lhs, TimeSystem_ rhs)
211 {
212 return TimeSystem_(int(lhs) | int(rhs));
213 }
214 /// @brief Allows combining flags of the TimeSystem enum.
215 /// @param[in] lhs Left-hand side enum value.
216 /// @param[in] rhs Right-hand side enum value.
217 /// @return The binary ORed value.
218 constexpr TimeSystem operator|(TimeSystem lhs, TimeSystem rhs)
219 {
220 return { TimeSystem_(lhs) | TimeSystem_(rhs) };
221 }
222 /// @brief Allows combining flags of the TimeSystem enum.
223 /// @param[in] lhs Left-hand side enum value.
224 /// @param[in] rhs Right-hand side enum value.
225 /// @return The binary ORed value.
226 constexpr TimeSystem operator|(TimeSystem_ lhs, TimeSystem rhs)
227 {
228 return { lhs | TimeSystem_(rhs) };
229 }
230 /// @brief Allows combining flags of the TimeSystem enum.
231 /// @param[in] lhs Left-hand side enum value.
232 /// @param[in] rhs Right-hand side enum value.
233 /// @return The binary ORed value.
234 constexpr TimeSystem operator|(TimeSystem lhs, TimeSystem_ rhs)
235 {
236 return { TimeSystem_(lhs) | rhs };
237 }
238
239 /// @brief Allows combining flags of the TimeSystem enum.
240 /// @param[in] lhs Left-hand side enum value.
241 /// @param[in] rhs Right-hand side enum value.
242 /// @return The binary ORed value.
243 constexpr TimeSystem_& operator|=(TimeSystem_& lhs, const TimeSystem_& rhs)
244 {
245 lhs = lhs | rhs;
246 return lhs;
247 }
248 /// @brief Allows combining flags of the TimeSystem enum.
249 /// @param[in] lhs Left-hand side enum value.
250 /// @param[in] rhs Right-hand side enum value.
251 /// @return The binary ORed value.
252 constexpr TimeSystem& operator|=(TimeSystem& lhs, const TimeSystem& rhs)
253 {
254 lhs = lhs | rhs;
255 return lhs;
256 }
257 /// @brief Allows combining flags of the TimeSystem enum.
258 /// @param[in] lhs Left-hand side enum value.
259 /// @param[in] rhs Right-hand side enum value.
260 /// @return The binary ORed value.
261 constexpr TimeSystem_& operator|=(TimeSystem_& lhs, const TimeSystem& rhs)
262 {
263 lhs = lhs | TimeSystem_(rhs);
264 return lhs;
265 }
266 /// @brief Allows combining flags of the TimeSystem enum.
267 /// @param[in] lhs Left-hand side enum value.
268 /// @param[in] rhs Right-hand side enum value.
269 /// @return The binary ORed value.
270 constexpr TimeSystem& operator|=(TimeSystem& lhs, const TimeSystem_& rhs)
271 {
272 lhs = lhs | rhs;
273 return lhs;
274 }
275
276 /// @brief Allows combining flags of the TimeSystem enum.
277 /// @param[in] lhs Left-hand side enum value.
278 /// @param[in] rhs Right-hand side enum value.
279 /// @return The binary ANDed value.
280 constexpr TimeSystem_ operator&(TimeSystem_ lhs, TimeSystem_ rhs)
281 {
282 return TimeSystem_(int(lhs) & int(rhs));
283 }
284 /// @brief Allows combining flags of the TimeSystem enum.
285 /// @param[in] lhs Left-hand side enum value.
286 /// @param[in] rhs Right-hand side enum value.
287 /// @return The binary ANDed value.
288 constexpr TimeSystem operator&(TimeSystem lhs, TimeSystem rhs)
289 {
290 return { TimeSystem_(lhs) & TimeSystem_(rhs) };
291 }
292 /// @brief Allows combining flags of the TimeSystem enum.
293 /// @param[in] lhs Left-hand side enum value.
294 /// @param[in] rhs Right-hand side enum value.
295 /// @return The binary ANDed value.
296 constexpr TimeSystem operator&(TimeSystem lhs, TimeSystem_ rhs)
297 {
298 return { TimeSystem_(lhs) & rhs };
299 }
300 /// @brief Allows combining flags of the TimeSystem enum.
301 /// @param[in] lhs Left-hand side enum value.
302 /// @param[in] rhs Right-hand side enum value.
303 /// @return The binary ANDed value.
304 constexpr TimeSystem operator&(TimeSystem_ lhs, TimeSystem rhs)
305 {
306 return { lhs & TimeSystem_(rhs) };
307 }
308
309 /// @brief Allows combining flags of the TimeSystem enum.
310 /// @param[in] lhs Left-hand side enum value.
311 /// @param[in] rhs Right-hand side enum value.
312 /// @return The binary ANDed value.
313 constexpr TimeSystem_& operator&=(TimeSystem_& lhs, const TimeSystem_& rhs)
314 {
315 lhs = lhs & rhs;
316 return lhs;
317 }
318 /// @brief Allows combining flags of the TimeSystem enum.
319 /// @param[in] lhs Left-hand side enum value.
320 /// @param[in] rhs Right-hand side enum value.
321 /// @return The binary ANDed value.
322 constexpr TimeSystem& operator&=(TimeSystem& lhs, const TimeSystem& rhs)
323 {
324 lhs = lhs & rhs;
325 return lhs;
326 }
327 /// @brief Allows combining flags of the TimeSystem enum.
328 /// @param[in] lhs Left-hand side enum value.
329 /// @param[in] rhs Right-hand side enum value.
330 /// @return The binary ANDed value.
331 constexpr TimeSystem_& operator&=(TimeSystem_& lhs, const TimeSystem& rhs)
332 {
333 lhs = lhs & TimeSystem_(rhs);
334 return lhs;
335 }
336 /// @brief Allows combining flags of the TimeSystem enum.
337 /// @param[in] lhs Left-hand side enum value.
338 /// @param[in] rhs Right-hand side enum value.
339 /// @return The binary ANDed value.
340 constexpr TimeSystem& operator&=(TimeSystem& lhs, const TimeSystem_& rhs)
341 {
342 lhs = lhs & rhs;
343 return lhs;
344 }
345
346 /// @brief Allows negating flags of the TimeSystem enum.
347 /// @param[in] rhs Right-hand side enum value.
348 /// @return The binary NEGed value.
349 constexpr TimeSystem operator~(TimeSystem_ rhs)
350 {
351 return { TimeSystem_(~int(rhs)) };
352 }
353 /// @brief Allows negating flags of the TimeSystem enum.
354 /// @param[in] rhs Right-hand side enum value.
355 /// @return The binary NEGed value.
356 constexpr TimeSystem operator~(TimeSystem rhs)
357 {
358 return { TimeSystem_(~int(rhs)) };
359 }
360
361 /// @brief Equal compares values
362 /// @param[in] lhs Left-hand side of the operator
363 /// @param[in] rhs Right-hand side of the operator
364 /// @return Whether the comparison was successful
365 6267 constexpr bool operator==(const TimeSystem& lhs, const TimeSystem& rhs) { return lhs.value == rhs.value; }
366 /// @brief Equal compares values
367 /// @param[in] lhs Left-hand side of the operator
368 /// @param[in] rhs Right-hand side of the operator
369 /// @return Whether the comparison was successful
370 48 constexpr bool operator==(const TimeSystem& lhs, const TimeSystem_& rhs) { return lhs == TimeSystem(rhs); }
371 /// @brief Equal compares values
372 /// @param[in] lhs Left-hand side of the operator
373 /// @param[in] rhs Right-hand side of the operator
374 /// @return Whether the comparison was successful
375 constexpr bool operator==(const TimeSystem_& lhs, const TimeSystem& rhs) { return TimeSystem(lhs) == rhs; }
376
377 /// @brief Inequal compares values
378 /// @param[in] lhs Left-hand side of the operator
379 /// @param[in] rhs Right-hand side of the operator
380 /// @return Whether the comparison was successful
381 constexpr bool operator!=(const TimeSystem& lhs, const TimeSystem& rhs) { return !(lhs == rhs); }
382 /// @brief Inequal compares values
383 /// @param[in] lhs Left-hand side of the operator
384 /// @param[in] rhs Right-hand side of the operator
385 /// @return Whether the comparison was successful
386 constexpr bool operator!=(const TimeSystem& lhs, const TimeSystem_& rhs) { return !(lhs == rhs); }
387 /// @brief Inequal compares values
388 /// @param[in] lhs Left-hand side of the operator
389 /// @param[in] rhs Right-hand side of the operator
390 /// @return Whether the comparison was successful
391 constexpr bool operator!=(const TimeSystem_& lhs, const TimeSystem& rhs) { return !(lhs == rhs); }
392
393 /// @brief Converts the provided TimeSystem into a json object
394 /// @param[out] j Return Json object
395 /// @param[in] timeSystem TimeSystem to convert
396 void to_json(json& j, const TimeSystem& timeSystem);
397 /// @brief Converts the provided json object into a TimeSystem
398 /// @param[in] j Json object with the time system
399 /// @param[out] timeSystem TimeSystem to return
400 void from_json(const json& j, TimeSystem& timeSystem);
401
402 } // namespace NAV
403
404 namespace std
405 {
406 /// @brief Hash function for TimeSystem (needed for unordered_map)
407 template<>
408 struct hash<NAV::TimeSystem>
409 {
410 /// @brief Hash function for TimeSystem
411 /// @param[in] f Time system
412 26440 std::size_t operator()(const NAV::TimeSystem& f) const
413 {
414 26440 return std::hash<NAV::TimeSystem_>{}(NAV::TimeSystem_(f));
415 }
416 };
417 } // namespace std
418
419 #ifndef DOXYGEN_IGNORE
420
421 /// @brief Formatter for TimeSystem
422 template<>
423 struct fmt::formatter<NAV::TimeSystem> : fmt::formatter<std::string>
424 {
425 /// @brief Defines how to format TimeSystem structs
426 /// @param[in] timeSys Struct to format
427 /// @param[in, out] ctx Format context
428 /// @return Output iterator
429 template<typename FormatContext>
430 auto format(const NAV::TimeSystem& timeSys, FormatContext& ctx) const
431 {
432 return fmt::formatter<std::string>::format(std::string(timeSys), ctx);
433 }
434 };
435
436 #endif
437
438 /// @brief Stream insertion operator overload
439 /// @param[in, out] os Output stream object to stream the time into
440 /// @param[in] obj Object to print
441 /// @return Returns the output stream object in order to chain stream insertions
442 std::ostream& operator<<(std::ostream& os, const NAV::TimeSystem& obj);
443