cmake-based buildsystem
[sol.git] / sol / date_time / duration.hpp
blobba8dfc53e42f71fb2ac5fa3b2e1a0c6e869a8578
1 #if !defined(sol_date_time_duration_hpp_included)
2 #define sol_date_time_duration_hpp_included
3 /// \addtogroup sol_date_time
4 /// \{
7 /**
8 * \file sol/date_time/duration.hpp
9 * \brief Length of time unattached to a any time point.
10 * \author Sven Suursoho
14 namespace sol { namespace date_time {
17 /// Common duration type, ancestor class for more specific duration
18 template <
19 typename FinalType
20 , typename TickType
21 , int TicksPerSecond
22 , int SecondsPerTick
24 class duration_type
26 public:
28 /// Count type at the resolution of duration type
29 typedef TickType tick_type;
32 /// Number of ticks per second
33 static tick_type const ticks_per_second = TicksPerSecond;
36 /// Number of seconds per tick
37 static tick_type const seconds_per_tick = SecondsPerTick;
40 /// True if resolution is less than 1 second
41 static bool const is_subsecond = TicksPerSecond > SecondsPerTick;
44 /// \internal Helper type definition
45 typedef duration_type<
46 FinalType, TickType, TicksPerSecond, SecondsPerTick
47 > this_type;
50 /// Construct duration object (defaulting to count of 0)
51 duration_type (tick_type d = 0)
52 : count_(d)
56 /// Return the count at the resolution of the time duration type
57 tick_type
58 get_count () const
60 return count_;
64 /// Return this + that
65 FinalType
66 operator+ (FinalType const &that) const
68 return FinalType(count_ + that.count_);
72 /// Return this += that
73 FinalType &
74 operator+= (FinalType const &that)
76 count_ += that.count_;
77 return *static_cast<FinalType *>(this);
81 /// Return this - that
82 FinalType
83 operator- (FinalType const &that) const
85 return FinalType(count_ - that.count_);
89 /// Return this -= that
90 FinalType &
91 operator-= (FinalType const &that)
93 count_ -= that.count_;
94 return *static_cast<FinalType *>(this);
98 /// Return this/divisor
99 FinalType
100 operator/ (int divisor) const
102 return FinalType(count_/divisor);
106 /// Return this/=divisor
107 FinalType &
108 operator/= (int divisor)
110 count_ /= divisor;
111 return *static_cast<FinalType *>(this);
115 /// Return this*divisor
116 FinalType
117 operator* (int multiplier) const
119 return FinalType(count_*multiplier);
123 /// Return this*=divisor
124 FinalType &
125 operator*= (int multiplier)
127 count_ *= multiplier;
128 return *static_cast<FinalType *>(this);
132 /// Return negate of this
133 FinalType
134 operator- () const
136 return FinalType(-count_);
140 /// Return true if this == that
141 bool
142 operator== (this_type const &that) const
144 return count_ == that.count_;
148 /// Return true if this != that
149 bool
150 operator!= (this_type const &that) const
152 return count_ != that.count_;
156 /// Return true if this > that
157 bool
158 operator> (this_type const &that) const
160 return count_ > that.count_;
164 /// Return true if this >= that
165 bool
166 operator>= (this_type const &that) const
168 return count_ >= that.count_;
172 /// Return true if this < that
173 bool
174 operator< (this_type const &that) const
176 return count_ < that.count_;
180 /// Return true if this <= that
181 bool
182 operator<= (this_type const &that) const
184 return count_ <= that.count_;
188 protected:
190 tick_type count_;
194 /// Duration in nanoseconds
195 class nanoseconds
196 : public duration_type<nanoseconds, long long, 1000000000, 0>
198 public:
200 nanoseconds (tick_type d = 0)
201 : this_type(d)
206 /// Duration in microseconds
207 class microseconds
208 : public duration_type<microseconds, long long, 1000000, 0>
210 public:
212 microseconds (tick_type d = 0)
213 : this_type(d)
217 operator nanoseconds () const
219 return count_*(nanoseconds::ticks_per_second/ticks_per_second);
224 /// Duration in milliseconds
225 class milliseconds
226 : public duration_type<milliseconds, long long, 1000, 0>
228 public:
230 milliseconds (tick_type d = 0)
231 : this_type(d)
235 operator nanoseconds () const
237 return count_*(nanoseconds::ticks_per_second/ticks_per_second);
241 operator microseconds () const
243 return count_*(microseconds::ticks_per_second/ticks_per_second);
248 /// Duration in seconds
249 class seconds
250 : public duration_type<seconds, long long, 1, 1>
252 public:
254 seconds (tick_type d = 0)
255 : this_type(d)
259 operator nanoseconds () const
261 return count_*(nanoseconds::ticks_per_second/ticks_per_second);
265 operator microseconds () const
267 return count_*(microseconds::ticks_per_second/ticks_per_second);
271 operator milliseconds () const
273 return count_*(milliseconds::ticks_per_second/ticks_per_second);
278 /// Duration in minutes
279 class minutes
280 : public duration_type<minutes, long long, 0, 60>
282 public:
284 minutes (tick_type d = 0): this_type(d)
288 operator nanoseconds () const
290 return count_*seconds_per_tick*nanoseconds::ticks_per_second;
294 operator microseconds () const
296 return count_*seconds_per_tick*microseconds::ticks_per_second;
300 operator milliseconds () const
302 return count_*seconds_per_tick*milliseconds::ticks_per_second;
306 operator seconds () const
308 return count_*seconds_per_tick;
313 /// Duration in hours
314 class hours
315 : public duration_type<hours, long long, 0, 3600>
317 public:
319 hours (tick_type d = 0)
320 : this_type(d)
324 operator nanoseconds () const
326 return count_*seconds_per_tick*nanoseconds::ticks_per_second;
330 operator microseconds () const
332 return count_*seconds_per_tick*microseconds::ticks_per_second;
336 operator milliseconds () const
338 return count_*seconds_per_tick*milliseconds::ticks_per_second;
342 operator seconds () const
344 return count_*seconds_per_tick;
348 operator minutes () const
350 return count_*(seconds_per_tick/minutes::seconds_per_tick);
355 }} // namespace sol::date_time
358 /// \}
359 #endif // sol_date_time_duration_hpp_included