1 // Copyright 2013 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.
5 #ifndef MOJO_PUBLIC_BINDINGS_PASSABLE_H_
6 #define MOJO_PUBLIC_BINDINGS_PASSABLE_H_
8 #include "mojo/public/bindings/lib/bindings_internal.h"
9 #include "mojo/public/cpp/system/core.h"
13 template <typename HandleType>
17 explicit Passable(HandleType* ptr) : ptr_(ptr) {
20 bool is_valid() const {
21 return ptr_ && ptr_->is_valid();
24 HandleType get() const {
25 return ptr_ ? *ptr_ : HandleType();
28 HandleType release() MOJO_WARN_UNUSED_RESULT {
29 return ptr_ ? internal::FetchAndReset(ptr_) : HandleType();
32 ScopedHandleBase<HandleType> Pass() {
33 return ScopedHandleBase<HandleType>(release());
38 // The default copy-ctor and operator= are OK.
43 template <typename HandleType>
44 inline Passable<HandleType> MakePassable(HandleType* ptr) {
45 return Passable<HandleType>(ptr);
48 template <typename HandleType>
49 class AssignableAndPassable : public Passable<HandleType> {
51 explicit AssignableAndPassable(HandleType* ptr) : Passable<HandleType>(ptr) {
55 void operator=(ScopedHandleBase<HandleType> scoper) {
56 reset(scoper.release());
59 void reset(HandleType obj = HandleType()) {
60 ScopedHandleBase<HandleType>(*this->ptr_);
61 this->ptr_->set_value(obj.value());
65 template <typename HandleType>
66 inline AssignableAndPassable<HandleType> MakeAssignableAndPassable(
68 return AssignableAndPassable<HandleType>(ptr);
73 #endif // MOJO_PUBLIC_BINDINGS_PASSABLE_H_