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 2704483002: Add a PrintTo function for rtc::Optional to aid with testing. (Closed)
Patch Set: Created 3 years, 10 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') | webrtc/base/optional_unittest.cc » ('J')
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
11 #ifndef WEBRTC_BASE_OPTIONAL_H_ 11 #ifndef WEBRTC_BASE_OPTIONAL_H_
12 #define WEBRTC_BASE_OPTIONAL_H_ 12 #define WEBRTC_BASE_OPTIONAL_H_
13 13
14 #include <algorithm> 14 #include <algorithm>
15 #include <memory> 15 #include <memory>
16 #include <ostream>
16 #include <utility> 17 #include <utility>
17 18
18 #include "webrtc/base/array_view.h" 19 #include "webrtc/base/array_view.h"
19 #include "webrtc/base/checks.h" 20 #include "webrtc/base/checks.h"
20 #include "webrtc/base/sanitizer.h" 21 #include "webrtc/base/sanitizer.h"
21 22
22 namespace rtc { 23 namespace rtc {
23 24
24 namespace optional_internal { 25 namespace optional_internal {
25 26
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after
289 char empty_; 290 char empty_;
290 // By placing value_ in a union, we get to manage its construction and 291 // By placing value_ in a union, we get to manage its construction and
291 // destruction manually: the Optional constructors won't automatically 292 // destruction manually: the Optional constructors won't automatically
292 // construct it, and the Optional destructor won't automatically destroy 293 // construct it, and the Optional destructor won't automatically destroy
293 // it. Basically, this just allocates a properly sized and aligned block of 294 // it. Basically, this just allocates a properly sized and aligned block of
294 // memory in which we can manually put a T with placement new. 295 // memory in which we can manually put a T with placement new.
295 T value_; 296 T value_;
296 }; 297 };
297 }; 298 };
298 299
300 // Add a PrintTo function for Optionals for use with gtest, only if
301 // there is already a valid operator<< available for the enclosed type.
302 // The cast in decltype will ensure PrintTo has a void return type.
303 template <typename T>
304 auto PrintTo(const Optional<T>& opt, std::ostream* os)
ossu 2017/02/16 14:23:48 Turns out the compiler does clever things that lin
tommi 2017/02/17 13:46:23 Can we call this PrintToForTest? There's a risk t
ossu 2017/02/17 13:50:06 Alas, no, it needs to be called PrintTo for gtest
305 -> decltype(static_cast<void>(*os << *opt)) {
ossu 2017/02/16 13:20:05 The SFINAE here is maybe not super-obvious if one'
kwiberg-webrtc 2017/02/16 14:07:37 Yes, more explanation is probably called for. How
ossu 2017/02/16 14:23:48 Will do!
306 if (opt) {
307 *os << *opt;
kwiberg-webrtc 2017/02/16 14:07:37 This is test code, so maybe indicate that the valu
ossu 2017/02/16 14:23:48 I considered that but was unsure if it would be mo
308 } else {
309 *os << "<empty optional>";
310 }
311 }
312
299 } // namespace rtc 313 } // namespace rtc
300 314
301 #endif // WEBRTC_BASE_OPTIONAL_H_ 315 #endif // WEBRTC_BASE_OPTIONAL_H_
OLDNEW
« no previous file with comments | « no previous file | webrtc/base/optional_unittest.cc » ('j') | webrtc/base/optional_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698