| 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_STRONG_BINDING_SET_H_ | |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_STRONG_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 strong bindings each of which is | |
| 20 // owned by the pipe it is bound to. The set takes ownership of the | |
| 21 // interfaces and will delete them when the bindings are closed. | |
| 22 template <typename Interface> | |
| 23 class StrongBindingSet { | |
| 24 public: | |
| 25 StrongBindingSet() {} | |
| 26 ~StrongBindingSet() { CloseAllBindings(); } | |
| 27 | |
| 28 // Adds a binding to the list and arranges for it to be removed when | |
| 29 // a connection error occurs. Takes ownership of |impl|, which | |
| 30 // will be deleted when the binding is closed. | |
| 31 void AddBinding(Interface* impl, InterfaceRequest<Interface> request) { | |
| 32 bindings_.emplace_back(new Binding<Interface>(impl, request.Pass())); | |
| 33 auto* binding = bindings_.back().get(); | |
| 34 // Set the connection error handler for the newly added Binding to be a | |
| 35 // function that will erase it from the vector. | |
| 36 binding->set_connection_error_handler([this, binding]() { | |
| 37 auto it = | |
| 38 std::find_if(bindings_.begin(), bindings_.end(), | |
| 39 [binding](const std::unique_ptr<Binding<Interface>>& b) { | |
| 40 return (b.get() == binding); | |
| 41 }); | |
| 42 assert(it != bindings_.end()); | |
| 43 delete binding->impl(); | |
| 44 bindings_.erase(it); | |
| 45 }); | |
| 46 } | |
| 47 | |
| 48 // Removes all bindings for the specified interface implementation. | |
| 49 // The implementation object is not destroyed. | |
| 50 void RemoveBindings(Interface* impl) { | |
| 51 bindings_.erase( | |
| 52 std::remove_if(bindings_.begin(), bindings_.end(), | |
| 53 [impl](const std::unique_ptr<Binding<Interface>>& b) { | |
| 54 return (b->impl() == impl); | |
| 55 })); | |
| 56 } | |
| 57 | |
| 58 // Closes all bindings and deletes their associated interfaces. | |
| 59 void CloseAllBindings() { | |
| 60 for (auto it = bindings_.begin(); it != bindings_.end(); ++it) { | |
| 61 delete (*it)->impl(); | |
| 62 } | |
| 63 bindings_.clear(); | |
| 64 } | |
| 65 | |
| 66 size_t size() const { return bindings_.size(); } | |
| 67 | |
| 68 private: | |
| 69 std::vector<std::unique_ptr<Binding<Interface>>> bindings_; | |
| 70 | |
| 71 MOJO_DISALLOW_COPY_AND_ASSIGN(StrongBindingSet); | |
| 72 }; | |
| 73 | |
| 74 } // namespace mojo | |
| 75 | |
| 76 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRONG_BINDING_SET_H_ | |
| OLD | NEW |