libvisual  0.5.0
lv_time.h
1 /* Libvisual - The audio visualisation framework.
2  *
3  * Copyright (C) 2012 Libvisual team
4  * 2004-2006 Dennis Smit
5  *
6  * Authors: Chong Kai Xiong <kaixiong@codeleft.sg>
7  * Dennis Smit <ds@nerds-incorporated.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU Lesser General Public License as
11  * published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */
23 
24 #ifndef _LV_TIME_H
25 #define _LV_TIME_H
26 
27 #include <libvisual/lv_types.h>
28 #include <time.h>
29 
35 #define VISUAL_NSECS_PER_SEC 1000000000
36 #define VISUAL_USECS_PER_SEC 1000000
37 #define VISUAL_MSECS_PER_SEC 1000
38 #define VISUAL_USECS_PER_MSEC 1000
39 #define VISUAL_NSECS_PER_MSEC 1000000
40 #define VISUAL_NSECS_PER_USEC 1000
41 
42 #ifdef __cplusplus
43 
44 #include <memory>
45 #include <cmath>
46 
47 namespace LV {
48 
50  class LV_API Time
51  {
52  public:
53 
55  long sec;
56 
58  long nsec;
59 
60  explicit Time (long sec_ = 0, long nsec_ = 0)
61  : sec (sec_)
62  , nsec (nsec_)
63  {}
64 
65  static Time from_secs (double secs)
66  {
67  double int_part, frac_part;
68  frac_part = std::modf (secs, &int_part);
69 
70  return Time (int_part, frac_part * VISUAL_NSECS_PER_SEC);
71  }
72 
73  static Time from_msecs (uint64_t msecs)
74  {
75  return Time (msecs / VISUAL_MSECS_PER_SEC,
76  (msecs % VISUAL_MSECS_PER_SEC) * VISUAL_NSECS_PER_MSEC);
77  }
78 
79  static Time from_usecs (uint64_t usecs)
80  {
81  return Time (usecs / VISUAL_USECS_PER_SEC,
82  (usecs % VISUAL_USECS_PER_SEC) * VISUAL_NSECS_PER_USEC);
83  }
84 
85  static Time now ();
86 
87  friend Time operator- (Time const& lhs, Time const& rhs)
88  {
89  Time diff (lhs);
90  diff -= rhs;
91 
92  return diff;
93  }
94 
95  Time& operator-= (Time const& rhs)
96  {
97  sec -= rhs.sec;
98  nsec -= rhs.nsec;
99 
100  if (nsec < 0) {
101  sec--;
102  nsec += VISUAL_NSECS_PER_SEC;
103  }
104 
105  return *this;
106  }
107 
108  friend bool operator== (Time const& lhs, Time const& rhs)
109  {
110  return lhs.sec == rhs.sec && lhs.nsec == rhs.nsec;
111  }
112 
113  friend bool operator!= (Time const& lhs, Time const& rhs)
114  {
115  return !(lhs == rhs);
116  }
117 
118  friend bool operator>= (Time const& lhs, Time const& rhs)
119  {
120  return lhs.sec >= rhs.sec;
121  }
122 
123  friend bool operator<= (Time const& lhs, Time const& rhs)
124  {
125  return rhs >= lhs;
126  }
127 
128  friend bool operator> (Time const& lhs, Time const& rhs)
129  {
130  return (lhs.sec > rhs.sec) || (lhs.sec == rhs.sec && lhs.nsec > rhs.nsec);
131  }
132 
133  friend bool operator< (Time const& lhs, Time const& rhs)
134  {
135  return rhs > lhs;
136  }
137 
139  double to_secs () const
140  {
141  return sec + nsec * (1.0 / VISUAL_NSECS_PER_SEC);
142  }
143 
145  uint64_t to_msecs () const
146  {
147  return uint64_t (sec) * VISUAL_MSECS_PER_SEC + nsec / VISUAL_NSECS_PER_MSEC;
148  }
149 
151  uint64_t to_usecs () const
152  {
153  return uint64_t (sec) * VISUAL_USECS_PER_SEC + nsec / VISUAL_NSECS_PER_USEC;
154  }
155 
157  static void usleep (uint64_t usecs);
158  };
159 
160  class LV_API Timer
161  {
162  public:
163 
165  Timer ();
166 
168  Timer (Timer const& timer);
169 
171  Timer (Timer&& rhs);
172 
174  ~Timer ();
175 
177  Timer& operator= (Timer const& rhs);
178 
180  Timer& operator= (Timer&& rhs);
181 
183  bool is_active () const;
184 
186  void reset ();
187 
189  void start ();
190 
192  void stop ();
193 
194  Time get_start_time () const;
195 
196  Time get_end_time () const;
197 
199  Time elapsed () const;
200 
201  bool is_past (Time const& age) const;
202 
203  private:
204 
205  class Impl;
206  std::unique_ptr<Impl> m_impl;
207  };
208 
209 } // LV namespace
210 
211 #endif /* __cplusplus */
212 
213 #ifdef __cplusplus
214 
215 typedef LV::Time VisTime;
216 typedef LV::Timer VisTimer;
217 
218 #else
219 
220 typedef struct _VisTime VisTime;
221 struct _VisTime;
222 
223 typedef struct _VisTimer VisTimer;
224 struct _VisTimer;
225 
226 #endif /* __cplusplus */
227 
228 LV_BEGIN_DECLS
229 
230 LV_API VisTime *visual_time_new (void);
231 LV_API VisTime *visual_time_new_now (void);
232 LV_API VisTime *visual_time_new_with_values (long sec, long nsec);
233 LV_API VisTime *visual_time_clone (VisTime *src);
234 LV_API void visual_time_free (VisTime *time_);
235 
236 LV_API void visual_time_set (VisTime *time_, long sec, long usec);
237 LV_API void visual_time_copy (VisTime *dest, VisTime *src);
238 LV_API void visual_time_get_now (VisTime *time_);
239 
240 LV_API void visual_time_diff (VisTime *diff, VisTime *time1, VisTime *time2);
241 LV_API int visual_time_is_past (VisTime *time_, VisTime *ref);
242 
243 LV_API double visual_time_to_secs (VisTime *time_);
244 LV_API uint64_t visual_time_to_msecs (VisTime *time_);
245 LV_API uint64_t visual_time_to_usecs (VisTime *time_);
246 
247 LV_API void visual_usleep (uint64_t usecs);
248 
249 LV_API void visual_time_set_from_msecs (VisTime *time_, uint64_t msecs);
250 
251 LV_API VisTimer *visual_timer_new (void);
252 LV_API void visual_timer_free (VisTimer *timer);
253 
254 LV_API void visual_timer_reset (VisTimer *timer);
255 LV_API void visual_timer_start (VisTimer *timer);
256 LV_API void visual_timer_stop (VisTimer *timer);
257 LV_API void visual_timer_resume (VisTimer *timer);
258 LV_API int visual_timer_is_active (VisTimer *timer);
259 
260 LV_API void visual_timer_elapsed (VisTimer *timer, VisTime *time_);
261 LV_API uint64_t visual_timer_elapsed_msecs (VisTimer *timer);
262 LV_API uint64_t visual_timer_elapsed_usecs (VisTimer *timer);
263 LV_API double visual_timer_elapsed_secs (VisTimer *timer);
264 
265 LV_API int visual_timer_is_past (VisTimer *timer, VisTime *time_);
266 LV_API int visual_timer_is_past2 (VisTimer *timer, long sec, long nsec);
267 
268 // FIXME: Remove this
269 //#define visual_time_get_now() (clock() / (float)CLOCKS_PER_SEC * 1000)
270 
271 LV_END_DECLS
272 
277 #endif /* _LV_TIME_H */