KItinerary

vdvbasictypes.h
Go to the documentation of this file.
1/*
2 SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#pragma once
8
9#include <QDateTime>
10
11#include <cstdint>
12
13/** @file vdvbasictypes.h
14 * Low-level data types used in VDV ticket structs.
15 */
16
17namespace KItinerary {
18
19#pragma pack(push)
20#pragma pack(1)
21
22/** Two-digit BCD encoded number. */
23template <int N>
25{
26 static_assert(N > 0 && N <= 4);
27 uint8_t data[N];
28
29 inline constexpr uint32_t value() const
30 {
31 uint32_t v = 0;
32 for (int i = 0; i < N; ++i) {
33 v *= 100;
34 v += ((data[i] & 0xF0) >> 4) * 10 + (data[i] & 0x0F);
35 }
36 return v;
37 }
38
39 inline constexpr operator uint32_t() const { return value(); }
40};
41
42/** Date encoded as 8 BCD digits. */
44{
45 VdvBcdNumber<2> bcdYear;
46 VdvBcdNumber<1> bcdMonth;
47 VdvBcdNumber<1> bcdDay;
48
49 inline QDate value() const
50 {
51 return QDate(bcdYear, bcdMonth, bcdDay);
52 }
53
54 inline operator QDate() const { return value(); }
55 inline bool operator==(const QDate &other) const { return value() == other; }
56 inline bool operator!=(const QDate &other) const { return value() != other; }
57
58 // dummy assignment operator for compatibility with the Q_PROPERTY system
59 inline VdvBcdDate& operator=(const QDate&) { return *this; }
60};
61
62/** Big-endian numeric value. */
63template <int N>
65{
66 static_assert(N > 0 && N <= 4);
67 uint8_t data[N];
68
69 inline constexpr uint32_t value() const
70 {
71 uint32_t v = 0;
72 for (int i = 0; i < N; ++i) {
73 v <<= 8;
74 v |= data[i];
75 }
76 return v;
77 }
78
79 inline constexpr operator uint32_t() const { return value(); }
80
81 // dummy assignment operator for compatibility with the Q_PROPERTY system
82 inline VdvNumber<N>& operator=(uint32_t) { return *this; }
83};
84
85/** Date/time representation encoded in 4 byte. */
87{
88 VdvNumber<4> data;
89
90 inline QDateTime value() const
91 {
92 return QDateTime(
93 {
94 (int)((data & 0b1111'1110'0000'0000'0000'0000'0000'0000) >> 25) + 1990,
95 (int)(data & 0b0000'0001'1110'0000'0000'0000'0000'0000) >> 21,
96 (int)(data & 0b0000'0000'0001'1111'0000'0000'0000'0000) >> 16
97 }, {
98 (int)(data & 0b0000'0000'0000'0000'1111'1000'0000'0000) >> 11,
99 (int)(data & 0b0000'0000'0000'0000'0000'0111'1110'0000) >> 5,
100 (int)(data & 0b0000'0000'0000'0000'0000'0000'0001'1111) * 2
101 });
102 }
103
104 inline operator QDateTime() const { return value(); }
105 inline bool operator==(const QDateTime &other) const { return value() == other; }
106 inline bool operator!=(const QDateTime &other) const { return value() != other; }
107
108 // dummy assignment operator for compatibility with the Q_PROPERTY system
109 inline VdvDateTimeCompact& operator=(const QDateTime&) { return *this; }
110};
111
112#pragma pack(pop)
113
114}
115
Classes for reservation/travel data models, data extraction and data augmentation.
Definition berelement.h:17
Date encoded as 8 BCD digits.
Two-digit BCD encoded number.
Date/time representation encoded in 4 byte.
Big-endian numeric value.
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:56:37 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.