Chromium Code Reviews| Index: mojo/common/service_set.h |
| diff --git a/mojo/common/service_set.h b/mojo/common/service_set.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..10ff439c3465ad482ea18bb5fa368fa902638360 |
| --- /dev/null |
| +++ b/mojo/common/service_set.h |
| @@ -0,0 +1,143 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef MOJO_COMMON_SERVICE_SET_H_ |
| +#define MOJO_COMMON_SERVICE_SET_H_ |
| + |
| +#include <map> |
|
Anand Mistry (off Chromium)
2015/11/12 07:11:32
nit: blank line after stl includes
Sam McNally
2015/11/13 00:49:59
Done.
|
| +#include "base/bind.h" |
| +#include "base/logging.h" |
| +#include "base/macros.h" |
| +#include "third_party/mojo/src/mojo/public/cpp/bindings/binding.h" |
| + |
| +namespace mojo { |
| + |
| +// A set of mojo services where each service's lifetime is bound to a message |
| +// pipe to its client. The template parameters Interface and Impl should be a |
| +// mojo interface and a concrete implementation of that interface, respectively. |
| +// |
| +// To create an instance of Impl in response to a request, call |
| +// CreateService(request.Pass(), <other args to Impl's constructor>); |
| +// |
| +// When the client of a service closes its connection, that service is |
| +// automatically destroyed. set_connection_error_handler() can be used to |
| +// register an ErrorHandler to be called when this occurs. When the handler is |
| +// called, the service is no longer contained in the set but is only destroyed |
| +// after the callback returns. |
| +// |
| +// A service may be manually destroyed by calling DestroyService(). Services |
| +// destroyed this way will not trigger a call to the ErrorHandler. |
| +template <typename Interface, typename Impl> |
| +class ServiceSet { |
| + public: |
| + using ErrorHandler = Callback<void(Impl*)>; |
| + class Iterator; |
| + |
| + ServiceSet() = default; |
| + |
| + void set_connection_error_handler(const ErrorHandler& error_handler) { |
| + error_handler_ = error_handler; |
| + } |
| + |
| + template <typename... Args> |
| + Impl* CreateService(InterfaceRequest<Interface> request, Args&&... args) { |
| + auto holder = make_scoped_ptr( |
| + new Holder(this, request.Pass(), std::forward<Args>(args)...)); |
|
Anand Mistry (off Chromium)
2015/11/12 07:11:32
#include <utility>
for std::forward
Sam McNally
2015/11/13 00:49:59
Done.
|
| + Impl* impl = holder->impl(); |
| + holders_.insert(std::make_pair(impl, holder.Pass())); |
| + return impl; |
| + } |
| + |
| + void DestroyService(Impl* impl) { |
| + size_t num_erased = holders_.erase(impl); |
| + DCHECK_EQ(1u, num_erased); |
| + } |
| + |
| + void clear() { holders_.clear(); } |
| + bool empty() const { return holders_.empty(); } |
| + size_t size() const { return holders_.size(); } |
| + Iterator begin() const { return Iterator(holders_.begin()); } |
| + Iterator end() const { return Iterator(holders_.end()); } |
| + |
| + private: |
| + class Holder; |
| + using Container = std::map<Impl*, scoped_ptr<Holder>>; |
| + |
| + void OnConnectionError(Holder* holder) { |
| + Impl* impl = holder->impl(); |
| + auto it = holders_.find(holder->impl()); |
| + DCHECK(it != holders_.end()); |
| + scoped_ptr<Holder> holder_owner = it->second.Pass(); |
| + holders_.erase(it); |
| + if (!error_handler_.is_null()) |
| + error_handler_.Run(impl); |
| + } |
| + |
| + ErrorHandler error_handler_; |
| + Container holders_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ServiceSet); |
| +}; |
| + |
| +template <typename Interface, typename Impl> |
| +class ServiceSet<Interface, Impl>::Holder { |
| + public: |
| + template <typename... Args> |
| + Holder(ServiceSet* service_set, |
| + InterfaceRequest<Interface> request, |
| + Args&&... args) |
| + : impl_(std::forward<Args>(args)...), |
| + binding_(&impl_, request.Pass()), |
| + service_set_(service_set) { |
| + binding_.set_connection_error_handler( |
| + base::Bind(&Holder::OnConnectionError, base::Unretained(this))); |
| + } |
| + |
| + void OnConnectionError() { service_set_->OnConnectionError(this); } |
| + |
| + Impl* impl() { return &impl_; } |
| + |
| + private: |
| + Impl impl_; |
| + Binding<Interface> binding_; |
| + |
| + // Owns this. |
| + ServiceSet* const service_set_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(Holder); |
| +}; |
| + |
| +template <typename Interface, typename Impl> |
| +class ServiceSet<Interface, Impl>::Iterator { |
| + public: |
| + using iterator_category = std::input_iterator_tag; |
| + using difference_type = std::ptrdiff_t; |
| + using value_type = Impl*; |
| + using pointer = value_type*; |
| + using reference = value_type&; |
| + |
| + Iterator() = default; |
| + |
| + Iterator& operator++() { |
| + ++it_; |
| + return *this; |
| + } |
| + |
| + Impl* operator*() const { return it_->first; } |
| + Impl* operator->() const { return it_->first; } |
| + bool operator==(const Iterator& other) const { return it_ == other.it_; } |
| + bool operator!=(const Iterator& other) const { return it_ != other.it_; } |
| + |
| + private: |
| + using IteratorImpl = typename Container::const_iterator; |
| + friend class ServiceSet; |
| + |
| + explicit Iterator(IteratorImpl it) : it_(it) {} |
| + |
| + IteratorImpl it_; |
| +}; |
| + |
| +} // namespace mojo |
| + |
| +#endif // MOJO_COMMON_SERVICE_SET_H_ |