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_WEAK_INTERFACE_PTR_SET_H_ |
6 #define MOJO_COMMON_WEAK_INTERFACE_PTR_SET_H_ | 6 #define MOJO_COMMON_WEAK_INTERFACE_PTR_SET_H_ |
7 | 7 |
| 8 #include <utility> |
8 #include <vector> | 9 #include <vector> |
9 | 10 |
10 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
11 #include "mojo/public/cpp/bindings/interface_ptr.h" | 12 #include "mojo/public/cpp/bindings/interface_ptr.h" |
12 | 13 |
13 namespace mojo { | 14 namespace mojo { |
14 | 15 |
15 template <typename Interface> | 16 template <typename Interface> |
16 class WeakInterfacePtr; | 17 class WeakInterfacePtr; |
17 | 18 |
18 template <typename Interface> | 19 template <typename Interface> |
19 class WeakInterfacePtrSet { | 20 class WeakInterfacePtrSet { |
20 public: | 21 public: |
21 WeakInterfacePtrSet() {} | 22 WeakInterfacePtrSet() {} |
22 ~WeakInterfacePtrSet() { CloseAll(); } | 23 ~WeakInterfacePtrSet() { CloseAll(); } |
23 | 24 |
24 void AddInterfacePtr(InterfacePtr<Interface> ptr) { | 25 void AddInterfacePtr(InterfacePtr<Interface> ptr) { |
25 auto weak_interface_ptr = new WeakInterfacePtr<Interface>(ptr.Pass()); | 26 auto weak_interface_ptr = new WeakInterfacePtr<Interface>(std::move(ptr)); |
26 ptrs_.push_back(weak_interface_ptr->GetWeakPtr()); | 27 ptrs_.push_back(weak_interface_ptr->GetWeakPtr()); |
27 ClearNullInterfacePtrs(); | 28 ClearNullInterfacePtrs(); |
28 } | 29 } |
29 | 30 |
30 template <typename FunctionType> | 31 template <typename FunctionType> |
31 void ForAllPtrs(FunctionType function) { | 32 void ForAllPtrs(FunctionType function) { |
32 for (const auto& it : ptrs_) { | 33 for (const auto& it : ptrs_) { |
33 if (it) | 34 if (it) |
34 function(it->get()); | 35 function(it->get()); |
35 } | 36 } |
(...skipping 17 matching lines...) Expand all Loading... |
53 }), ptrs_.end()); | 54 }), ptrs_.end()); |
54 } | 55 } |
55 | 56 |
56 std::vector<WPWIPI> ptrs_; | 57 std::vector<WPWIPI> ptrs_; |
57 }; | 58 }; |
58 | 59 |
59 template <typename Interface> | 60 template <typename Interface> |
60 class WeakInterfacePtr { | 61 class WeakInterfacePtr { |
61 public: | 62 public: |
62 explicit WeakInterfacePtr(InterfacePtr<Interface> ptr) | 63 explicit WeakInterfacePtr(InterfacePtr<Interface> ptr) |
63 : ptr_(ptr.Pass()), weak_ptr_factory_(this) { | 64 : ptr_(std::move(ptr)), weak_ptr_factory_(this) { |
64 ptr_.set_connection_error_handler([this]() { delete this; }); | 65 ptr_.set_connection_error_handler([this]() { delete this; }); |
65 } | 66 } |
66 ~WeakInterfacePtr() {} | 67 ~WeakInterfacePtr() {} |
67 | 68 |
68 void Close() { ptr_.reset(); } | 69 void Close() { ptr_.reset(); } |
69 | 70 |
70 Interface* get() { return ptr_.get(); } | 71 Interface* get() { return ptr_.get(); } |
71 | 72 |
72 base::WeakPtr<WeakInterfacePtr> GetWeakPtr() { | 73 base::WeakPtr<WeakInterfacePtr> GetWeakPtr() { |
73 return weak_ptr_factory_.GetWeakPtr(); | 74 return weak_ptr_factory_.GetWeakPtr(); |
74 } | 75 } |
75 | 76 |
76 private: | 77 private: |
77 InterfacePtr<Interface> ptr_; | 78 InterfacePtr<Interface> ptr_; |
78 base::WeakPtrFactory<WeakInterfacePtr> weak_ptr_factory_; | 79 base::WeakPtrFactory<WeakInterfacePtr> weak_ptr_factory_; |
79 | 80 |
80 DISALLOW_COPY_AND_ASSIGN(WeakInterfacePtr); | 81 DISALLOW_COPY_AND_ASSIGN(WeakInterfacePtr); |
81 }; | 82 }; |
82 | 83 |
83 } // namespace mojo | 84 } // namespace mojo |
84 | 85 |
85 #endif // MOJO_COMMON_WEAK_INTERFACE_PTR_SET_H_ | 86 #endif // MOJO_COMMON_WEAK_INTERFACE_PTR_SET_H_ |
OLD | NEW |