Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(412)

Side by Side Diff: mojo/public/cpp/bindings/passable.h

Issue 294833002: Mojo: more idiomatic C++ bindings (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix windows bustage Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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.
4
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_PASSABLE_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_PASSABLE_H_
7
8 #include "mojo/public/cpp/bindings/lib/bindings_internal.h"
9 #include "mojo/public/cpp/system/core.h"
10
11 namespace mojo {
12
13 template <typename HandleType>
14 class Passable {
15 public:
16 // |ptr| may be null.
17 explicit Passable(HandleType* ptr) : ptr_(ptr) {
18 }
19
20 bool is_valid() const {
21 return ptr_ && ptr_->is_valid();
22 }
23
24 HandleType get() const {
25 return ptr_ ? *ptr_ : HandleType();
26 }
27
28 HandleType release() MOJO_WARN_UNUSED_RESULT {
29 return ptr_ ? internal::FetchAndReset(ptr_) : HandleType();
30 }
31
32 ScopedHandleBase<HandleType> Pass() {
33 return ScopedHandleBase<HandleType>(release());
34 }
35
36 protected:
37 Passable();
38 // The default copy-ctor and operator= are OK.
39
40 HandleType* ptr_;
41 };
42
43 template <typename HandleType>
44 inline Passable<HandleType> MakePassable(HandleType* ptr) {
45 return Passable<HandleType>(ptr);
46 }
47
48 template <typename HandleType>
49 class AssignableAndPassable : public Passable<HandleType> {
50 public:
51 explicit AssignableAndPassable(HandleType* ptr) : Passable<HandleType>(ptr) {
52 assert(ptr);
53 }
54
55 void operator=(ScopedHandleBase<HandleType> scoper) {
56 reset(scoper.release());
57 }
58
59 void reset(HandleType obj = HandleType()) {
60 ScopedHandleBase<HandleType>(*this->ptr_);
61 this->ptr_->set_value(obj.value());
62 }
63 };
64
65 template <typename HandleType>
66 inline AssignableAndPassable<HandleType> MakeAssignableAndPassable(
67 HandleType* ptr) {
68 return AssignableAndPassable<HandleType>(ptr);
69 }
70
71 } // namespace mojo
72
73 #endif // MOJO_PUBLIC_CPP_BINDINGS_PASSABLE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698