OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef BASE_OPTIONAL_H_ | |
6 #define BASE_OPTIONAL_H_ | |
7 | |
8 #include <cstddef> | |
9 | |
10 #include "base/logging.h" | |
11 | |
12 namespace base { | |
13 | |
14 // base::Optional is a Chromium version of the C++ library experimental optional | |
15 // class: http://en.cppreference.com/w/cpp/experimental/optional/optional | |
16 // It is not meant to be a full implementation of the experimental class but a | |
danakj
2015/10/15 20:45:26
Why? Can you expand?
mlamouri (slow - plz ping)
2015/10/24 09:46:59
Done.
| |
17 // subset of it. It uses nullptr_t instead of exeperimental::nullopt_t. | |
mlamouri (slow - plz ping)
2015/08/23 14:35:03
If preferred, I can create an Optional::nullopt_t?
danakj
2015/10/15 20:45:26
nullptr i guess is misleading cuz this isn't holdi
mlamouri (slow - plz ping)
2015/10/24 09:46:59
Done.
| |
18 template <typename T> | |
19 class Optional { | |
20 public: | |
21 Optional() : value_(), is_null_(true) { } | |
22 Optional(std::nullptr_t) : value_(), is_null_(true) { } | |
danakj
2015/10/15 20:45:26
std::nullptr_t is a c++11 library feature. no go.
mlamouri (slow - plz ping)
2015/10/24 09:46:59
I switched to base::nullopt_t;
| |
23 Optional(const T& value) : value_(value), is_null_(false) { } | |
24 Optional(const Optional& other) | |
25 : value_(other.value_), is_null_(other.is_null_) { | |
26 } | |
27 | |
28 Optional& operator=(std::nullptr_t) { | |
danakj
2015/10/15 20:45:26
styleguide says to name all arguments even if not
mlamouri (slow - plz ping)
2015/10/24 09:46:59
Done.
| |
29 value_ = T(); | |
30 is_null_ = true; | |
31 return *this; | |
32 } | |
33 | |
34 Optional& operator=(const Optional& other) { | |
35 value_ = other.value_; | |
36 is_null_ = other.is_null_; | |
37 return *this; | |
38 } | |
39 | |
40 Optional& operator=(const T& value) { | |
41 value_ = value; | |
42 is_null_ = false; | |
43 return *this; | |
44 } | |
45 | |
46 const T* operator->() const { DCHECK(!is_null_); return &value_; } | |
47 T* operator->() { DCHECK(!is_null_); return &value_; } | |
48 const T& operator*() const { DCHECK(!is_null_); return value_; } | |
49 T& operator*() { DCHECK(!is_null_); return value_; } | |
50 | |
51 explicit operator bool() const { return !is_null_; } | |
52 | |
53 const T& value() const { DCHECK(!is_null_); return value_; } | |
54 T& value() { DCHECK(!is_null_); return value_; } | |
55 | |
56 const T& value_or(const T& default_value) const { | |
57 return is_null_ ? default_value : value_; | |
58 } | |
59 | |
60 bool operator==(const Optional& other) const { | |
61 return (is_null_ && other.is_null_) || | |
62 (!is_null_ && !other.is_null_ && value_ == other.value_); | |
63 } | |
64 | |
65 bool operator!=(const Optional& other) const { | |
66 return !this->operator==(other); | |
67 } | |
68 | |
69 private: | |
70 T value_; | |
danakj
2015/10/15 20:45:26
This is going to default-construct a T even if you
mlamouri (slow - plz ping)
2015/10/24 09:46:59
Done.
| |
71 bool is_null_; | |
72 }; | |
73 | |
74 } // namespace base | |
75 | |
76 #endif // BASE_OPTIONAL_H_ | |
OLD | NEW |