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

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

Issue 2624573004: Add advice about Optional function arguments (Closed)
Patch Set: Created 3 years, 11 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 | no next file » | 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 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 // Optional<size_t> so that it can return nothing in case the caller passed 70 // Optional<size_t> so that it can return nothing in case the caller passed
71 // it a null pointer; the function should probably use RTC_[D]CHECK instead, 71 // it a null pointer; the function should probably use RTC_[D]CHECK instead,
72 // and return plain size_t. 72 // and return plain size_t.
73 // 73 //
74 // - As a return value for functions that may fail to return a value on all 74 // - As a return value for functions that may fail to return a value on all
75 // allowed inputs, but need to tell the caller what went wrong. Returning 75 // allowed inputs, but need to tell the caller what went wrong. Returning
76 // Optional<double> when parsing a single number as in the example above 76 // Optional<double> when parsing a single number as in the example above
77 // might make sense, but any larger parse job is probably going to need to 77 // might make sense, but any larger parse job is probably going to need to
78 // tell the caller what the problem was, not just that there was one. 78 // tell the caller what the problem was, not just that there was one.
79 // 79 //
80 // - As a non-mutable function argument. When you want to pass a value of a
81 // type T that can fail to be there, const T* is almost always both fastest
82 // and cleanest. (If you're *sure* that the the caller will always already
83 // have an Optional<T>, const Optional<T>& is slightly faster than const T*,
84 // but this is a micro-optimization. In general, stick to const T*.)
85 //
80 // TODO(kwiberg): Get rid of this class when the standard library has 86 // TODO(kwiberg): Get rid of this class when the standard library has
81 // std::optional (and we're allowed to use it). 87 // std::optional (and we're allowed to use it).
82 template <typename T> 88 template <typename T>
83 class Optional final { 89 class Optional final {
84 public: 90 public:
85 // Construct an empty Optional. 91 // Construct an empty Optional.
86 Optional() : has_value_(false), empty_('\0') { 92 Optional() : has_value_(false), empty_('\0') {
87 PoisonValue(); 93 PoisonValue();
88 } 94 }
89 95
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after
280 // construct it, and the Optional destructor won't automatically destroy 286 // construct it, and the Optional destructor won't automatically destroy
281 // it. Basically, this just allocates a properly sized and aligned block of 287 // it. Basically, this just allocates a properly sized and aligned block of
282 // memory in which we can manually put a T with placement new. 288 // memory in which we can manually put a T with placement new.
283 T value_; 289 T value_;
284 }; 290 };
285 }; 291 };
286 292
287 } // namespace rtc 293 } // namespace rtc
288 294
289 #endif // WEBRTC_BASE_OPTIONAL_H_ 295 #endif // WEBRTC_BASE_OPTIONAL_H_
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698