dlgTextEntry_Keyboard: rename to TouchTextEntry
[xcsoar.git] / src / NMEA / Validity.hpp
blob87c1d0ca6422311f2c3443938a9cc8c7b869daa9
1 /*
2 Copyright_License {
4 XCSoar Glide Computer - http://www.xcsoar.org/
5 Copyright (C) 2000-2013 The XCSoar Project
6 A detailed list of copyright holders can be found in the file "AUTHORS".
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #ifndef XCSOAR_VALIDITY_HPP
25 #define XCSOAR_VALIDITY_HPP
27 #include "Math/fixed.hpp"
29 #include <type_traits>
31 #include <stdint.h>
33 /**
34 * This keeps track when a value was last changed, to check if it was
35 * updated recently or to see if it has expired. Additionally, it can
36 * track if the attribute is not set (timestamp is zero).
38 class Validity {
39 static constexpr int BITS = 6;
41 uint32_t last;
43 constexpr
44 static uint32_t Import(fixed time) {
45 #ifdef FIXED_MATH
46 return (uint32_t)(time << BITS);
47 #else
48 return (uint32_t)(time * (1 << BITS));
49 #endif
52 constexpr
53 static uint32_t Import(unsigned time) {
54 return (uint32_t)(time << BITS);
57 constexpr
58 static fixed Export(uint32_t i) {
59 #ifdef FIXED_MATH
60 return fixed(i) >> BITS;
61 #else
62 return fixed(i) / (1 << BITS);
63 #endif
66 public:
67 /**
68 * Cheap default constructor without initialization.
70 Validity() = default;
72 /**
73 * Initialize the object with the specified timestamp.
75 explicit constexpr Validity(fixed _last):last(Import(_last)) {}
77 public:
78 /**
79 * Clears the time stamp, marking the referenced value "invalid".
81 void Clear() {
82 last = 0;
85 /**
86 * Updates the time stamp, setting it to the current clock. This
87 * marks the referenced value as "valid".
89 * @param now the current time stamp in seconds
91 void Update(fixed now) {
92 last = Import(now);
95 /**
96 * Checks if the time stamp has expired, and calls clear() if so.
98 * @param now the current time stamp in seconds
99 * @param max_age the maximum age in seconds
100 * @return true if the value is expired
102 bool Expire(fixed _now, fixed _max_age) {
103 const uint32_t now = Import(_now);
104 const uint32_t max_age = Import(_max_age);
106 if (IsValid() &&
107 (now < last || /* time warp? */
108 now > last + max_age)) { /* expired? */
109 Clear();
110 return true;
111 } else
112 /* not expired */
113 return false;
117 * Checks if the time stamp is older than the given time.
119 * @param now the current time stamp in seconds
120 * @param max_age the maximum age in seconds
121 * @return true if the value is expired
123 bool IsOlderThan(fixed _now, fixed _max_age) const {
124 if (!IsValid())
125 return true;
127 const uint32_t now = Import(_now);
128 const uint32_t max_age = Import(_max_age);
130 return (now < last || /* time warp? */
131 now > last + max_age); /* expired? */
134 constexpr bool IsValid() const {
135 return last > 0;
139 * This function calculates the time difference of the two Validity objects
140 * @param other The second Validity object
141 * @return The time difference in seconds
143 fixed GetTimeDifference(const Validity &other) const {
144 assert(IsValid());
145 assert(other.IsValid());
147 return Export(last - other.last);
151 * Was the value modified since the time the "other" object was
152 * taken?
154 constexpr bool Modified(const Validity &other) const {
155 return last > other.last;
158 constexpr bool operator==(const Validity &other) const {
159 return last == other.last;
162 constexpr bool operator!=(const Validity &other) const {
163 return last != other.last;
166 bool Complement(const Validity &other) {
167 if (!IsValid() && other.IsValid()) {
168 *this = other;
169 return true;
170 } else
171 return false;
175 * Check this stored Validity object for a time warp and clear if it
176 * one has occurred. If this object is invalid, it is not
177 * considered a time warp, even if the current object is valid.
179 * @param current the current "real" time stamp
180 * @param max_period if time in "current" has advanced more than
181 * this number of seconds, consider this a time warp, too
182 * @return true if a time warp has occurred and this object has been
183 * cleared, false if this object is within range
185 bool FixTimeWarp(const Validity &current, unsigned max_period=300) {
186 if (!IsValid())
187 return false;
189 if (last + Import(max_period) < current.last || last > current.last) {
190 /* out of range, this is a time warp */
191 Clear();
192 return true;
195 return false;
198 constexpr operator bool() const {
199 return IsValid();
203 static_assert(std::is_trivial<Validity>::value, "type is not trivial");
205 #endif