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

Side by Side Diff: mojo/common/weak_interface_ptr_set.h

Issue 1841863002: Update monet. (Closed) Base URL: https://github.com/domokit/monet.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 | « mojo/common/weak_binding_set.h ('k') | mojo/converters/base/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #ifndef MOJO_COMMON_WEAK_INTERFACE_PTR_SET_H_
6 #define MOJO_COMMON_WEAK_INTERFACE_PTR_SET_H_
7
8 #include <vector>
9
10 #include "base/logging.h"
11 #include "base/memory/weak_ptr.h"
12 #include "mojo/public/cpp/bindings/interface_ptr.h"
13
14 namespace mojo {
15
16 namespace internal {
17
18 // TODO(vtl): This name of this class is a little odd -- it's not a "weak
19 // pointer", but a wrapper around InterfacePtr that owns itself and can vend
20 // weak pointers to itself. Probably, with connection error callbacks instead of
21 // ErrorHandlers, this class is unneeded, and WeakInterfacePtrSet can simply
22 // own/remove interface pointers as connection errors occur.
23 // https://github.com/domokit/mojo/issues/311
24 template <typename Interface>
25 class WeakInterfacePtr {
26 public:
27 explicit WeakInterfacePtr(InterfacePtr<Interface> ptr)
28 : ptr_(ptr.Pass()), weak_ptr_factory_(this) {
29 ptr_.set_connection_error_handler([this]() { delete this; });
30 }
31 ~WeakInterfacePtr() {}
32
33 void Close() { ptr_.reset(); }
34
35 Interface* get() { return ptr_.get(); }
36
37 base::WeakPtr<WeakInterfacePtr> GetWeakPtr() {
38 return weak_ptr_factory_.GetWeakPtr();
39 }
40
41 private:
42 InterfacePtr<Interface> ptr_;
43 base::WeakPtrFactory<WeakInterfacePtr> weak_ptr_factory_;
44
45 DISALLOW_COPY_AND_ASSIGN(WeakInterfacePtr);
46 };
47
48 } // namespace internal
49
50 // A WeakInterfacePtrSet contains a collection of InterfacePtrs
51 // that are automatically removed from the collection and destroyed
52 // when their associated MessagePipe experiences a connection error.
53 // When the set is destroyed all of the MessagePipes will be closed.
54 // TODO(rudominer) Rename this class since the ownership of the elements
55 // is not "weak" from the point of view of the client.
56 template <typename Interface>
57 class WeakInterfacePtrSet {
58 public:
59 WeakInterfacePtrSet() {}
60 ~WeakInterfacePtrSet() { CloseAll(); }
61
62 // |ptr| must be bound to a message pipe.
63 void AddInterfacePtr(InterfacePtr<Interface> ptr) {
64 DCHECK(ptr.is_bound());
65 auto weak_interface_ptr =
66 new internal::WeakInterfacePtr<Interface>(ptr.Pass());
67 ptrs_.push_back(weak_interface_ptr->GetWeakPtr());
68 ClearNullInterfacePtrs();
69 }
70
71 // Applies |function| to each of the InterfacePtrs in the set.
72 template <typename FunctionType>
73 void ForAllPtrs(FunctionType function) {
74 for (const auto& it : ptrs_) {
75 if (it)
76 function(it->get());
77 }
78 ClearNullInterfacePtrs();
79 }
80
81 // Closes the MessagePipe associated with each of the InterfacePtrs in
82 // this set and clears the set.
83 void CloseAll() {
84 for (const auto& it : ptrs_) {
85 if (it)
86 it->Close();
87 }
88 ptrs_.clear();
89 }
90
91 // TODO(rudominer) After reworking this class and eliminating the method
92 // ClearNullInterfacePtrs, this method should become const.
93 size_t size() {
94 ClearNullInterfacePtrs();
95 return ptrs_.size();
96 }
97
98 private:
99 using WPWIPI = base::WeakPtr<internal::WeakInterfacePtr<Interface>>;
100
101 void ClearNullInterfacePtrs() {
102 ptrs_.erase(std::remove_if(ptrs_.begin(), ptrs_.end(), [](const WPWIPI& p) {
103 return p.get() == nullptr;
104 }), ptrs_.end());
105 }
106
107 std::vector<WPWIPI> ptrs_;
108 };
109
110 } // namespace mojo
111
112 #endif // MOJO_COMMON_WEAK_INTERFACE_PTR_SET_H_
OLDNEW
« no previous file with comments | « mojo/common/weak_binding_set.h ('k') | mojo/converters/base/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698