OLD | NEW |
| (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_BINDING_SET_H_ | |
6 #define MOJO_PUBLIC_CPP_BINDINGS_BINDING_SET_H_ | |
7 | |
8 #include <assert.h> | |
9 | |
10 #include <algorithm> | |
11 #include <memory> | |
12 #include <vector> | |
13 | |
14 #include "mojo/public/cpp/bindings/binding.h" | |
15 #include "mojo/public/cpp/system/macros.h" | |
16 | |
17 namespace mojo { | |
18 | |
19 // Use this class to manage a set of bindings each of which is | |
20 // owned by the pipe it is bound to. | |
21 template <typename Interface> | |
22 class BindingSet { | |
23 public: | |
24 BindingSet() {} | |
25 ~BindingSet() { CloseAllBindings(); } | |
26 | |
27 // Adds a binding to the list and arranges for it to be removed when | |
28 // a connection error occurs. Does not take ownership of |impl|, which | |
29 // must outlive the binding set. | |
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 assert(it != bindings_.end()); | |
42 bindings_.erase(it); | |
43 }); | |
44 } | |
45 | |
46 void CloseAllBindings() { bindings_.clear(); } | |
47 | |
48 size_t size() const { return bindings_.size(); } | |
49 | |
50 private: | |
51 std::vector<std::unique_ptr<Binding<Interface>>> bindings_; | |
52 | |
53 MOJO_DISALLOW_COPY_AND_ASSIGN(BindingSet); | |
54 }; | |
55 | |
56 } // namespace mojo | |
57 | |
58 #endif // MOJO_PUBLIC_CPP_BINDINGS_BINDING_SET_H_ | |
OLD | NEW |