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

Side by Side Diff: mojo/public/cpp/system/handle.h

Issue 392783006: Mojo: Split public/cpp/system/core.h into pieces (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 5 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_SYSTEM_HANDLE_H_
6 #define MOJO_PUBLIC_CPP_SYSTEM_HANDLE_H_
7
8 #include <limits>
9
10 #include "mojo/public/c/system/functions.h"
11 #include "mojo/public/c/system/types.h"
12 #include "mojo/public/cpp/system/macros.h"
13
14 namespace mojo {
15
16 // OVERVIEW
17 //
18 // |Handle| and |...Handle|:
19 //
20 // |Handle| is a simple, copyable wrapper for the C type |MojoHandle| (which is
21 // just an integer). Its purpose is to increase type-safety, not provide
22 // lifetime management. For the same purpose, we have trivial *subclasses* of
23 // |Handle|, e.g., |MessagePipeHandle| and |DataPipeProducerHandle|. |Handle|
24 // and its subclasses impose *no* extra overhead over using |MojoHandle|s
25 // directly.
26 //
27 // Note that though we provide constructors for |Handle|/|...Handle| from a
28 // |MojoHandle|, we do not provide, e.g., a constructor for |MessagePipeHandle|
29 // from a |Handle|. This is for type safety: If we did, you'd then be able to
30 // construct a |MessagePipeHandle| from, e.g., a |DataPipeProducerHandle| (since
31 // it's a |Handle|).
32 //
33 // |ScopedHandleBase| and |Scoped...Handle|:
34 //
35 // |ScopedHandleBase<HandleType>| is a templated scoped wrapper, for the handle
36 // types above (in the same sense that a C++11 |unique_ptr<T>| is a scoped
37 // wrapper for a |T*|). It provides lifetime management, closing its owned
38 // handle on destruction. It also provides (emulated) move semantics, again
39 // along the lines of C++11's |unique_ptr| (and exactly like Chromium's
40 // |scoped_ptr|).
41 //
42 // |ScopedHandle| is just (a typedef of) a |ScopedHandleBase<Handle>|.
43 // Similarly, |ScopedMessagePipeHandle| is just a
44 // |ScopedHandleBase<MessagePipeHandle>|. Etc. Note that a
45 // |ScopedMessagePipeHandle| is *not* a (subclass of) |ScopedHandle|.
46 //
47 // Wrapper functions:
48 //
49 // We provide simple wrappers for the |Mojo...()| functions (in
50 // mojo/public/c/system/core.h -- see that file for details on individual
51 // functions).
52 //
53 // The general guideline is functions that imply ownership transfer of a handle
54 // should take (or produce) an appropriate |Scoped...Handle|, while those that
55 // don't take a |...Handle|. For example, |CreateMessagePipe()| has two
56 // |ScopedMessagePipe| "out" parameters, whereas |Wait()| and |WaitMany()| take
57 // |Handle| parameters. Some, have both: e.g., |DuplicatedBuffer()| takes a
58 // suitable (unscoped) handle (e.g., |SharedBufferHandle|) "in" parameter and
59 // produces a suitable scoped handle (e.g., |ScopedSharedBufferHandle| a.k.a.
60 // |ScopedHandleBase<SharedBufferHandle>|) as an "out" parameter.
61 //
62 // An exception are some of the |...Raw()| functions. E.g., |CloseRaw()| takes a
63 // |Handle|, leaving the user to discard the handle.
64 //
65 // More significantly, |WriteMessageRaw()| exposes the full API complexity of
66 // |MojoWriteMessage()| (but doesn't require any extra overhead). It takes a raw
67 // array of |Handle|s as input, and takes ownership of them (i.e., invalidates
68 // them) on *success* (but not on failure). There are a number of reasons for
69 // this. First, C++03 |std::vector|s cannot contain the move-only
70 // |Scoped...Handle|s. Second, |std::vector|s impose extra overhead
71 // (necessitating heap-allocation of the buffer). Third, |std::vector|s wouldn't
72 // provide the desired level of flexibility/safety: a vector of handles would
73 // have to be all of the same type (probably |Handle|/|ScopedHandle|). Fourth,
74 // it's expected to not be used directly, but instead be used by generated
75 // bindings.
76 //
77 // Other |...Raw()| functions expose similar rough edges, e.g., dealing with raw
78 // pointers (and lengths) instead of taking |std::vector|s or similar.
79
80 // ScopedHandleBase ------------------------------------------------------------
81
82 // Scoper for the actual handle types defined further below. It's move-only,
83 // like the C++11 |unique_ptr|.
84 template <class HandleType>
85 class ScopedHandleBase {
86 MOJO_MOVE_ONLY_TYPE_FOR_CPP_03(ScopedHandleBase, RValue)
87
88 public:
89 ScopedHandleBase() {}
90 explicit ScopedHandleBase(HandleType handle) : handle_(handle) {}
91 ~ScopedHandleBase() { CloseIfNecessary(); }
92
93 template <class CompatibleHandleType>
94 explicit ScopedHandleBase(ScopedHandleBase<CompatibleHandleType> other)
95 : handle_(other.release()) {
96 }
97
98 // Move-only constructor and operator=.
99 ScopedHandleBase(RValue other) : handle_(other.object->release()) {}
100 ScopedHandleBase& operator=(RValue other) {
101 if (other.object != this) {
102 CloseIfNecessary();
103 handle_ = other.object->release();
104 }
105 return *this;
106 }
107
108 const HandleType& get() const { return handle_; }
109
110 template <typename PassedHandleType>
111 static ScopedHandleBase<HandleType> From(
112 ScopedHandleBase<PassedHandleType> other) {
113 MOJO_COMPILE_ASSERT(
114 sizeof(static_cast<PassedHandleType*>(static_cast<HandleType*>(0))),
115 HandleType_is_not_a_subtype_of_PassedHandleType);
116 return ScopedHandleBase<HandleType>(
117 static_cast<HandleType>(other.release().value()));
118 }
119
120 void swap(ScopedHandleBase& other) {
121 handle_.swap(other.handle_);
122 }
123
124 HandleType release() MOJO_WARN_UNUSED_RESULT {
125 HandleType rv;
126 rv.swap(handle_);
127 return rv;
128 }
129
130 void reset(HandleType handle = HandleType()) {
131 CloseIfNecessary();
132 handle_ = handle;
133 }
134
135 bool is_valid() const {
136 return handle_.is_valid();
137 }
138
139 private:
140 void CloseIfNecessary() {
141 if (!handle_.is_valid())
142 return;
143 MojoResult result MOJO_ALLOW_UNUSED = MojoClose(handle_.value());
144 assert(result == MOJO_RESULT_OK);
145 }
146
147 HandleType handle_;
148 };
149
150 template <typename HandleType>
151 inline ScopedHandleBase<HandleType> MakeScopedHandle(HandleType handle) {
152 return ScopedHandleBase<HandleType>(handle);
153 }
154
155 // Handle ----------------------------------------------------------------------
156
157 const MojoHandle kInvalidHandleValue = MOJO_HANDLE_INVALID;
158
159 // Wrapper base class for |MojoHandle|.
160 class Handle {
161 public:
162 Handle() : value_(kInvalidHandleValue) {}
163 explicit Handle(MojoHandle value) : value_(value) {}
164 ~Handle() {}
165
166 void swap(Handle& other) {
167 MojoHandle temp = value_;
168 value_ = other.value_;
169 other.value_ = temp;
170 }
171
172 bool is_valid() const {
173 return value_ != kInvalidHandleValue;
174 }
175
176 MojoHandle value() const { return value_; }
177 MojoHandle* mutable_value() { return &value_; }
178 void set_value(MojoHandle value) { value_ = value; }
179
180 private:
181 MojoHandle value_;
182
183 // Copying and assignment allowed.
184 };
185
186 // Should have zero overhead.
187 MOJO_COMPILE_ASSERT(sizeof(Handle) == sizeof(MojoHandle),
188 bad_size_for_cpp_Handle);
189
190 // The scoper should also impose no more overhead.
191 typedef ScopedHandleBase<Handle> ScopedHandle;
192 MOJO_COMPILE_ASSERT(sizeof(ScopedHandle) == sizeof(Handle),
193 bad_size_for_cpp_ScopedHandle);
194
195 inline MojoResult Wait(const Handle& handle,
196 MojoHandleSignals signals,
197 MojoDeadline deadline) {
198 return MojoWait(handle.value(), signals, deadline);
199 }
200
201 // |HandleVectorType| and |FlagsVectorType| should be similar enough to
202 // |std::vector<Handle>| and |std::vector<MojoHandleSignals>|, respectively:
203 // - They should have a (const) |size()| method that returns an unsigned type.
204 // - They must provide contiguous storage, with access via (const) reference to
205 // that storage provided by a (const) |operator[]()| (by reference).
206 template <class HandleVectorType, class FlagsVectorType>
207 inline MojoResult WaitMany(const HandleVectorType& handles,
208 const FlagsVectorType& signals,
209 MojoDeadline deadline) {
210 if (signals.size() != handles.size())
211 return MOJO_RESULT_INVALID_ARGUMENT;
212 if (handles.size() > std::numeric_limits<uint32_t>::max())
213 return MOJO_RESULT_OUT_OF_RANGE;
214
215 if (handles.size() == 0)
216 return MojoWaitMany(NULL, NULL, 0, deadline);
217
218 const Handle& first_handle = handles[0];
219 const MojoHandleSignals& first_signals = signals[0];
220 return MojoWaitMany(
221 reinterpret_cast<const MojoHandle*>(&first_handle),
222 reinterpret_cast<const MojoHandleSignals*>(&first_signals),
223 static_cast<uint32_t>(handles.size()),
224 deadline);
225 }
226
227 // |Close()| takes ownership of the handle, since it'll invalidate it.
228 // Note: There's nothing to do, since the argument will be destroyed when it
229 // goes out of scope.
230 template <class HandleType>
231 inline void Close(ScopedHandleBase<HandleType> /*handle*/) {}
232
233 // Most users should typically use |Close()| (above) instead.
234 inline MojoResult CloseRaw(Handle handle) {
235 return MojoClose(handle.value());
236 }
237
238 // Strict weak ordering, so that |Handle|s can be used as keys in |std::map|s,
239 // etc.
240 inline bool operator<(const Handle& a, const Handle& b) {
241 return a.value() < b.value();
242 }
243
244 } // namespace mojo
245
246 #endif // MOJO_PUBLIC_CPP_SYSTEM_HANDLE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698