OLD | NEW |
| (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_BINDINGS_PASSABLE_H_ | |
6 #define MOJO_PUBLIC_BINDINGS_PASSABLE_H_ | |
7 | |
8 #include "mojo/public/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_BINDINGS_PASSABLE_H_ | |
OLD | NEW |