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

Side by Side Diff: mojo/common/strong_binding_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/mojo_common_export.h ('k') | mojo/common/strong_binding_set_unittest.cc » ('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_STRONG_BINDING_SET_H_
6 #define MOJO_COMMON_STRONG_BINDING_SET_H_
7
8 #include <algorithm>
9 #include <memory>
10 #include <vector>
11
12 #include "base/logging.h"
13 #include "base/macros.h"
14 #include "mojo/public/cpp/bindings/binding.h"
15
16 namespace mojo {
17
18 // Use this class to manage a set of strong bindings each of which is
19 // owned by the pipe it is bound to. The set takes ownership of the
20 // interfaces and will delete them when the bindings are closed.
21 template <typename Interface>
22 class StrongBindingSet {
23 public:
24 StrongBindingSet() {}
25 ~StrongBindingSet() { CloseAllBindings(); }
26
27 // Adds a binding to the list and arranges for it to be removed when
28 // a connection error occurs. Takes ownership of |impl|, which
29 // will be deleted when the binding is closed.
30 void AddBinding(Interface* impl, InterfaceRequest<Interface> request) {
31 bindings_.emplace_back(new Binding<Interface>(impl, request.Pass()));
32 auto* binding = bindings_.back().get();
33 // Set the connection error handler for the newly added Binding to be a
34 // function that will erase it from the vector.
35 binding->set_connection_error_handler([this, binding]() {
36 auto it =
37 std::find_if(bindings_.begin(), bindings_.end(),
38 [binding](const std::unique_ptr<Binding<Interface>>& b) {
39 return (b.get() == binding);
40 });
41 DCHECK(it != bindings_.end());
42 delete binding->impl();
43 bindings_.erase(it);
44 });
45 }
46
47 // Removes all bindings for the specified interface implementation.
48 // The implementation object is not destroyed.
49 void RemoveBindings(Interface* impl) {
50 bindings_.erase(
51 std::remove_if(bindings_.begin(), bindings_.end(),
52 [impl](const std::unique_ptr<Binding<Interface>>& b) {
53 return (b->impl() == impl);
54 }));
55 }
56
57 // Closes all bindings and deletes their associated interfaces.
58 void CloseAllBindings() {
59 for (auto it = bindings_.begin(); it != bindings_.end(); ++it) {
60 delete (*it)->impl();
61 }
62 bindings_.clear();
63 }
64
65 size_t size() const { return bindings_.size(); }
66
67 private:
68 std::vector<std::unique_ptr<Binding<Interface>>> bindings_;
69
70 DISALLOW_COPY_AND_ASSIGN(StrongBindingSet);
71 };
72
73 } // namespace mojo
74
75 #endif // MOJO_COMMON_STRONG_BINDING_SET_H_
OLDNEW
« no previous file with comments | « mojo/common/mojo_common_export.h ('k') | mojo/common/strong_binding_set_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698