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

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

Issue 2424063002: Add rtc::Optional::emplace (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 175 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 186
187 // Destroy any contained value. Has no effect if we have no value. 187 // Destroy any contained value. Has no effect if we have no value.
188 void reset() { 188 void reset() {
189 if (!has_value_) 189 if (!has_value_)
190 return; 190 return;
191 value_.~T(); 191 value_.~T();
192 has_value_ = false; 192 has_value_ = false;
193 PoisonValue(); 193 PoisonValue();
194 } 194 }
195 195
196 template <class... Args>
197 void emplace(Args&&... args) {
198 if (has_value_)
199 value_.~T();
200 else
201 UnpoisonValue();
202 new (&value_) T(std::forward<Args>(args)...);
203 has_value_ = true;
204 }
205
196 // Conversion to bool to test if we have a value. 206 // Conversion to bool to test if we have a value.
197 explicit operator bool() const { return has_value_; } 207 explicit operator bool() const { return has_value_; }
198 208
199 // Dereferencing. Only allowed if we have a value. 209 // Dereferencing. Only allowed if we have a value.
200 const T* operator->() const { 210 const T* operator->() const {
201 RTC_DCHECK(has_value_); 211 RTC_DCHECK(has_value_);
202 return &value_; 212 return &value_;
203 } 213 }
204 T* operator->() { 214 T* operator->() {
205 RTC_DCHECK(has_value_); 215 RTC_DCHECK(has_value_);
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
258 // construct it, and the Optional destructor won't automatically destroy 268 // construct it, and the Optional destructor won't automatically destroy
259 // it. Basically, this just allocates a properly sized and aligned block of 269 // it. Basically, this just allocates a properly sized and aligned block of
260 // memory in which we can manually put a T with placement new. 270 // memory in which we can manually put a T with placement new.
261 T value_; 271 T value_;
262 }; 272 };
263 }; 273 };
264 274
265 } // namespace rtc 275 } // namespace rtc
266 276
267 #endif // WEBRTC_BASE_OPTIONAL_H_ 277 #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