Bare Metal Programming Tool Kit
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
time.h
1 //***************************************************************************
2 //
3 // file : bmptk/core/time.h
4 //
5 // LICENSE (MIT expat license, copy of bmptk/license.txt)
6 //
7 // Copyright (c) 2013 Wouter van Ooijen (wouter@voti.nl)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be included
18 // in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23 // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
24 // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE..
27 //
28 //***************************************************************************
29 
30 
31 namespace bmptk {
32 
34 //
48 class time {
49 
50  // the time expressed as 64-bit signed integer
51  long long int t;
52 
53  // convert an integer to a time
54  constexpr time( long long int n ): t( n ){}
55 
56  // current_time() must be able to convert an integer to a time
58 public:
59 
61  time(){}
62 
63  #ifndef DOXYDOC
64  // to be used only by const us, hence not documented
65  // return the time value for one microsecond
66  static constexpr time us(){ return time( BMPTK_TICKS_PER_US ); }
67  #endif
68 
70  constexpr time operator+( const time &rhs ) const {
71  return time( t + rhs.t );
72  }
73 
75  void operator+=( const time &rhs ){
76  t += rhs.t;
77  }
78 
80  constexpr time operator-( const time &rhs ) const {
81  return time( t - rhs.t );
82  }
83 
85  void operator-=( const time &rhs ){
86  t -= rhs.t;
87  }
88 
90  constexpr time operator*( const int n ) const {
91  return time( t * n );
92  }
93 
95  void operator*=( const int n ){
96  t *= n;
97  }
98 
100  constexpr time operator/( const int n ) const {
101  return time( t / n );
102  }
103 
105  void operator/=( const int n ){
106  t /= n;
107  }
108 
110  constexpr unsigned long long int operator/( const time rhs ) const {
111  return t / rhs.t;
112  }
113 
115  constexpr bool operator<( const time rhs ) const {
116  return t < rhs.t;
117  }
118 
120  constexpr bool operator<=( const time rhs ) const {
121  return t <= rhs.t;
122  }
123 
125  constexpr bool operator>( const time rhs ) const {
126  return t > rhs.t;
127  }
128 
130  constexpr bool operator>=( const time rhs ) const {
131  return t >= rhs.t;
132  }
133 
135  constexpr bool operator==( const time rhs ) const {
136  return t == rhs.t;
137  }
138 
140  constexpr bool operator!=( const time rhs ) const {
141  return t != rhs.t;
142  }
143 };
144 
146 inline constexpr time operator*( long long int n, const time rhs ){
147  return rhs * n;
148 }
149 
150 
152 //
157 constexpr time us( time::us() );
158 
160 //
165 constexpr time ms( us * 1000 );
166 
168 //
173 constexpr time s( us * 1000 * 1000 );
174 
175 
193 time current_time();
194 
196 //
203 void wait_busy_until( time t );
204 
206 //
214 void wait_until( time t );
215 
217 //
230 void wait_busy( time t );
231 
233 //
245 void wait( time t );
246 
247 
248 }; // namespace bmptk;