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_COMMON_WEAK_INTERFACE_PTR_SET_H_ | 5 #ifndef MOJO_COMMON_INTERFACE_PTR_SET_H_ |
6 #define MOJO_COMMON_WEAK_INTERFACE_PTR_SET_H_ | 6 #define MOJO_COMMON_INTERFACE_PTR_SET_H_ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/memory/weak_ptr.h" | |
12 #include "mojo/public/cpp/bindings/interface_ptr.h" | 11 #include "mojo/public/cpp/bindings/interface_ptr.h" |
13 | 12 |
14 namespace mojo { | 13 namespace mojo { |
15 | 14 |
16 // A WeakInterfacePtrSet contains a collection of InterfacePtrs | 15 // An InterfacePtrSet contains a collection of InterfacePtrs |
17 // that are automatically removed from the collection and destroyed | 16 // that are automatically removed from the collection and destroyed |
18 // when their associated MessagePipe experiences a connection error. | 17 // when their associated MessagePipe experiences a connection error. |
19 // When the set is destroyed all of the MessagePipes will be closed. | 18 // When the set is destroyed all of the MessagePipes will be closed. |
20 // TODO(rudominer) Rename this class since the ownership of the elements | |
21 // is not "weak" from the point of view of the client. | |
22 template <typename Interface> | 19 template <typename Interface> |
23 class WeakInterfacePtrSet { | 20 class InterfacePtrSet { |
24 public: | 21 public: |
25 WeakInterfacePtrSet() {} | 22 InterfacePtrSet() {} |
26 ~WeakInterfacePtrSet() { CloseAll(); } | 23 ~InterfacePtrSet() { CloseAll(); } |
27 | 24 |
28 // |ptr| must be bound to a message pipe. | 25 // |ptr| must be bound to a message pipe. |
29 void AddInterfacePtr(InterfacePtr<Interface> ptr) { | 26 void AddInterfacePtr(InterfacePtr<Interface> ptr) { |
30 DCHECK(ptr.is_bound()); | 27 DCHECK(ptr.is_bound()); |
31 ptrs_.emplace_back(ptr.Pass()); | 28 ptrs_.emplace_back(ptr.Pass()); |
32 InterfacePtr<Interface>& intrfc_ptr = ptrs_.back(); | 29 InterfacePtr<Interface>& intrfc_ptr = ptrs_.back(); |
33 Interface* pointer = intrfc_ptr.get(); | 30 Interface* pointer = intrfc_ptr.get(); |
34 // Set the connection error handler for the newly added InterfacePtr to be a | 31 // Set the connection error handler for the newly added InterfacePtr to be a |
35 // function that will erase it from the vector. | 32 // function that will erase it from the vector. |
36 intrfc_ptr.set_connection_error_handler([pointer, this]() { | 33 intrfc_ptr.set_connection_error_handler([pointer, this]() { |
(...skipping 28 matching lines...) Expand all Loading... |
65 } | 62 } |
66 | 63 |
67 size_t size() const { return ptrs_.size(); } | 64 size_t size() const { return ptrs_.size(); } |
68 | 65 |
69 private: | 66 private: |
70 std::vector<InterfacePtr<Interface>> ptrs_; | 67 std::vector<InterfacePtr<Interface>> ptrs_; |
71 }; | 68 }; |
72 | 69 |
73 } // namespace mojo | 70 } // namespace mojo |
74 | 71 |
75 #endif // MOJO_COMMON_WEAK_INTERFACE_PTR_SET_H_ | 72 #endif // MOJO_COMMON_INTERFACE_PTR_SET_H_ |
OLD | NEW |