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

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, note that we only allow multiplicative
162 // with ints, and additive operations with other deltas. 162 // operations with other types.
rvargas (doing something else) 2015/02/21 01:17:45 nit: Thanks. The comment was certainly confusing.
michaeln 2015/02/23 20:02:17 Done
163 TimeDelta operator*(int64 a) const { 163 template<typename T>
164 TimeDelta operator*(T a) const {
164 return TimeDelta(delta_ * a); 165 return TimeDelta(delta_ * a);
165 } 166 }
166 TimeDelta operator/(int64 a) const { 167 template<typename T>
168 TimeDelta operator/(T a) const {
167 return TimeDelta(delta_ / a); 169 return TimeDelta(delta_ / a);
168 } 170 }
169 TimeDelta& operator*=(int64 a) { 171 template<typename T>
172 TimeDelta& operator*=(T a) {
170 delta_ *= a; 173 delta_ *= a;
171 return *this; 174 return *this;
172 } 175 }
173 TimeDelta& operator/=(int64 a) { 176 template<typename T>
177 TimeDelta& operator/=(T a) {
174 delta_ /= a; 178 delta_ /= a;
175 return *this; 179 return *this;
176 } 180 }
181
177 int64 operator/(TimeDelta a) const { 182 int64 operator/(TimeDelta a) const {
178 return delta_ / a.delta_; 183 return delta_ / a.delta_;
179 } 184 }
180 185
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. 186 // Defined below because it depends on the definition of the other classes.
190 Time operator+(Time t) const; 187 Time operator+(Time t) const;
191 TimeTicks operator+(TimeTicks t) const; 188 TimeTicks operator+(TimeTicks t) const;
192 189
193 // Comparison operators. 190 // Comparison operators.
194 bool operator==(TimeDelta other) const { 191 bool operator==(TimeDelta other) const {
195 return delta_ == other.delta_; 192 return delta_ == other.delta_;
196 } 193 }
197 bool operator!=(TimeDelta other) const { 194 bool operator!=(TimeDelta other) const {
198 return delta_ != other.delta_; 195 return delta_ != other.delta_;
199 } 196 }
200 bool operator<(TimeDelta other) const { 197 bool operator<(TimeDelta other) const {
201 return delta_ < other.delta_; 198 return delta_ < other.delta_;
202 } 199 }
203 bool operator<=(TimeDelta other) const { 200 bool operator<=(TimeDelta other) const {
204 return delta_ <= other.delta_; 201 return delta_ <= other.delta_;
205 } 202 }
206 bool operator>(TimeDelta other) const { 203 bool operator>(TimeDelta other) const {
207 return delta_ > other.delta_; 204 return delta_ > other.delta_;
208 } 205 }
209 bool operator>=(TimeDelta other) const { 206 bool operator>=(TimeDelta other) const {
210 return delta_ >= other.delta_; 207 return delta_ >= other.delta_;
211 } 208 }
212 209
213 private: 210 private:
214 friend class Time; 211 friend class Time;
215 friend class TimeTicks; 212 friend class TimeTicks;
216 friend TimeDelta operator*(int64 a, TimeDelta td); 213 template<typename T>
rvargas (doing something else) 2015/02/21 01:17:44 We can remove this...
214 friend TimeDelta operator*(T a, TimeDelta td);
217 215
218 // Constructs a delta given the duration in microseconds. This is private 216 // Constructs a delta given the duration in microseconds. This is private
219 // to avoid confusion by callers with an integer constructor. Use 217 // to avoid confusion by callers with an integer constructor. Use
220 // FromSeconds, FromMilliseconds, etc. instead. 218 // FromSeconds, FromMilliseconds, etc. instead.
221 explicit TimeDelta(int64 delta_us) : delta_(delta_us) { 219 explicit TimeDelta(int64 delta_us) : delta_(delta_us) {
222 } 220 }
223 221
224 // Delta in microseconds. 222 // Delta in microseconds.
225 int64 delta_; 223 int64 delta_;
226 }; 224 };
227 225
228 inline TimeDelta operator*(int64 a, TimeDelta td) { 226 template<typename T>
227 inline TimeDelta operator*(T a, TimeDelta td) {
229 return TimeDelta(a * td.delta_); 228 return TimeDelta(a * td.delta_);
rvargas (doing something else) 2015/02/21 01:17:45 with a return td * a;
michaeln 2015/02/23 20:02:18 Done (thnx)
230 } 229 }
231 230
232 // For logging use only. 231 // For logging use only.
233 BASE_EXPORT std::ostream& operator<<(std::ostream& os, TimeDelta time_delta); 232 BASE_EXPORT std::ostream& operator<<(std::ostream& os, TimeDelta time_delta);
234 233
235 // Time ----------------------------------------------------------------------- 234 // Time -----------------------------------------------------------------------
236 235
237 // Represents a wall clock time in UTC. 236 // Represents a wall clock time in UTC.
238 class BASE_EXPORT Time { 237 class BASE_EXPORT Time {
239 public: 238 public:
(...skipping 522 matching lines...) Expand 10 before | Expand all | Expand 10 after
762 inline TimeTicks TimeDelta::operator+(TimeTicks t) const { 761 inline TimeTicks TimeDelta::operator+(TimeTicks t) const {
763 return TimeTicks(t.ticks_ + delta_); 762 return TimeTicks(t.ticks_ + delta_);
764 } 763 }
765 764
766 // For logging use only. 765 // For logging use only.
767 BASE_EXPORT std::ostream& operator<<(std::ostream& os, TimeTicks time_ticks); 766 BASE_EXPORT std::ostream& operator<<(std::ostream& os, TimeTicks time_ticks);
768 767
769 } // namespace base 768 } // namespace base
770 769
771 #endif // BASE_TIME_TIME_H_ 770 #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