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

Side by Side Diff: mojo/public/cpp/bindings/interface_ptr_set.h

Issue 1872903003: Generalize InterfacePtrSet for [Associated]InterfacePtr. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Sync and rebase. 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 | « mash/session/session.cc ('k') | mojo/services/tracing/tracing_app.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
11 #include "base/macros.h" 11 #include "base/macros.h"
12 #include "base/memory/weak_ptr.h" 12 #include "base/memory/weak_ptr.h"
13 #include "mojo/public/cpp/bindings/associated_interface_ptr.h"
13 #include "mojo/public/cpp/bindings/interface_ptr.h" 14 #include "mojo/public/cpp/bindings/interface_ptr.h"
14 15
15 namespace mojo { 16 namespace mojo {
16 17
17 template <typename Interface> 18 template <typename Interface, template <typename> class Ptr>
18 class InterfacePtrSet { 19 class PtrSet {
yzshen1 2016/04/08 20:52:56 Please put this in mojo::internal namespace.
msw 2016/04/08 21:12:14 Done.
19 public: 20 public:
20 InterfacePtrSet() {} 21 PtrSet() {}
21 ~InterfacePtrSet() { CloseAll(); } 22 ~PtrSet() { CloseAll(); }
22 23
23 void AddInterfacePtr(InterfacePtr<Interface> ptr) { 24 void AddPtr(Ptr<Interface> ptr) {
24 auto weak_interface_ptr = new Element(std::move(ptr)); 25 auto weak_interface_ptr = new Element(std::move(ptr));
25 ptrs_.push_back(weak_interface_ptr->GetWeakPtr()); 26 ptrs_.push_back(weak_interface_ptr->GetWeakPtr());
26 ClearNullInterfacePtrs(); 27 ClearNullPtrs();
27 } 28 }
28 29
29 template <typename FunctionType> 30 template <typename FunctionType>
30 void ForAllPtrs(FunctionType function) { 31 void ForAllPtrs(FunctionType function) {
31 for (const auto& it : ptrs_) { 32 for (const auto& it : ptrs_) {
32 if (it) 33 if (it)
33 function(it->get()); 34 function(it->get());
34 } 35 }
35 ClearNullInterfacePtrs(); 36 ClearNullPtrs();
36 } 37 }
37 38
38 void CloseAll() { 39 void CloseAll() {
39 for (const auto& it : ptrs_) { 40 for (const auto& it : ptrs_) {
40 if (it) 41 if (it)
41 it->Close(); 42 it->Close();
42 } 43 }
43 ptrs_.clear(); 44 ptrs_.clear();
44 } 45 }
45 46
46 private: 47 private:
47 class Element { 48 class Element {
48 public: 49 public:
49 explicit Element(InterfacePtr<Interface> ptr) 50 explicit Element(Ptr<Interface> ptr)
50 : ptr_(std::move(ptr)), weak_ptr_factory_(this) { 51 : ptr_(std::move(ptr)), weak_ptr_factory_(this) {
51 ptr_.set_connection_error_handler([this]() { delete this; }); 52 ptr_.set_connection_error_handler([this]() { delete this; });
52 } 53 }
53 ~Element() {} 54 ~Element() {}
54 55
55 void Close() { ptr_.reset(); } 56 void Close() { ptr_.reset(); }
56 57
57 Interface* get() { return ptr_.get(); } 58 Interface* get() { return ptr_.get(); }
58 59
59 base::WeakPtr<Element> GetWeakPtr() { 60 base::WeakPtr<Element> GetWeakPtr() {
60 return weak_ptr_factory_.GetWeakPtr(); 61 return weak_ptr_factory_.GetWeakPtr();
61 } 62 }
62 63
63 private: 64 private:
64 InterfacePtr<Interface> ptr_; 65 Ptr<Interface> ptr_;
65 base::WeakPtrFactory<Element> weak_ptr_factory_; 66 base::WeakPtrFactory<Element> weak_ptr_factory_;
66 67
67 DISALLOW_COPY_AND_ASSIGN(Element); 68 DISALLOW_COPY_AND_ASSIGN(Element);
68 }; 69 };
69 70
70 void ClearNullInterfacePtrs() { 71 void ClearNullPtrs() {
71 ptrs_.erase(std::remove_if(ptrs_.begin(), ptrs_.end(), 72 ptrs_.erase(std::remove_if(ptrs_.begin(), ptrs_.end(),
72 [](const base::WeakPtr<Element>& p) { 73 [](const base::WeakPtr<Element>& p) {
73 return p.get() == nullptr; 74 return p.get() == nullptr;
74 }), 75 }),
75 ptrs_.end()); 76 ptrs_.end());
76 } 77 }
77 78
78 std::vector<base::WeakPtr<Element>> ptrs_; 79 std::vector<base::WeakPtr<Element>> ptrs_;
79 }; 80 };
80 81
82 template <typename Interface>
83 using InterfacePtrSet = PtrSet<Interface, InterfacePtr>;
84
85 template <typename Interface>
86 using AssociatedInterfacePtrSet = PtrSet<Interface, AssociatedInterfacePtr>;
87
81 } // namespace mojo 88 } // namespace mojo
82 89
83 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_SET_H_ 90 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_SET_H_
OLDNEW
« no previous file with comments | « mash/session/session.cc ('k') | mojo/services/tracing/tracing_app.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698