Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(164)

Side by Side Diff: third_party/WebKit/Source/wtf/Optional.h

Issue 1860143002: Allow Vector<Optional> and Optional::get() (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 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 Optional_h 5 #ifndef Optional_h
6 #define Optional_h 6 #define Optional_h
7 7
8 #include "wtf/Alignment.h" 8 #include "wtf/Alignment.h"
9 #include "wtf/Allocator.h" 9 #include "wtf/Allocator.h"
10 #include "wtf/Assertions.h" 10 #include "wtf/Assertions.h"
(...skipping 10 matching lines...) Expand all
21 // 21 //
22 // Use this instead of OwnPtr for cases where you only want to conditionally 22 // Use this instead of OwnPtr for cases where you only want to conditionally
23 // construct a "scope" object. 23 // construct a "scope" object.
24 // 24 //
25 // Example: 25 // Example:
26 // Optional<DrawingRecorder> recorder; 26 // Optional<DrawingRecorder> recorder;
27 // if (shouldDraw) 27 // if (shouldDraw)
28 // recorder.emplace(constructor, args, here); 28 // recorder.emplace(constructor, args, here);
29 // // recorder destroyed at end of scope 29 // // recorder destroyed at end of scope
30 // 30 //
31 // It can be used in WTF::Vector.
32 //
31 // Note in particular that unlike a pointer, though, dereferencing a const 33 // Note in particular that unlike a pointer, though, dereferencing a const
32 // optional yields a const reference. 34 // optional yields a const reference.
33 35
34 template <typename T> 36 template <typename T>
35 class Optional final { 37 class Optional final {
36 DISALLOW_NEW(); 38 DISALLOW_NEW_EXCEPT_PLACEMENT_NEW();
37 WTF_MAKE_NONCOPYABLE(Optional); 39 WTF_MAKE_NONCOPYABLE(Optional);
38 public: 40 public:
39 Optional() : m_ptr(nullptr) { } 41 Optional() : m_ptr(nullptr) { }
40 ~Optional() 42 ~Optional()
41 { 43 {
42 if (m_ptr) 44 if (m_ptr)
43 m_ptr->~T(); 45 m_ptr->~T();
44 } 46 }
45 47
46 typedef T* Optional::*UnspecifiedBoolType; 48 typedef T* Optional::*UnspecifiedBoolType;
47 operator UnspecifiedBoolType() const { return m_ptr ? &Optional::m_ptr : nul lptr; } 49 operator UnspecifiedBoolType() const { return m_ptr ? &Optional::m_ptr : nul lptr; }
48 50
49 T& operator*() { ASSERT_WITH_SECURITY_IMPLICATION(m_ptr); return *m_ptr; } 51 T& operator*() { ASSERT_WITH_SECURITY_IMPLICATION(m_ptr); return *m_ptr; }
50 const T& operator*() const { ASSERT_WITH_SECURITY_IMPLICATION(m_ptr); return *m_ptr; } 52 const T& operator*() const { ASSERT_WITH_SECURITY_IMPLICATION(m_ptr); return *m_ptr; }
51 T* operator->() { ASSERT_WITH_SECURITY_IMPLICATION(m_ptr); return m_ptr; } 53 T* operator->() { ASSERT_WITH_SECURITY_IMPLICATION(m_ptr); return m_ptr; }
52 const T* operator->() const { ASSERT_WITH_SECURITY_IMPLICATION(m_ptr); retur n m_ptr; } 54 const T* operator->() const { ASSERT_WITH_SECURITY_IMPLICATION(m_ptr); retur n m_ptr; }
55 T* get() { return m_ptr; }
56 const T* get() const { return m_ptr; }
53 57
54 template <typename... Args> 58 template <typename... Args>
55 void emplace(Args&&... args) 59 void emplace(Args&&... args)
56 { 60 {
57 RELEASE_ASSERT(!m_ptr); 61 RELEASE_ASSERT(!m_ptr);
58 m_ptr = reinterpret_cast_ptr<T*>(&m_storage.buffer); 62 m_ptr = reinterpret_cast_ptr<T*>(&m_storage.buffer);
59 new (m_ptr) T(std::forward<Args>(args)...); 63 new (m_ptr) T(std::forward<Args>(args)...);
60 } 64 }
61 65
62 private: 66 private:
63 T* m_ptr; 67 T* m_ptr;
64 AlignedBuffer<sizeof(T), WTF_ALIGN_OF(T)> m_storage; 68 AlignedBuffer<sizeof(T), WTF_ALIGN_OF(T)> m_storage;
65 }; 69 };
66 70
67 } // namespace WTF 71 } // namespace WTF
68 72
69 using WTF::Optional; 73 using WTF::Optional;
70 74
71 #endif // Optional_h 75 #endif // Optional_h
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698