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

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

Issue 1735583002: Rename WeakBindingSet/WeakInterfacePtrSet to BindingSet/InterfacePtrSet. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 9 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
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 <utility>
9 #include <vector>
10
11 #include "base/macros.h"
12 #include "base/memory/weak_ptr.h"
13 #include "mojo/public/cpp/bindings/interface_ptr.h"
14
15 namespace mojo {
16
17 template <typename Interface>
18 class InterfacePtrSet {
19 public:
20 InterfacePtrSet() {}
21 ~InterfacePtrSet() { CloseAll(); }
22
23 void AddInterfacePtr(InterfacePtr<Interface> ptr) {
24 auto weak_interface_ptr = new Element(std::move(ptr));
25 ptrs_.push_back(weak_interface_ptr->GetWeakPtr());
26 ClearNullInterfacePtrs();
27 }
28
29 template <typename FunctionType>
30 void ForAllPtrs(FunctionType function) {
31 for (const auto& it : ptrs_) {
32 if (it)
33 function(it->get());
34 }
35 ClearNullInterfacePtrs();
36 }
37
38 void CloseAll() {
39 for (const auto& it : ptrs_) {
40 if (it)
41 it->Close();
42 }
43 ptrs_.clear();
44 }
45
46 private:
47 class Element {
48 public:
49 explicit Element(InterfacePtr<Interface> ptr)
50 : ptr_(std::move(ptr)), weak_ptr_factory_(this) {
51 ptr_.set_connection_error_handler([this]() { delete this; });
52 }
53 ~Element() {}
54
55 void Close() { ptr_.reset(); }
56
57 Interface* get() { return ptr_.get(); }
58
59 base::WeakPtr<Element> GetWeakPtr() {
60 return weak_ptr_factory_.GetWeakPtr();
61 }
62
63 private:
64 InterfacePtr<Interface> ptr_;
65 base::WeakPtrFactory<Element> weak_ptr_factory_;
66
67 DISALLOW_COPY_AND_ASSIGN(Element);
68 };
69
70 void ClearNullInterfacePtrs() {
71 ptrs_.erase(std::remove_if(ptrs_.begin(), ptrs_.end(),
72 [](const base::WeakPtr<Element>& p) {
73 return p.get() == nullptr;
74 }),
75 ptrs_.end());
76 }
77
78 std::vector<base::WeakPtr<Element>> ptrs_;
79 };
80
81 } // namespace mojo
82
83 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_SET_H_
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/binding_set.h ('k') | mojo/public/cpp/bindings/tests/pickle_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698