PPL  1.2
Time_inlines.hh
Go to the documentation of this file.
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
37  : secs(0), microsecs(0) {
38  assert(OK());
39 }
40 
41 inline
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
49 Time::Time(long s, long m)
50  : secs(s),
51  microsecs(m) {
52  if (microsecs >= USECS_PER_SEC) {
55  }
56  assert(OK());
57 }
58 
59 inline long
60 Time::seconds() const {
61  return secs;
62 }
63 
64 inline long
66  return microsecs;
67 }
68 
69 inline Time&
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&
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
102 operator+(const Time& x, const Time& y) {
103  Time z = x;
104  z += y;
105  return z;
106 }
107 
108 inline Time
109 operator-(const Time& x, const Time& y) {
110  Time z = x;
111  z -= y;
112  return z;
113 }
114 
115 inline bool
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
122 operator!=(const Time& x, const Time& y) {
123  assert(x.OK() && y.OK());
124  return !(x == y);
125 }
126 
127 inline bool
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
135 operator<=(const Time& x, const Time& y) {
136  return x < y || x == y;
137 }
138 
139 inline bool
140 operator>(const Time& x, const Time& y) {
141  return y < x;
142 }
143 
144 inline bool
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)
long microseconds() const
Returns the number of microseconds that, when added to the number of seconds returned by seconds()...
Definition: Time_inlines.hh:65
static const long USECS_PER_SEC
Number of microseconds in a second.
Definition: Time_defs.hh:112
Time & operator+=(const Time &y)
Adds y to *this.
Definition: Time_inlines.hh:70
Time operator+(const Time &x, const Time &y)
Returns the sum of x and y.
bool operator<(const Time &x, const Time &y)
Returns true if and only if x is shorter than y.
Time operator-(const Time &x, const Time &y)
Returns the difference of x and y or the null interval, if x is shorter than y.
A class for representing and manipulating positive time intervals.
Definition: Time_defs.hh:75
bool operator==(const Time &x, const Time &y)
Returns true if and only if x and y are equal.
Time & operator-=(const Time &y)
Subtracts y from *this; if *this is shorter than y, *this is set to the null interval.
Definition: Time_inlines.hh:84
The entire library is confined to this namespace.
Definition: version.hh:61
bool operator>(const Time &x, const Time &y)
Returns true if and only if x is longer than y.
bool operator!=(const Time &x, const Time &y)
Returns true if and only if x and y are different.
bool operator>=(const Time &x, const Time &y)
Returns true if and only if x is longer than or equal to y.
long seconds() const
Returns the number of whole seconds contained in the represented time interval.
Definition: Time_inlines.hh:60
bool OK() const
Checks if all the invariants are satisfied.
Definition: Time.cc:30
bool operator<=(const Time &x, const Time &y)
Returns true if and only if x is shorter than or equal to y.