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..b7b45ee146e65184549a6cc0fac44e3854634bb3 |
| --- /dev/null |
| +++ b/mojo/common/service_set.h |
| @@ -0,0 +1,130 @@ |
| +// 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 "base/bind.h" |
| +#include "base/callback_internal.h" |
| +#include "base/containers/scoped_ptr_map.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 its message |
|
Anand Mistry (off Chromium)
2015/11/10 04:53:39
This could really do with a longer comment.
Sam McNally
2015/11/11 07:04:02
Done.
|
| +// pipe. |
| +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* AddService(InterfaceRequest<Interface> request, Args&&... args) { |
|
Anand Mistry (off Chromium)
2015/11/10 04:53:39
This doesn't just add a service. Maybe rename to C
Sam McNally
2015/11/11 07:04:02
Done.
|
| + auto holder = make_scoped_ptr(new Holder( |
| + this, request.Pass(), base::internal::CallbackForward(args)...)); |
| + Impl* impl = holder->impl(); |
| + holders_.insert(impl, holder.Pass()); |
| + return impl; |
| + } |
| + |
| + void EraseService(Impl* impl) { |
|
Anand Mistry (off Chromium)
2015/11/10 04:53:39
Maybe RemoveService, or DestroyService if you rena
Sam McNally
2015/11/11 07:04:02
Done.
|
| + size_t num_erased = holders_.erase(impl); |
| + DCHECK_EQ(1u, num_erased); |
|
Anand Mistry (off Chromium)
2015/11/10 04:53:39
IWYU.
#include "base/logging.h"
Sam McNally
2015/11/11 07:04:02
Done.
|
| + } |
| + |
| + void EraseAllServices() { holders_.clear(); } |
|
Anand Mistry (off Chromium)
2015/11/10 04:53:39
Maybe clear() instead? It fits the stl-like patter
Sam McNally
2015/11/11 07:04:02
Done.
|
| + |
| + 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 = base::ScopedPtrMap<Impl*, scoped_ptr<Holder>>; |
| + |
| + void OnConnectionError(Holder* holder) { |
| + Impl* impl = holder->impl(); |
| + EraseService(impl); |
| + if (!error_handler_.is_null()) |
| + error_handler_.Run(impl); |
|
Anand Mistry (off Chromium)
2015/11/10 04:53:39
Hmmm. At this point, |impl| has been deleted. I ca
Sam McNally
2015/11/11 07:04:02
Done.
|
| + } |
| + |
| + ErrorHandler error_handler_; |
| + Container holders_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ServiceSet); |
| +}; |
| + |
| +template <typename Interface, typename Impl> |
| +class ServiceSet<Interface, Impl>::Holder { |
| + public: |
| + using ErrorHandler = base::Callback<void(Holder*)>; |
|
Anand Mistry (off Chromium)
2015/11/10 04:53:39
Unused?
Sam McNally
2015/11/11 07:04:02
Done.
|
| + |
| + template <typename... Args> |
| + Holder(ServiceSet* service_set, |
| + InterfaceRequest<Interface> request, |
| + Args&&... args) |
| + : impl_(base::internal::CallbackForward(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_ |