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>
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).
39 static constexpr int BITS
= 6;
44 static uint32_t Import(fixed time
) {
46 return (uint32_t)(time
<< BITS
);
48 return (uint32_t)(time
* (1 << BITS
));
53 static uint32_t Import(unsigned time
) {
54 return (uint32_t)(time
<< BITS
);
58 static fixed
Export(uint32_t i
) {
60 return fixed(i
) >> BITS
;
62 return fixed(i
) / (1 << BITS
);
68 * Cheap default constructor without initialization.
73 * Initialize the object with the specified timestamp.
75 explicit constexpr Validity(fixed _last
):last(Import(_last
)) {}
79 * Clears the time stamp, marking the referenced value "invalid".
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
) {
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
);
107 (now
< last
|| /* time warp? */
108 now
> last
+ max_age
)) { /* expired? */
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 {
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 {
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 {
145 assert(other
.IsValid());
147 return Export(last
- other
.last
);
151 * Was the value modified since the time the "other" object was
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()) {
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
¤t
, unsigned max_period
=300) {
189 if (last
+ Import(max_period
) < current
.last
|| last
> current
.last
) {
190 /* out of range, this is a time warp */
198 constexpr operator bool() const {
203 static_assert(std::is_trivial
<Validity
>::value
, "type is not trivial");