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

Side by Side Diff: mojo/common/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/DEPS ('k') | mojo/common/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_BINDING_SET_H_
6 #define MOJO_COMMON_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 bindings each of which is
19 // owned by the pipe it is bound to.
20 template <typename Interface>
21 class BindingSet {
22 public:
23 BindingSet() {}
24 ~BindingSet() { CloseAllBindings(); }
25
26 // Adds a binding to the list and arranges for it to be removed when
27 // a connection error occurs. Does not take ownership of |impl|, which
28 // must outlive the binding set.
29 void AddBinding(Interface* impl, InterfaceRequest<Interface> request) {
30 bindings_.emplace_back(new Binding<Interface>(impl, request.Pass()));
31 auto* binding = bindings_.back().get();
32 // Set the connection error handler for the newly added Binding to be a
33 // function that will erase it from the vector.
34 binding->set_connection_error_handler([this, binding]() {
35 auto it =
36 std::find_if(bindings_.begin(), bindings_.end(),
37 [binding](const std::unique_ptr<Binding<Interface>>& b) {
38 return (b.get() == binding);
39 });
40 DCHECK(it != bindings_.end());
41 bindings_.erase(it);
42 });
43 }
44
45 void CloseAllBindings() { bindings_.clear(); }
46
47 size_t size() const { return bindings_.size(); }
48
49 private:
50 std::vector<std::unique_ptr<Binding<Interface>>> bindings_;
51
52 DISALLOW_COPY_AND_ASSIGN(BindingSet);
53 };
54
55 } // namespace mojo
56
57 #endif // MOJO_COMMON_BINDING_SET_H_
OLDNEW
« no previous file with comments | « mojo/common/DEPS ('k') | mojo/common/binding_set_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698