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_LIB_PASSABLE_H_
6 #define MOJO_PUBLIC_BINDINGS_LIB_PASSABLE_H_
8 #include "mojo/public/system/core_cpp.h"
12 template <typename HandleType>
16 explicit Passable(HandleType* ptr) : ptr_(ptr) {
19 bool is_valid() const {
20 return ptr_ && ptr_->is_valid();
23 HandleType get() const {
24 return ptr_ ? *ptr_ : HandleType();
27 HandleType release() MOJO_WARN_UNUSED_RESULT {
28 return ptr_ ? internal::FetchAndReset(ptr_) : HandleType();
31 ScopedHandleBase<HandleType> Pass() {
32 return ScopedHandleBase<HandleType>(release());
37 // The default copy-ctor and operator= are OK.
42 template <typename HandleType>
43 inline Passable<HandleType> MakePassable(HandleType* ptr) {
44 return Passable<HandleType>(ptr);
47 template <typename HandleType>
48 class AssignableAndPassable : public Passable<HandleType> {
50 explicit AssignableAndPassable(HandleType* ptr) : Passable<HandleType>(ptr) {
54 void operator=(ScopedHandleBase<HandleType> scoper) {
55 reset(scoper.release());
58 void reset(HandleType obj = HandleType()) {
59 ScopedHandleBase<HandleType>(*this->ptr_);
60 this->ptr_->set_value(obj.value());
64 template <typename HandleType>
65 inline AssignableAndPassable<HandleType> MakeAssignableAndPassable(
67 return AssignableAndPassable<HandleType>(ptr);
72 #endif // MOJO_PUBLIC_BINDINGS_LIB_PASSABLE_H_