OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_SET_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_SET_H_ |
6 #define MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_SET_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_SET_H_ |
7 | 7 |
8 #include <utility> | 8 #include <utility> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 it->Close(); | 43 it->Close(); |
44 } | 44 } |
45 ptrs_.clear(); | 45 ptrs_.clear(); |
46 } | 46 } |
47 | 47 |
48 private: | 48 private: |
49 class Element { | 49 class Element { |
50 public: | 50 public: |
51 explicit Element(Ptr<Interface> ptr) | 51 explicit Element(Ptr<Interface> ptr) |
52 : ptr_(std::move(ptr)), weak_ptr_factory_(this) { | 52 : ptr_(std::move(ptr)), weak_ptr_factory_(this) { |
53 ptr_.set_connection_error_handler([this]() { delete this; }); | 53 ptr_.set_connection_error_handler(base::Bind(&DeleteElement, this)); |
54 } | 54 } |
| 55 |
55 ~Element() {} | 56 ~Element() {} |
56 | 57 |
57 void Close() { ptr_.reset(); } | 58 void Close() { ptr_.reset(); } |
58 | 59 |
59 Interface* get() { return ptr_.get(); } | 60 Interface* get() { return ptr_.get(); } |
60 | 61 |
61 base::WeakPtr<Element> GetWeakPtr() { | 62 base::WeakPtr<Element> GetWeakPtr() { |
62 return weak_ptr_factory_.GetWeakPtr(); | 63 return weak_ptr_factory_.GetWeakPtr(); |
63 } | 64 } |
64 | 65 |
65 private: | 66 private: |
| 67 static void DeleteElement(Element* element) { delete element; } |
| 68 |
66 Ptr<Interface> ptr_; | 69 Ptr<Interface> ptr_; |
67 base::WeakPtrFactory<Element> weak_ptr_factory_; | 70 base::WeakPtrFactory<Element> weak_ptr_factory_; |
68 | 71 |
69 DISALLOW_COPY_AND_ASSIGN(Element); | 72 DISALLOW_COPY_AND_ASSIGN(Element); |
70 }; | 73 }; |
71 | 74 |
72 void ClearNullPtrs() { | 75 void ClearNullPtrs() { |
73 ptrs_.erase(std::remove_if(ptrs_.begin(), ptrs_.end(), | 76 ptrs_.erase(std::remove_if(ptrs_.begin(), ptrs_.end(), |
74 [](const base::WeakPtr<Element>& p) { | 77 [](const base::WeakPtr<Element>& p) { |
75 return p.get() == nullptr; | 78 return p.get() == nullptr; |
76 }), | 79 }), |
77 ptrs_.end()); | 80 ptrs_.end()); |
78 } | 81 } |
79 | 82 |
80 std::vector<base::WeakPtr<Element>> ptrs_; | 83 std::vector<base::WeakPtr<Element>> ptrs_; |
81 }; | 84 }; |
82 | 85 |
83 } // namespace internal | 86 } // namespace internal |
84 | 87 |
85 template <typename Interface> | 88 template <typename Interface> |
86 using InterfacePtrSet = internal::PtrSet<Interface, InterfacePtr>; | 89 using InterfacePtrSet = internal::PtrSet<Interface, InterfacePtr>; |
87 | 90 |
88 template <typename Interface> | 91 template <typename Interface> |
89 using AssociatedInterfacePtrSet = | 92 using AssociatedInterfacePtrSet = |
90 internal::PtrSet<Interface, AssociatedInterfacePtr>; | 93 internal::PtrSet<Interface, AssociatedInterfacePtr>; |
91 | 94 |
92 } // namespace mojo | 95 } // namespace mojo |
93 | 96 |
94 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_SET_H_ | 97 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_SET_H_ |
OLD | NEW |