Chromium Code Reviews| Index: third_party/WebKit/Source/wtf/Optional.h |
| diff --git a/third_party/WebKit/Source/wtf/Optional.h b/third_party/WebKit/Source/wtf/Optional.h |
| index a28bd804308c3e218f80c575ac77cabae5448102..2dd7da96684070b41a3315ce8fb7ce9c3971db3e 100644 |
| --- a/third_party/WebKit/Source/wtf/Optional.h |
| +++ b/third_party/WebKit/Source/wtf/Optional.h |
| @@ -5,69 +5,20 @@ |
| #ifndef Optional_h |
| #define Optional_h |
| -#include "wtf/Alignment.h" |
| -#include "wtf/Allocator.h" |
| -#include "wtf/Assertions.h" |
| -#include "wtf/Noncopyable.h" |
| -#include "wtf/StdLibExtras.h" |
| - |
| -#include <new> |
| -#include <utility> |
| +#include "base/optional.h" |
| +#include "wtf/TypeTraits.h" |
| namespace WTF { |
| -// This is a lightweight template similar to std::experimental::optional. |
| -// It currently does not support assignment, swapping, comparison, etc. |
| -// |
| -// Use this instead of OwnPtr for cases where you only want to conditionally |
| -// construct a "scope" object. |
| -// |
| -// Example: |
| -// Optional<DrawingRecorder> recorder; |
| -// if (shouldDraw) |
| -// recorder.emplace(constructor, args, here); |
| -// // recorder destroyed at end of scope |
| -// |
| -// It can be used in WTF::Vector. |
| -// |
| -// Note in particular that unlike a pointer, though, dereferencing a const |
| -// optional yields a const reference. |
| - |
| +// See base/optional.h for documentation. |
| template <typename T> |
| -class Optional final { |
| - DISALLOW_NEW_EXCEPT_PLACEMENT_NEW(); |
| - WTF_MAKE_NONCOPYABLE(Optional); |
| -public: |
| - Optional() : m_ptr(nullptr) { } |
| - ~Optional() |
| - { |
| - if (m_ptr) |
| - m_ptr->~T(); |
| - } |
| - |
| - typedef T* Optional::*UnspecifiedBoolType; |
| - operator UnspecifiedBoolType() const { return m_ptr ? &Optional::m_ptr : nullptr; } |
| - |
| - T& operator*() { ASSERT_WITH_SECURITY_IMPLICATION(m_ptr); return *m_ptr; } |
| - const T& operator*() const { ASSERT_WITH_SECURITY_IMPLICATION(m_ptr); return *m_ptr; } |
| - T* operator->() { ASSERT_WITH_SECURITY_IMPLICATION(m_ptr); return m_ptr; } |
| - const T* operator->() const { ASSERT_WITH_SECURITY_IMPLICATION(m_ptr); return m_ptr; } |
| - T* get() { return m_ptr; } |
| - const T* get() const { return m_ptr; } |
| - |
| - template <typename... Args> |
| - void emplace(Args&&... args) |
| - { |
| - RELEASE_ASSERT(!m_ptr); |
| - m_ptr = reinterpret_cast_ptr<T*>(&m_storage.buffer); |
| - new (m_ptr) T(std::forward<Args>(args)...); |
| - } |
| - |
| -private: |
| - T* m_ptr; |
| - AlignedBuffer<sizeof(T), WTF_ALIGN_OF(T)> m_storage; |
| +class Optional : public base::Optional<T> { |
| + static_assert(!IsGarbageCollectedType<T>::value, "Garbage collected types should not be stack-allocated."); |
|
jbroman
2016/04/20 21:13:24
FYI, doing things this way will cause most of the
danakj
2016/04/20 21:33:22
Yes! That's what I really want, thank you for the
|
| }; |
| +constexpr base::nullopt_t nullopt = base::nullopt; |
| +constexpr base::in_place_t in_place = base::in_place; |
| + |
| } // namespace WTF |
| using WTF::Optional; |