Chromium Code Reviews| Index: mojo/public/cpp/bindings/lib/scoped_interface_endpoint_handle.cc |
| diff --git a/mojo/public/cpp/bindings/lib/scoped_interface_endpoint_handle.cc b/mojo/public/cpp/bindings/lib/scoped_interface_endpoint_handle.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..500a358fff1038a232fb6e717d23c0ddb3e3fe4b |
| --- /dev/null |
| +++ b/mojo/public/cpp/bindings/lib/scoped_interface_endpoint_handle.cc |
| @@ -0,0 +1,75 @@ |
| +// 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. |
| + |
| +#include "mojo/public/cpp/bindings/lib/scoped_interface_endpoint_handle.h" |
| + |
| +#include "base/logging.h" |
| +#include "mojo/public/cpp/bindings/lib/multiplex_router.h" |
| + |
| +namespace mojo { |
| +namespace internal { |
| + |
| +ScopedInterfaceEndpointHandle::ScopedInterfaceEndpointHandle() |
| + : id_(kInvalidInterfaceId), is_local_(true) {} |
|
sky
2015/11/19 17:16:27
Use delegating constructors.
yzshen1
2015/11/19 22:00:41
Done.
|
| + |
| +ScopedInterfaceEndpointHandle::ScopedInterfaceEndpointHandle( |
| + InterfaceId id, |
| + bool is_local, |
| + scoped_refptr<MultiplexRouter> router) |
| + : id_(id), is_local_(is_local), router_(router.Pass()) { |
| + DCHECK(!IsValidInterfaceId(id) || router_); |
| +} |
| + |
| +ScopedInterfaceEndpointHandle::ScopedInterfaceEndpointHandle( |
| + ScopedInterfaceEndpointHandle&& other) |
| + : id_(other.id_), is_local_(other.is_local_) { |
| + router_.swap(other.router_); |
| + other.id_ = kInvalidInterfaceId; |
| +} |
| + |
| +ScopedInterfaceEndpointHandle::~ScopedInterfaceEndpointHandle() { |
| + reset(); |
| +} |
| + |
| +ScopedInterfaceEndpointHandle& ScopedInterfaceEndpointHandle::operator=( |
| + ScopedInterfaceEndpointHandle&& other) { |
| + reset(); |
|
sky
2015/11/19 17:16:27
Do you need to handle the case of other == this?
yzshen1
2015/11/19 22:00:41
Because this is a move assignment, I think it is u
|
| + |
| + id_ = other.id_; |
| + is_local_ = other.is_local_; |
| + router_.swap(other.router_); |
| + |
| + other.id_ = kInvalidInterfaceId; |
| + |
| + return *this; |
| +} |
| + |
| +void ScopedInterfaceEndpointHandle::reset() { |
| + if (!IsValidInterfaceId(id_)) |
| + return; |
| + |
| + router_->CloseEndpointHandle(id_, is_local_); |
| + |
| + id_ = kInvalidInterfaceId; |
| + router_ = nullptr; |
| +} |
| + |
| +void ScopedInterfaceEndpointHandle::swap(ScopedInterfaceEndpointHandle& other) { |
| + using std::swap; |
| + swap(other.id_, id_); |
| + swap(other.is_local_, is_local_); |
| + swap(other.router_, router_); |
| +} |
| + |
| +InterfaceId ScopedInterfaceEndpointHandle::release() { |
| + InterfaceId result = id_; |
| + |
| + id_ = kInvalidInterfaceId; |
| + router_ = nullptr; |
| + |
| + return result; |
| +} |
| + |
| +} // namespace internal |
| +} // namespace mojo |