27 #include <libvisual/lv_types.h>
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
60 explicit Time (
long sec_ = 0,
long nsec_ = 0)
65 static Time from_secs (
double secs)
67 double int_part, frac_part;
68 frac_part = std::modf (secs, &int_part);
70 return Time (int_part, frac_part * VISUAL_NSECS_PER_SEC);
73 static Time from_msecs (uint64_t msecs)
75 return Time (msecs / VISUAL_MSECS_PER_SEC,
76 (msecs % VISUAL_MSECS_PER_SEC) * VISUAL_NSECS_PER_MSEC);
79 static Time from_usecs (uint64_t usecs)
81 return Time (usecs / VISUAL_USECS_PER_SEC,
82 (usecs % VISUAL_USECS_PER_SEC) * VISUAL_NSECS_PER_USEC);
87 friend Time operator- (Time
const& lhs, Time
const& rhs)
95 Time& operator-= (Time
const& rhs)
102 nsec += VISUAL_NSECS_PER_SEC;
108 friend bool operator== (Time
const& lhs, Time
const& rhs)
110 return lhs.sec == rhs.sec && lhs.nsec == rhs.nsec;
113 friend bool operator!= (Time
const& lhs, Time
const& rhs)
115 return !(lhs == rhs);
118 friend bool operator>= (Time
const& lhs, Time
const& rhs)
120 return lhs.sec >= rhs.sec;
123 friend bool operator<= (Time
const& lhs, Time
const& rhs)
128 friend bool operator> (Time
const& lhs, Time
const& rhs)
130 return (lhs.sec > rhs.sec) || (lhs.sec == rhs.sec && lhs.nsec > rhs.nsec);
133 friend bool operator< (Time
const& lhs, Time
const& rhs)
139 double to_secs ()
const
141 return sec + nsec * (1.0 / VISUAL_NSECS_PER_SEC);
145 uint64_t to_msecs ()
const
147 return uint64_t (sec) * VISUAL_MSECS_PER_SEC + nsec / VISUAL_NSECS_PER_MSEC;
151 uint64_t to_usecs ()
const
153 return uint64_t (sec) * VISUAL_USECS_PER_SEC + nsec / VISUAL_NSECS_PER_USEC;
157 static void usleep (uint64_t usecs);
183 bool is_active ()
const;
194 Time get_start_time ()
const;
196 Time get_end_time ()
const;
199 Time elapsed ()
const;
201 bool is_past (
Time const& age)
const;
206 std::unique_ptr<Impl> m_impl;
220 typedef struct _VisTime VisTime;
223 typedef struct _VisTimer VisTimer;
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_);
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_);
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);
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_);
247 LV_API
void visual_usleep (uint64_t usecs);
249 LV_API
void visual_time_set_from_msecs (VisTime *time_, uint64_t msecs);
251 LV_API VisTimer *visual_timer_new (
void);
252 LV_API
void visual_timer_free (VisTimer *timer);
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);
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);
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);