0.4.1
Loading...
Searching...
No Matches
TimeSystem.hpp
Go to the documentation of this file.
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>
21using json = nlohmann::json; ///< json namespace
22
23namespace NAV
24{
25/// @brief List of all time systems
26enum 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
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 constexpr TimeSystem(TimeSystem_ type) // NOLINT(hicpp-explicit-conversions, google-explicit-constructor)
48 : value(type)
49 {}
50
51 /// @brief Construct new object from std::string
52 /// @param[in] typeString String representation of the TimeSystem
53 static TimeSystem fromString(const std::string& typeString)
54 {
55 if (typeString == "UTC")
56 {
57 return UTC;
58 }
59 if (typeString == "GPST" || typeString == "GPS")
60 {
61 return GPST;
62 }
63 if (typeString == "GLNT" || typeString == "GLO")
64 {
65 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
91 {
92 value = v;
93 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 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
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 {
165 value = UTC;
166 break;
168 value = GPST;
169 break;
171 value = GLNT;
172 break;
174 value = GST;
175 break;
177 value = BDT;
178 break;
180 value = QZSST;
181 break;
183 value = IRNSST;
184 break;
185 default:
187 break;
188 }
189 }
190
191 private:
192 /// @brief Internal value
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
199bool 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
204const 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.
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.
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.
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.
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.
243constexpr 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.
252constexpr 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.
261constexpr 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.
270constexpr 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.
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.
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.
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.
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.
313constexpr 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.
322constexpr 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.
331constexpr 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.
340constexpr 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.
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.
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
365constexpr 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
370constexpr 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
375constexpr 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
381constexpr 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
386constexpr 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
391constexpr 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
396void 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
400void from_json(const json& j, TimeSystem& timeSystem);
401
402} // namespace NAV
403
404namespace std
405{
406/// @brief Hash function for TimeSystem (needed for unordered_map)
407template<>
408struct hash<NAV::TimeSystem>
409{
410 /// @brief Hash function for TimeSystem
411 /// @param[in] f Time system
412 std::size_t operator()(const NAV::TimeSystem& f) const
413 {
414 return std::hash<NAV::TimeSystem_>{}(NAV::TimeSystem_(f));
415 }
416};
417} // namespace std
418
419#ifndef DOXYGEN_IGNORE
420
421/// @brief Formatter for TimeSystem
422template<>
423struct 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
442std::ostream& operator<<(std::ostream& os, const NAV::TimeSystem& obj);
nlohmann::json json
json namespace
std::ostream & operator<<(std::ostream &os, const NAV::TimeSystem &obj)
Stream insertion operator overload.
Time System defintions.
TimeSystemEnum
Continuous enum for the time systems.
@ IRNSST
Indian Regional Navigation Satellite System Time.
@ COUNT
Amount of items in the enum.
@ GST
Galileo System Time.
@ UTC
Coordinated Universal Time.
@ GLNT
GLONASS Time (GLONASST)
@ QZSST
Quasi-Zenith Satellite System Time.
friend constexpr bool operator==(const TimeSystem &lhs, const TimeSystem &rhs)
Equal compares values.
static TimeSystem fromString(const std::string &typeString)
Construct new object from std::string.
TimeSystem_ value
Internal value.
constexpr TimeSystem(TimeSystemEnum timeSystem)
TimeSystemEnum getEnumValue() const
Returns the enum value (only one must be set)
constexpr const char * toString() const
Converts the time system into a string.
constexpr TimeSystem()=default
Default Constructor.
constexpr TimeSystem(TimeSystem_ type)
Implicit Constructor from Value type.
static TimeSystemEnum GetTimeSystemEnumValue(TimeSystem timeSystem)
Returns the enum value (only one must be set)
constexpr TimeSystem & operator=(TimeSystem_ v)
Assignment operator from Value type.
TimeSystem_
List of all time systems.
@ IRNSST
Indian Regional Navigation Satellite System Time.
@ GST
Galileo System Time.
@ BDT
BeiDou Time.
@ GLNT
GLONASS Time (GLONASST)
@ TimeSys_None
No Time system.
@ QZSST
Quasi-Zenith Satellite System Time.
@ GPST
GPS Time.
@ UTC
Coordinated Universal Time.
void to_json(json &j, const Node &node)
Converts the provided node into a json object.
Definition Node.cpp:990
const char * to_string(gui::widgets::PositionWithFrame::ReferenceFrame refFrame)
Converts the enum to a string.
constexpr Frequency_ operator~(Frequency_ rhs)
Allows negating flags of the Frequency enum.
constexpr Frequency_ & operator&=(Frequency_ &lhs, const Frequency_ &rhs)
Allows combining flags of the Frequency enum.
Code operator&(const Code &lhs, const Code &rhs)
Definition Code.cpp:697
void from_json(const json &j, Node &node)
Converts the provided json object into a node object.
Definition Node.cpp:1007
constexpr Frequency_ & operator|=(Frequency_ &lhs, const Frequency_ &rhs)
Allows combining flags of the Frequency enum.
constexpr bool operator!=(const Node::Kind &lhs, const Node::Kind &rhs)
Inequal compares Node::Kind values.
Definition Node.hpp:506
bool ComboTimeSystem(const char *label, TimeSystem &timeSystem)
Shows a ComboBox to select the time system.
constexpr bool operator==(const Node::Kind &lhs, const Node::Kind &rhs)
Equal compares Node::Kind values.
Definition Node.hpp:501
Code operator|(const Code &lhs, const Code &rhs)
Definition Code.cpp:673
std::size_t operator()(const NAV::TimeSystem &f) const
Hash function for TimeSystem.