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

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

Issue 791493006: De-client tracing.TraceController interface (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 6 years 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 #include <vector>
6
7 #include "base/memory/weak_ptr.h"
8 #include "mojo/public/cpp/bindings/interface_ptr.h"
9
10 namespace mojo {
11
12 template <typename Interface>
13 class WeakInterfacePtr;
14
15 template <typename Interface>
16 class WeakInterfacePtrSet {
17 public:
18 WeakInterfacePtrSet() {}
19 ~WeakInterfacePtrSet() { CloseAll(); }
20
21 void AddInterfacePtr(InterfacePtr<Interface> ptr) {
22 auto weak_interface_ptr = new WeakInterfacePtr<Interface>(ptr.Pass());
23 ptrs_.push_back(weak_interface_ptr->GetWeakPtr());
24 ClearNullInterfacePtrs();
25 }
26
27 template <typename FunctionType>
28 void ForAllPtrs(FunctionType function) {
29 for (const auto& it : ptrs_) {
30 if (it)
31 function(it->get());
32 }
33 ClearNullInterfacePtrs();
34 }
35
36 void CloseAll() {
37 for (const auto& it : ptrs_) {
38 if (it)
39 it->Close();
40 }
41 ptrs_.clear();
42 }
43
44 private:
45 using WPWIPI = base::WeakPtr<WeakInterfacePtr<Interface>>;
46
47 void ClearNullInterfacePtrs() {
48 ptrs_.erase(std::remove_if(ptrs_.begin(), ptrs_.end(), [](const WPWIPI& p) {
49 return p.get() == nullptr;
50 }), ptrs_.end());
51 }
52
53 std::vector<WPWIPI> ptrs_;
54 };
55
56 template <typename Interface>
57 class WeakInterfacePtr : public ErrorHandler {
58 public:
59 explicit WeakInterfacePtr(InterfacePtr<Interface> ptr)
60 : ptr_(ptr.Pass()), weak_ptr_factory_(this) {
61 ptr_.set_error_handler(this);
62 }
63 ~WeakInterfacePtr() override {}
64
65 void Close() { ptr_.reset(); }
66
67 Interface* get() { return ptr_.get(); }
68
69 base::WeakPtr<WeakInterfacePtr> GetWeakPtr() {
70 return weak_ptr_factory_.GetWeakPtr();
71 }
72
73 private:
74 // ErrorHandler implementation
75 void OnConnectionError() override { delete this; }
76
77 InterfacePtr<Interface> ptr_;
78 base::WeakPtrFactory<WeakInterfacePtr> weak_ptr_factory_;
79
80 DISALLOW_COPY_AND_ASSIGN(WeakInterfacePtr);
81 };
82
83 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698