OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_OPTIONAL_H_ | 5 #ifndef BASE_OPTIONAL_H_ |
6 #define BASE_OPTIONAL_H_ | 6 #define BASE_OPTIONAL_H_ |
7 | 7 |
| 8 #include <ostream> |
8 #include <type_traits> | 9 #include <type_traits> |
9 | 10 |
10 #include "base/logging.h" | 11 #include "base/logging.h" |
11 #include "base/memory/aligned_memory.h" | 12 #include "base/memory/aligned_memory.h" |
12 #include "base/template_util.h" | 13 #include "base/template_util.h" |
13 | 14 |
14 namespace base { | 15 namespace base { |
15 | 16 |
16 // Specification: | 17 // Specification: |
17 // http://en.cppreference.com/w/cpp/utility/optional/in_place_t | 18 // http://en.cppreference.com/w/cpp/utility/optional/in_place_t |
(...skipping 411 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
429 template <class T> | 430 template <class T> |
430 constexpr bool operator>=(const T& value, const Optional<T>& opt) { | 431 constexpr bool operator>=(const T& value, const Optional<T>& opt) { |
431 return !(value < opt); | 432 return !(value < opt); |
432 } | 433 } |
433 | 434 |
434 template <class T> | 435 template <class T> |
435 constexpr Optional<typename std::decay<T>::type> make_optional(T&& value) { | 436 constexpr Optional<typename std::decay<T>::type> make_optional(T&& value) { |
436 return Optional<typename std::decay<T>::type>(std::forward<T>(value)); | 437 return Optional<typename std::decay<T>::type>(std::forward<T>(value)); |
437 } | 438 } |
438 | 439 |
| 440 // For logging use only. |
| 441 template <class T> |
| 442 std::ostream& operator<<(std::ostream& os, const Optional<T>& opt) { |
| 443 if (opt) |
| 444 os << opt.value(); |
| 445 else |
| 446 os << "(unset)"; |
| 447 return os; |
| 448 } |
| 449 |
439 template <class T> | 450 template <class T> |
440 void swap(Optional<T>& lhs, Optional<T>& rhs) { | 451 void swap(Optional<T>& lhs, Optional<T>& rhs) { |
441 lhs.swap(rhs); | 452 lhs.swap(rhs); |
442 } | 453 } |
443 | 454 |
444 } // namespace base | 455 } // namespace base |
445 | 456 |
446 namespace std { | 457 namespace std { |
447 | 458 |
448 template <class T> | 459 template <class T> |
449 struct hash<base::Optional<T>> { | 460 struct hash<base::Optional<T>> { |
450 size_t operator()(const base::Optional<T>& opt) const { | 461 size_t operator()(const base::Optional<T>& opt) const { |
451 return opt == base::nullopt ? 0 : std::hash<T>()(*opt); | 462 return opt == base::nullopt ? 0 : std::hash<T>()(*opt); |
452 } | 463 } |
453 }; | 464 }; |
454 | 465 |
455 } // namespace std | 466 } // namespace std |
456 | 467 |
457 #endif // BASE_OPTIONAL_H_ | 468 #endif // BASE_OPTIONAL_H_ |
OLD | NEW |