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

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

Issue 2432393002: Add rtc::Optional equality/unequality comparisions with object (Closed)
Patch Set: Feedback Created 4 years, 2 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 | 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...) Expand 10 before | Expand all | Expand 10 after
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 }
242 friend bool operator==(const Optional& opt, const T& value) {
243 return opt.has_value_ && opt.value_ == value;
244 }
245 friend bool operator==(const T& value, const Optional& opt) {
246 return opt.has_value_ && value == opt.value_;
247 }
248
243 friend bool operator!=(const Optional& m1, const Optional& m2) { 249 friend bool operator!=(const Optional& m1, const Optional& m2) {
244 return m1.has_value_ && m2.has_value_ ? m1.value_ != m2.value_ 250 return m1.has_value_ && m2.has_value_ ? m1.value_ != m2.value_
245 : m1.has_value_ != m2.has_value_; 251 : m1.has_value_ != m2.has_value_;
246 } 252 }
253 friend bool operator!=(const Optional& opt, const T& value) {
254 return !opt.has_value_ || opt.value_ != value;
255 }
256 friend bool operator!=(const T& value, const Optional& opt) {
257 return !opt.has_value_ || value != opt.value_;
258 }
247 259
248 private: 260 private:
249 // Tell sanitizers that value_ shouldn't be touched. 261 // Tell sanitizers that value_ shouldn't be touched.
250 void PoisonValue() { 262 void PoisonValue() {
251 rtc::AsanPoison(rtc::MakeArrayView(&value_, 1)); 263 rtc::AsanPoison(rtc::MakeArrayView(&value_, 1));
252 rtc::MsanMarkUninitialized(rtc::MakeArrayView(&value_, 1)); 264 rtc::MsanMarkUninitialized(rtc::MakeArrayView(&value_, 1));
253 } 265 }
254 266
255 // Tell sanitizers that value_ is OK to touch again. 267 // Tell sanitizers that value_ is OK to touch again.
256 void UnpoisonValue() { 268 void UnpoisonValue() {
(...skipping 11 matching lines...) Expand all
268 // construct it, and the Optional destructor won't automatically destroy 280 // construct it, and the Optional destructor won't automatically destroy
269 // it. Basically, this just allocates a properly sized and aligned block of 281 // 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. 282 // memory in which we can manually put a T with placement new.
271 T value_; 283 T value_;
272 }; 284 };
273 }; 285 };
274 286
275 } // namespace rtc 287 } // namespace rtc
276 288
277 #endif // WEBRTC_BASE_OPTIONAL_H_ 289 #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
This is Rietveld 408576698