1 /* Time class implementation: inline functions.
2    Copyright (C) 2001-2010 Roberto Bagnara <bagnara@cs.unipr.it>
3    Copyright (C) 2010-2016 BUGSENG srl (http://bugseng.com)
4 
5 This file is part of the Parma Polyhedra Library (PPL).
6 
7 The PPL is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 The PPL is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software Foundation,
19 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307, USA.
20 
21 For the most up-to-date information see the Parma Polyhedra Library
22 site: http://bugseng.com/products/ppl/ . */
23 
24 #ifndef PPL_Time_inlines_hh
25 #define PPL_Time_inlines_hh 1
26 
27 #include <cassert>
28 
29 namespace Parma_Polyhedra_Library {
30 
31 namespace Implementation {
32 
33 namespace Watchdog {
34 
35 inline
Time()36 Time::Time()
37   : secs(0), microsecs(0) {
38   assert(OK());
39 }
40 
41 inline
Time(long centisecs)42 Time::Time(long centisecs)
43   : secs(centisecs / CSECS_PER_SEC),
44     microsecs((centisecs % CSECS_PER_SEC) * (USECS_PER_SEC/CSECS_PER_SEC)) {
45   assert(OK());
46 }
47 
48 inline
Time(long s,long m)49 Time::Time(long s, long m)
50   : secs(s),
51     microsecs(m) {
52   if (microsecs >= USECS_PER_SEC) {
53     secs += microsecs / USECS_PER_SEC;
54     microsecs %= USECS_PER_SEC;
55   }
56   assert(OK());
57 }
58 
59 inline long
seconds() const60 Time::seconds() const {
61   return secs;
62 }
63 
64 inline long
microseconds() const65 Time::microseconds() const {
66   return microsecs;
67 }
68 
69 inline Time&
operator +=(const Time & y)70 Time::operator+=(const Time& y) {
71   long r_secs = secs + y.secs;
72   long r_microsecs = microsecs + y.microsecs;
73   if (r_microsecs >= USECS_PER_SEC) {
74     ++r_secs;
75     r_microsecs %= USECS_PER_SEC;
76   }
77   secs = r_secs;
78   microsecs = r_microsecs;
79   assert(OK());
80   return *this;
81 }
82 
83 inline Time&
operator -=(const Time & y)84 Time::operator-=(const Time& y) {
85   long r_secs = secs - y.secs;
86   long r_microsecs = microsecs - y.microsecs;
87   if (r_microsecs < 0) {
88     --r_secs;
89     r_microsecs += USECS_PER_SEC;
90   }
91   if (r_secs < 0) {
92     r_secs = 0;
93     r_microsecs = 0;
94   }
95   secs = r_secs;
96   microsecs = r_microsecs;
97   assert(OK());
98   return *this;
99 }
100 
101 inline Time
operator +(const Time & x,const Time & y)102 operator+(const Time& x, const Time& y) {
103   Time z = x;
104   z += y;
105   return z;
106 }
107 
108 inline Time
operator -(const Time & x,const Time & y)109 operator-(const Time& x, const Time& y) {
110   Time z = x;
111   z -= y;
112   return z;
113 }
114 
115 inline bool
operator ==(const Time & x,const Time & y)116 operator==(const Time& x, const Time& y) {
117   assert(x.OK() && y.OK());
118   return x.seconds() == y.seconds() && y.microseconds() == y.microseconds();
119 }
120 
121 inline bool
operator !=(const Time & x,const Time & y)122 operator!=(const Time& x, const Time& y) {
123   assert(x.OK() && y.OK());
124   return !(x == y);
125 }
126 
127 inline bool
operator <(const Time & x,const Time & y)128 operator<(const Time& x, const Time& y) {
129   assert(x.OK() && y.OK());
130   return x.seconds() < y.seconds()
131     || (x.seconds() == y.seconds() && x.microseconds() < y.microseconds());
132 }
133 
134 inline bool
operator <=(const Time & x,const Time & y)135 operator<=(const Time& x, const Time& y) {
136   return x < y || x == y;
137 }
138 
139 inline bool
operator >(const Time & x,const Time & y)140 operator>(const Time& x, const Time& y) {
141   return y < x;
142 }
143 
144 inline bool
operator >=(const Time & x,const Time & y)145 operator>=(const Time& x, const Time& y) {
146   return y <= x;
147 }
148 
149 } // namespace Watchdog
150 
151 } // namespace Implementation
152 
153 } // namespace Parma_Polyhedra_Library
154 
155 #endif // !defined(PPL_Time_inlines_hh)
156