Index: Source/bindings/v8/Optional.h |
diff --git a/Source/bindings/v8/Optional.h b/Source/bindings/v8/Optional.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..4a1beee5fdeafdda9d79f84675be9e38cac2fc40 |
--- /dev/null |
+++ b/Source/bindings/v8/Optional.h |
@@ -0,0 +1,58 @@ |
+// Copyright 2014 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 Optional_h |
+#define Optional_h |
+ |
+#include "wtf/Assertions.h" |
+ |
+namespace WebCore { |
+ |
+template <typename T> |
+class Optional { |
+public: |
+ Optional(const T& value, bool isMissing) |
+ : m_value(value) |
+ , m_isMissing(isMissing) { } |
+ |
+ Optional(const Optional& other) |
+ : m_value(other.m_value) |
+ , m_isMissing(other.m_isMissing) { } |
+ |
+ template <typename U> |
+ Optional(const Optional<U>& other) |
Jens Widell
2014/06/17 09:53:37
This operator might be useful (or not work at all)
|
+ : m_value(other.m_value) |
+ , m_isMissing(other.m_isMissing) { } |
+ |
+ Optional& operator=(const Optional& other) |
+ { |
+ m_value = other.m_value; |
+ m_isMissing = other.m_isMissing; |
+ return *this; |
+ } |
+ |
+ T get() const { ASSERT(!m_isMissing); return m_value; } |
+ bool isMissing() const { return m_isMissing; } |
+ |
+ operator bool() const { return !m_isMissing && m_value; } |
+ |
+ bool operator==(const Optional& other) const |
+ { |
+ return (m_isMissing && other.m_isMissing) || (!m_isMissing && !other.m_isMissing && m_value == other.m_value); |
+ } |
+ |
+private: |
+ T m_value; |
+ bool m_isMissing; |
+}; |
+ |
+template <typename T> |
+Optional<T> makeOptional(T value, bool isMissing) |
+{ |
+ return Optional<T>(value, isMissing); |
+} |
+ |
+} // namespace WebCore |
+ |
+#endif // Optional_h |