xoreos  0.0.5
timestamp.h
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 /* Based on ScummVM (<http://scummvm.org>) code, which is released
26  * under the terms of version 2 or later of the GNU General Public
27  * License.
28  *
29  * The original copyright note in ScummVM reads as follows:
30  *
31  * ScummVM is the legal property of its developers, whose names
32  * are too numerous to list here. Please refer to the COPYRIGHT
33  * file distributed with this source distribution.
34  *
35  * This program is free software; you can redistribute it and/or
36  * modify it under the terms of the GNU General Public License
37  * as published by the Free Software Foundation; either version 2
38  * of the License, or (at your option) any later version.
39  *
40  * This program is distributed in the hope that it will be useful,
41  * but WITHOUT ANY WARRANTY; without even the implied warranty of
42  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43  * GNU General Public License for more details.
44  *
45  * You should have received a copy of the GNU General Public License
46  * along with this program; if not, write to the Free Software
47  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
48  */
49 
50 #ifndef COMMON_TIMESTAMP_H
51 #define COMMON_TIMESTAMP_H
52 
53 #include "src/common/types.h"
54 
55 namespace Common {
56 
57 class Rational;
58 
108 class Timestamp {
109 public:
116 
124 
131  Timestamp(uint64 secs, uint64 frames, const Rational &framerate);
132 
138  Timestamp convertToFramerate(uint64 newFramerate) const;
139 
145  bool operator==(const Timestamp &ts) const;
146  bool operator!=(const Timestamp &ts) const;
147  bool operator<(const Timestamp &ts) const;
148  bool operator<=(const Timestamp &ts) const;
149  bool operator>(const Timestamp &ts) const;
150  bool operator>=(const Timestamp &ts) const;
151 
157  Timestamp addFrames(int64 frames) const;
158 
164  Timestamp addMsecs(int64 msecs) const;
165 
166 
167  // unary minus
168  Timestamp operator-() const;
169 
174  Timestamp operator+(const Timestamp &ts) const;
175 
180  Timestamp operator-(const Timestamp &ts) const;
181 
187  int64 frameDiff(const Timestamp &ts) const;
188 
190  int64 msecsDiff(const Timestamp &ts) const;
191 
196  int64 msecs() const;
197 
202  inline int64 secs() const {
203  return _secs;
204  }
205 
209  inline int64 totalNumberOfFrames() const {
211  }
212 
218  inline int64 numberOfFrames() const {
220  }
221 
223  inline uint64 framerate() const { return _framerate / _framerateFactor; }
224 
225 protected:
230  int64 cmp(const Timestamp &ts) const;
231 
236  void normalize();
237 
241  void addIntern(const Timestamp &ts);
242 
243 protected:
251 
265 
274 
280 };
281 
282 
283 } // End of namespace Common
284 
285 #endif
int64 frameDiff(const Timestamp &ts) const
Computes the number of frames between this timestamp and ts.
Definition: timestamp.cpp:231
int64 totalNumberOfFrames() const
Return the time in frames described by this timestamp.
Definition: timestamp.h:209
Definition: 2dafile.h:39
A simple rational class that holds fractions.
Definition: rational.h:56
uint64 framerate() const
Return the framerate used by this timestamp.
Definition: timestamp.h:223
uint64_t uint64
Definition: types.h:206
uint64 _framerate
The internal framerate, i.e.
Definition: timestamp.h:273
bool operator==(const Timestamp &ts) const
Check whether to timestamps describe the exact same moment in time.
Definition: timestamp.cpp:138
int64 msecsDiff(const Timestamp &ts) const
Computes the number off milliseconds between this timestamp and ts.
Definition: timestamp.cpp:257
bool operator!=(const Timestamp &ts) const
Definition: timestamp.cpp:142
int64 _secs
The seconds part of this timestamp.
Definition: timestamp.h:250
bool operator>=(const Timestamp &ts) const
Definition: timestamp.cpp:158
bool operator<=(const Timestamp &ts) const
Definition: timestamp.cpp:150
Timestamp operator-() const
Definition: timestamp.cpp:211
Timestamp(uint64 msecs=0, uint64 framerate=1)
Set up a timestamp with a given time and framerate.
Definition: timestamp.cpp:57
Timestamp addMsecs(int64 msecs) const
Returns a new timestamp, which corresponds to the time encoded by this timestamp with the given numbe...
Definition: timestamp.cpp:190
Timestamp operator+(const Timestamp &ts) const
Compute the sum of two timestamps.
Definition: timestamp.cpp:219
uint64 _framerateFactor
Factor by which the original framerate specified by the client code was multipled to obtain the inter...
Definition: timestamp.h:279
int64 cmp(const Timestamp &ts) const
Compare this timestamp to another one and return a value similar to strcmp.
Definition: timestamp.cpp:162
Low-level type definitions to handle fixed width types portably.
void normalize()
Normalize this timestamp by making _numFrames non-negative and reducing it modulo _framerate...
Definition: timestamp.cpp:124
int64 secs() const
Return the time in seconds described by this timestamp, rounded down.
Definition: timestamp.h:202
int64 numberOfFrames() const
A timestamp consists of a number of seconds, plus a number of frames, the latter describing a fractio...
Definition: timestamp.h:218
int64_t int64
Definition: types.h:205
bool operator>(const Timestamp &ts) const
Definition: timestamp.cpp:154
bool operator<(const Timestamp &ts) const
Definition: timestamp.cpp:146
Timestamps allow specifying points in time and measuring time intervals with a sub-millisecond granul...
Definition: timestamp.h:108
int64 msecs() const
Return the time in milliseconds described by this timestamp, rounded down.
Definition: timestamp.cpp:261
int64 _numFrames
The number of frames which together with _secs encodes the timestamp.
Definition: timestamp.h:264
Timestamp addFrames(int64 frames) const
Returns a new timestamp, which corresponds to the time encoded by this timestamp with the given numbe...
Definition: timestamp.cpp:179
Timestamp convertToFramerate(uint64 newFramerate) const
Return a timestamp which represents as closely as possible the point in time describes by this timest...
Definition: timestamp.cpp:98
void addIntern(const Timestamp &ts)
Add another timestamp to this one and normalize the result.
Definition: timestamp.cpp:201