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

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

Issue 2090223003: Added an empty member to the union of rtc::Optional, so that it is always initializable. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Removed else clauses; added explanatory comment. Created 4 years, 5 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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
52 // Optional<double> when parsing a single number as in the example above 52 // Optional<double> when parsing a single number as in the example above
53 // might make sense, but any larger parse job is probably going to need to 53 // might make sense, but any larger parse job is probably going to need to
54 // tell the caller what the problem was, not just that there was one. 54 // tell the caller what the problem was, not just that there was one.
55 // 55 //
56 // TODO(kwiberg): Get rid of this class when the standard library has 56 // TODO(kwiberg): Get rid of this class when the standard library has
57 // std::optional (and we're allowed to use it). 57 // std::optional (and we're allowed to use it).
58 template <typename T> 58 template <typename T>
59 class Optional final { 59 class Optional final {
60 public: 60 public:
61 // Construct an empty Optional. 61 // Construct an empty Optional.
62 Optional() : has_value_(false) {} 62 Optional() : has_value_(false), empty_('\0') {}
63 63
64 // Construct an Optional that contains a value. 64 // Construct an Optional that contains a value.
65 explicit Optional(const T& value) : has_value_(true) { 65 explicit Optional(const T& value) : has_value_(true) {
66 new (&value_) T(value); 66 new (&value_) T(value);
67 } 67 }
68 explicit Optional(T&& value) : has_value_(true) { 68 explicit Optional(T&& value) : has_value_(true) {
69 new (&value_) T(std::move(value)); 69 new (&value_) T(std::move(value));
70 } 70 }
71 71
72 // Copy constructor: copies the value from m if it has one. 72 // Copy constructor: copies the value from m if it has one.
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
182 : m1.has_value_ == m2.has_value_; 182 : m1.has_value_ == m2.has_value_;
183 } 183 }
184 friend bool operator!=(const Optional& m1, const Optional& m2) { 184 friend bool operator!=(const Optional& m1, const Optional& m2) {
185 return m1.has_value_ && m2.has_value_ ? m1.value_ != m2.value_ 185 return m1.has_value_ && m2.has_value_ ? m1.value_ != m2.value_
186 : m1.has_value_ != m2.has_value_; 186 : m1.has_value_ != m2.has_value_;
187 } 187 }
188 188
189 private: 189 private:
190 bool has_value_; // True iff value_ contains a live value. 190 bool has_value_; // True iff value_ contains a live value.
191 union { 191 union {
192 // empty_ exists only to make it possible to initialize the union, even when
193 // it doesn't contain any data. If the union goes uninitialized, it may
194 // trigger compiler warnings.
195 char empty_;
192 // By placing value_ in a union, we get to manage its construction and 196 // By placing value_ in a union, we get to manage its construction and
193 // destruction manually: the Optional constructors won't automatically 197 // destruction manually: the Optional constructors won't automatically
194 // construct it, and the Optional destructor won't automatically destroy 198 // construct it, and the Optional destructor won't automatically destroy
195 // it. Basically, this just allocates a properly sized and aligned block of 199 // it. Basically, this just allocates a properly sized and aligned block of
196 // memory in which we can manually put a T with placement new. 200 // memory in which we can manually put a T with placement new.
197 T value_; 201 T value_;
198 }; 202 };
199 }; 203 };
200 204
201 } // namespace rtc 205 } // namespace rtc
202 206
203 #endif // WEBRTC_BASE_OPTIONAL_H_ 207 #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