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

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

Issue 2250183003: Make the fuchsia mojo/public repo the source of truth. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 4 years, 4 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/public/cpp/bindings/interface_ptr.h ('k') | mojo/public/cpp/bindings/interface_request.h » ('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_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_SET_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_SET_H_
7
8 #include <assert.h>
9
10 #include <vector>
11
12 #include "mojo/public/cpp/bindings/interface_ptr.h"
13
14 namespace mojo {
15
16 // An InterfacePtrSet contains a collection of InterfacePtrs
17 // that are automatically removed from the collection and destroyed
18 // when their associated MessagePipe experiences a connection error.
19 // When the set is destroyed all of the MessagePipes will be closed.
20 template <typename Interface>
21 class InterfacePtrSet {
22 public:
23 InterfacePtrSet() {}
24 ~InterfacePtrSet() { CloseAll(); }
25
26 // |ptr| must be bound to a message pipe.
27 void AddInterfacePtr(InterfacePtr<Interface> ptr) {
28 assert(ptr.is_bound());
29 ptrs_.emplace_back(ptr.Pass());
30 InterfacePtr<Interface>& intrfc_ptr = ptrs_.back();
31 Interface* pointer = intrfc_ptr.get();
32 // Set the connection error handler for the newly added InterfacePtr to be a
33 // function that will erase it from the vector.
34 intrfc_ptr.set_connection_error_handler([pointer, this]() {
35 // Since InterfacePtr itself is a movable type, the thing that uniquely
36 // identifies the InterfacePtr we wish to erase is its Interface*.
37 auto it = std::find_if(ptrs_.begin(), ptrs_.end(),
38 [pointer](const InterfacePtr<Interface>& p) {
39 return (p.get() == pointer);
40 });
41 assert(it != ptrs_.end());
42 ptrs_.erase(it);
43 });
44 }
45
46 // Applies |function| to each of the InterfacePtrs in the set.
47 template <typename FunctionType>
48 void ForAllPtrs(FunctionType function) {
49 for (const auto& it : ptrs_) {
50 if (it)
51 function(it.get());
52 }
53 }
54
55 // Closes the MessagePipe associated with each of the InterfacePtrs in
56 // this set and clears the set.
57 void CloseAll() {
58 for (auto& it : ptrs_) {
59 if (it)
60 it.reset();
61 }
62 ptrs_.clear();
63 }
64
65 size_t size() const { return ptrs_.size(); }
66
67 private:
68 std::vector<InterfacePtr<Interface>> ptrs_;
69 };
70
71 } // namespace mojo
72
73 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_SET_H_
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/interface_ptr.h ('k') | mojo/public/cpp/bindings/interface_request.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698