Chromium Code Reviews

Side by Side Diff: webrtc/base/optional.h

Issue 2432393002: Add rtc::Optional equality/unequality comparisions with object (Closed)
Patch Set: Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff |
« no previous file with comments | « no previous file | webrtc/base/optional_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 /* 1 /*
2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2015 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 216 matching lines...)
227 // Dereference with a default value in case we don't have a value. 227 // Dereference with a default value in case we don't have a value.
228 const T& value_or(const T& default_val) const { 228 const T& value_or(const T& default_val) const {
229 // The no-op call prevents the compiler from generating optimized code that 229 // The no-op call prevents the compiler from generating optimized code that
230 // reads value_ even if !has_value_, but only if FunctionThatDoesNothing is 230 // reads value_ even if !has_value_, but only if FunctionThatDoesNothing is
231 // not completely inlined; see its declaration.). 231 // not completely inlined; see its declaration.).
232 return has_value_ ? *optional_internal::FunctionThatDoesNothing(&value_) 232 return has_value_ ? *optional_internal::FunctionThatDoesNothing(&value_)
233 : default_val; 233 : default_val;
234 } 234 }
235 235
236 // Equality tests. Two Optionals are equal if they contain equivalent values, 236 // Equality tests. Two Optionals are equal if they contain equivalent values,
237 // or 237 // or if they're both empty.
238 // if they're both empty.
239 friend bool operator==(const Optional& m1, const Optional& m2) { 238 friend bool operator==(const Optional& m1, const Optional& m2) {
240 return m1.has_value_ && m2.has_value_ ? m1.value_ == m2.value_ 239 return m1.has_value_ && m2.has_value_ ? m1.value_ == m2.value_
241 : m1.has_value_ == m2.has_value_; 240 : m1.has_value_ == m2.has_value_;
242 } 241 }
243 friend bool operator!=(const Optional& m1, const Optional& m2) { 242 friend bool operator!=(const Optional& m1, const Optional& m2) {
244 return m1.has_value_ && m2.has_value_ ? m1.value_ != m2.value_ 243 return m1.has_value_ && m2.has_value_ ? m1.value_ != m2.value_
245 : m1.has_value_ != m2.has_value_; 244 : m1.has_value_ != m2.has_value_;
246 } 245 }
246 friend bool operator==(const Optional& opt, const T& value) {
247 return opt.has_value_ && opt.value_ == value;
248 }
249 friend bool operator==(const T& value, const Optional& opt) {
250 return opt.has_value_ && value == opt.value_;
251 }
252 friend bool operator!=(const Optional& opt, const T& value) {
253 return !opt.has_value_ || opt.value_ != value;
254 }
255 friend bool operator!=(const T& value, const Optional& opt) {
256 return !opt.has_value_ || value != opt.value_;
257 }
kwiberg-webrtc 2016/10/19 11:16:42 Maybe do all the operator== before the operator!=
danilchap 2016/10/19 11:23:52 Good point. Done.
247 258
248 private: 259 private:
249 // Tell sanitizers that value_ shouldn't be touched. 260 // Tell sanitizers that value_ shouldn't be touched.
250 void PoisonValue() { 261 void PoisonValue() {
251 rtc::AsanPoison(rtc::MakeArrayView(&value_, 1)); 262 rtc::AsanPoison(rtc::MakeArrayView(&value_, 1));
252 rtc::MsanMarkUninitialized(rtc::MakeArrayView(&value_, 1)); 263 rtc::MsanMarkUninitialized(rtc::MakeArrayView(&value_, 1));
253 } 264 }
254 265
255 // Tell sanitizers that value_ is OK to touch again. 266 // Tell sanitizers that value_ is OK to touch again.
256 void UnpoisonValue() { 267 void UnpoisonValue() {
(...skipping 11 matching lines...)
268 // construct it, and the Optional destructor won't automatically destroy 279 // construct it, and the Optional destructor won't automatically destroy
269 // it. Basically, this just allocates a properly sized and aligned block of 280 // it. Basically, this just allocates a properly sized and aligned block of
270 // memory in which we can manually put a T with placement new. 281 // memory in which we can manually put a T with placement new.
271 T value_; 282 T value_;
272 }; 283 };
273 }; 284 };
274 285
275 } // namespace rtc 286 } // namespace rtc
276 287
277 #endif // WEBRTC_BASE_OPTIONAL_H_ 288 #endif // WEBRTC_BASE_OPTIONAL_H_
OLDNEW
« no previous file with comments | « no previous file | webrtc/base/optional_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine