Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(96)

Side by Side Diff: base/time/time.h

Issue 945143002: base::Time multiplicative operator overloading (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | base/time/time_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Time represents an absolute point in coordinated universal time (UTC), 5 // Time represents an absolute point in coordinated universal time (UTC),
6 // internally represented as microseconds (s/1,000,000) since the Windows epoch 6 // internally represented as microseconds (s/1,000,000) since the Windows epoch
7 // (1601-01-01 00:00:00 UTC) (See http://crbug.com/14734). System-dependent 7 // (1601-01-01 00:00:00 UTC) (See http://crbug.com/14734). System-dependent
8 // clock interface routines are defined in time_PLATFORM.cc. 8 // clock interface routines are defined in time_PLATFORM.cc.
9 // 9 //
10 // TimeDelta represents a duration of time, internally represented in 10 // TimeDelta represents a duration of time, internally represented in
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 return *this; 151 return *this;
152 } 152 }
153 TimeDelta& operator-=(TimeDelta other) { 153 TimeDelta& operator-=(TimeDelta other) {
154 delta_ -= other.delta_; 154 delta_ -= other.delta_;
155 return *this; 155 return *this;
156 } 156 }
157 TimeDelta operator-() const { 157 TimeDelta operator-() const {
158 return TimeDelta(-delta_); 158 return TimeDelta(-delta_);
159 } 159 }
160 160
161 // Computations with ints, note that we only allow multiplicative operations 161 // Computations with numeric types.
162 // with ints, and additive operations with other deltas. 162 template<typename T>
163 TimeDelta operator*(int64 a) const { 163 TimeDelta operator*(T a) const {
164 return TimeDelta(delta_ * a); 164 return TimeDelta(delta_ * a);
165 } 165 }
166 TimeDelta operator/(int64 a) const { 166 template<typename T>
167 TimeDelta operator/(T a) const {
167 return TimeDelta(delta_ / a); 168 return TimeDelta(delta_ / a);
168 } 169 }
169 TimeDelta& operator*=(int64 a) { 170 template<typename T>
171 TimeDelta& operator*=(T a) {
170 delta_ *= a; 172 delta_ *= a;
171 return *this; 173 return *this;
172 } 174 }
173 TimeDelta& operator/=(int64 a) { 175 template<typename T>
176 TimeDelta& operator/=(T a) {
174 delta_ /= a; 177 delta_ /= a;
175 return *this; 178 return *this;
176 } 179 }
180
177 int64 operator/(TimeDelta a) const { 181 int64 operator/(TimeDelta a) const {
178 return delta_ / a.delta_; 182 return delta_ / a.delta_;
179 } 183 }
180 184
181 // Multiplicative computations with floats.
182 TimeDelta multiply_by(double a) const {
183 return TimeDelta(delta_ * a);
184 }
185 TimeDelta divide_by(double a) const {
186 return TimeDelta(delta_ / a);
187 }
188
189 // Defined below because it depends on the definition of the other classes. 185 // Defined below because it depends on the definition of the other classes.
190 Time operator+(Time t) const; 186 Time operator+(Time t) const;
191 TimeTicks operator+(TimeTicks t) const; 187 TimeTicks operator+(TimeTicks t) const;
192 188
193 // Comparison operators. 189 // Comparison operators.
194 bool operator==(TimeDelta other) const { 190 bool operator==(TimeDelta other) const {
195 return delta_ == other.delta_; 191 return delta_ == other.delta_;
196 } 192 }
197 bool operator!=(TimeDelta other) const { 193 bool operator!=(TimeDelta other) const {
198 return delta_ != other.delta_; 194 return delta_ != other.delta_;
199 } 195 }
200 bool operator<(TimeDelta other) const { 196 bool operator<(TimeDelta other) const {
201 return delta_ < other.delta_; 197 return delta_ < other.delta_;
202 } 198 }
203 bool operator<=(TimeDelta other) const { 199 bool operator<=(TimeDelta other) const {
204 return delta_ <= other.delta_; 200 return delta_ <= other.delta_;
205 } 201 }
206 bool operator>(TimeDelta other) const { 202 bool operator>(TimeDelta other) const {
207 return delta_ > other.delta_; 203 return delta_ > other.delta_;
208 } 204 }
209 bool operator>=(TimeDelta other) const { 205 bool operator>=(TimeDelta other) const {
210 return delta_ >= other.delta_; 206 return delta_ >= other.delta_;
211 } 207 }
212 208
213 private: 209 private:
214 friend class Time; 210 friend class Time;
215 friend class TimeTicks; 211 friend class TimeTicks;
216 friend TimeDelta operator*(int64 a, TimeDelta td);
217 212
218 // Constructs a delta given the duration in microseconds. This is private 213 // Constructs a delta given the duration in microseconds. This is private
219 // to avoid confusion by callers with an integer constructor. Use 214 // to avoid confusion by callers with an integer constructor. Use
220 // FromSeconds, FromMilliseconds, etc. instead. 215 // FromSeconds, FromMilliseconds, etc. instead.
221 explicit TimeDelta(int64 delta_us) : delta_(delta_us) { 216 explicit TimeDelta(int64 delta_us) : delta_(delta_us) {
222 } 217 }
223 218
224 // Delta in microseconds. 219 // Delta in microseconds.
225 int64 delta_; 220 int64 delta_;
226 }; 221 };
227 222
228 inline TimeDelta operator*(int64 a, TimeDelta td) { 223 template<typename T>
229 return TimeDelta(a * td.delta_); 224 inline TimeDelta operator*(T a, TimeDelta td) {
225 return td * a;
230 } 226 }
231 227
232 // For logging use only. 228 // For logging use only.
233 BASE_EXPORT std::ostream& operator<<(std::ostream& os, TimeDelta time_delta); 229 BASE_EXPORT std::ostream& operator<<(std::ostream& os, TimeDelta time_delta);
234 230
235 // Time ----------------------------------------------------------------------- 231 // Time -----------------------------------------------------------------------
236 232
237 // Represents a wall clock time in UTC. 233 // Represents a wall clock time in UTC.
238 class BASE_EXPORT Time { 234 class BASE_EXPORT Time {
239 public: 235 public:
(...skipping 522 matching lines...) Expand 10 before | Expand all | Expand 10 after
762 inline TimeTicks TimeDelta::operator+(TimeTicks t) const { 758 inline TimeTicks TimeDelta::operator+(TimeTicks t) const {
763 return TimeTicks(t.ticks_ + delta_); 759 return TimeTicks(t.ticks_ + delta_);
764 } 760 }
765 761
766 // For logging use only. 762 // For logging use only.
767 BASE_EXPORT std::ostream& operator<<(std::ostream& os, TimeTicks time_ticks); 763 BASE_EXPORT std::ostream& operator<<(std::ostream& os, TimeTicks time_ticks);
768 764
769 } // namespace base 765 } // namespace base
770 766
771 #endif // BASE_TIME_TIME_H_ 767 #endif // BASE_TIME_TIME_H_
OLDNEW
« no previous file with comments | « no previous file | base/time/time_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698