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_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_ |
OLD | NEW |