Index: base/optional.h |
diff --git a/base/optional.h b/base/optional.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..426a8ebca71d1f05791d69a3d4849119e4294701 |
--- /dev/null |
+++ b/base/optional.h |
@@ -0,0 +1,76 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef BASE_OPTIONAL_H_ |
+#define BASE_OPTIONAL_H_ |
+ |
+#include <cstddef> |
+ |
+#include "base/logging.h" |
+ |
+namespace base { |
+ |
+// base::Optional is a Chromium version of the C++ library experimental optional |
+// class: http://en.cppreference.com/w/cpp/experimental/optional/optional |
+// 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.
|
+// 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.
|
+template <typename T> |
+class Optional { |
+ public: |
+ Optional() : value_(), is_null_(true) { } |
+ 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;
|
+ Optional(const T& value) : value_(value), is_null_(false) { } |
+ Optional(const Optional& other) |
+ : value_(other.value_), is_null_(other.is_null_) { |
+ } |
+ |
+ 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.
|
+ value_ = T(); |
+ is_null_ = true; |
+ return *this; |
+ } |
+ |
+ Optional& operator=(const Optional& other) { |
+ value_ = other.value_; |
+ is_null_ = other.is_null_; |
+ return *this; |
+ } |
+ |
+ Optional& operator=(const T& value) { |
+ value_ = value; |
+ is_null_ = false; |
+ return *this; |
+ } |
+ |
+ const T* operator->() const { DCHECK(!is_null_); return &value_; } |
+ T* operator->() { DCHECK(!is_null_); return &value_; } |
+ const T& operator*() const { DCHECK(!is_null_); return value_; } |
+ T& operator*() { DCHECK(!is_null_); return value_; } |
+ |
+ explicit operator bool() const { return !is_null_; } |
+ |
+ const T& value() const { DCHECK(!is_null_); return value_; } |
+ T& value() { DCHECK(!is_null_); return value_; } |
+ |
+ const T& value_or(const T& default_value) const { |
+ return is_null_ ? default_value : value_; |
+ } |
+ |
+ bool operator==(const Optional& other) const { |
+ return (is_null_ && other.is_null_) || |
+ (!is_null_ && !other.is_null_ && value_ == other.value_); |
+ } |
+ |
+ bool operator!=(const Optional& other) const { |
+ return !this->operator==(other); |
+ } |
+ |
+ private: |
+ 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.
|
+ bool is_null_; |
+}; |
+ |
+} // namespace base |
+ |
+#endif // BASE_OPTIONAL_H_ |