libvisual  0.5.0
lv_time.cpp
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 #include "config.h"
25 #include "lv_time.h"
26 #include "private/lv_time_system.hpp"
27 #include "lv_common.h"
28 
29 namespace LV {
30 
31  class Timer::Impl
32  {
33  public:
34 
35  Time start;
36  Time end;
37  bool active;
38  };
39 
40  Time Time::now ()
41  {
42  return TimeSystem::now ();
43  }
44 
45  void Time::usleep (uint64_t usecs)
46  {
47  TimeSystem::usleep (usecs);
48  }
49 
51  : m_impl (new Impl)
52  {
53  reset ();
54  }
55 
57  {
58  // empty
59  }
60 
61  Timer::Timer (Timer const& rhs)
62  : m_impl {new Impl (*rhs.m_impl)}
63  {
64  // nothing
65  }
66 
68  : m_impl {std::move (rhs.m_impl)}
69  {
70  // nothing
71  }
72 
74  {
75  *m_impl = *rhs.m_impl;
76  return *this;
77  }
78 
80  {
81  m_impl.swap (rhs.m_impl);
82  return *this;
83  }
84 
85  void Timer::reset ()
86  {
87  m_impl->start = m_impl->end = Time ();
88  m_impl->active = false;
89  }
90 
91  bool Timer::is_active () const
92  {
93  return m_impl->active;
94  }
95 
96  void Timer::start ()
97  {
98  m_impl->start = Time::now ();
99  m_impl->active = true;
100  }
101 
102  void Timer::stop ()
103  {
104  m_impl->end = Time::now ();
105  m_impl->active = false;
106  }
107 
108  Time Timer::get_start_time () const
109  {
110  return m_impl->start;
111  }
112 
113  Time Timer::get_end_time () const
114  {
115  return m_impl->end;
116  }
117 
119  {
120  if (is_active ())
121  return Time::now () - m_impl->start;
122  else
123  return m_impl->end - m_impl->start;
124  }
125 
126  bool Timer::is_past (Time const& age) const
127  {
128  return elapsed () > age;
129  }
130 
131 } // LV namespace