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_SYSTEM_CORE_CPP_H_
6 #define MOJO_PUBLIC_SYSTEM_CORE_CPP_H_
13 #include "mojo/public/system/core.h"
14 #include "mojo/public/system/macros.h"
15 #include "mojo/public/system/system_export.h"
19 // Standalone functions --------------------------------------------------------
21 inline MojoTimeTicks GetTimeTicksNow() {
22 return MojoGetTimeTicksNow();
25 // ScopedHandleBase ------------------------------------------------------------
27 // Scoper for the actual handle types defined further below. It's move-only,
28 // like the C++11 |unique_ptr|.
29 template <class HandleType>
30 class ScopedHandleBase {
31 MOJO_MOVE_ONLY_TYPE_FOR_CPP_03(ScopedHandleBase, RValue);
35 explicit ScopedHandleBase(HandleType handle) : handle_(handle) {}
36 ~ScopedHandleBase() { CloseIfNecessary(); }
38 template <class CompatibleHandleType>
39 explicit ScopedHandleBase(ScopedHandleBase<CompatibleHandleType> other)
40 : handle_(other.release()) {
43 // Move-only constructor and operator=.
44 ScopedHandleBase(RValue other) : handle_(other.object->release()) {}
45 ScopedHandleBase& operator=(RValue other) {
46 handle_ = other.object->release();
50 const HandleType& get() const { return handle_; }
52 void swap(ScopedHandleBase& other) {
53 handle_.swap(other.handle_);
56 HandleType release() MOJO_WARN_UNUSED_RESULT {
62 void reset(HandleType handle = HandleType()) {
67 bool is_valid() const {
68 return handle_.is_valid();
72 void CloseIfNecessary() {
73 if (!handle_.is_valid())
75 MojoResult result MOJO_ALLOW_UNUSED = MojoClose(handle_.value());
76 assert(result == MOJO_RESULT_OK);
82 template <typename HandleType>
83 inline ScopedHandleBase<HandleType> MakeScopedHandle(HandleType handle) {
84 return ScopedHandleBase<HandleType>(handle);
87 // Handle ----------------------------------------------------------------------
89 const MojoHandle kInvalidHandleValue = MOJO_HANDLE_INVALID;
91 // Wrapper base class for |MojoHandle|.
94 Handle() : value_(MOJO_HANDLE_INVALID) {}
95 explicit Handle(MojoHandle value) : value_(value) {}
98 void swap(Handle& other) {
99 MojoHandle temp = value_;
100 value_ = other.value_;
104 bool is_valid() const {
105 return value_ != MOJO_HANDLE_INVALID;
108 MojoHandle value() const { return value_; }
109 MojoHandle* mutable_value() { return &value_; }
110 void set_value(MojoHandle value) { value_ = value; }
115 // Copying and assignment allowed.
118 // Should have zero overhead.
119 MOJO_COMPILE_ASSERT(sizeof(Handle) == sizeof(MojoHandle),
120 bad_size_for_cpp_Handle);
122 // The scoper should also impose no more overhead.
123 typedef ScopedHandleBase<Handle> ScopedHandle;
124 MOJO_COMPILE_ASSERT(sizeof(ScopedHandle) == sizeof(Handle),
125 bad_size_for_cpp_ScopedHandle);
127 inline MojoResult Wait(const Handle& handle,
129 MojoDeadline deadline) {
130 return MojoWait(handle.value(), flags, deadline);
133 // |HandleVectorType| and |FlagsVectorType| should be similar enough to
134 // |std::vector<Handle>| and |std::vector<MojoWaitFlags>|, respectively:
135 // - They should have a (const) |size()| method that returns an unsigned type.
136 // - They must provide contiguous storage, with access via (const) reference to
137 // that storage provided by a (const) |operator[]()| (by reference).
138 template <class HandleVectorType, class FlagsVectorType>
139 inline MojoResult WaitMany(const HandleVectorType& handles,
140 const FlagsVectorType& flags,
141 MojoDeadline deadline) {
142 if (flags.size() != handles.size())
143 return MOJO_RESULT_INVALID_ARGUMENT;
144 if (handles.size() > std::numeric_limits<uint32_t>::max())
145 return MOJO_RESULT_OUT_OF_RANGE;
147 if (handles.size() == 0)
148 return MojoWaitMany(NULL, NULL, 0, deadline);
150 const Handle& first_handle = handles[0];
151 const MojoWaitFlags& first_flag = flags[0];
152 return MojoWaitMany(reinterpret_cast<const MojoHandle*>(&first_handle),
153 reinterpret_cast<const MojoWaitFlags*>(&first_flag),
154 static_cast<uint32_t>(handles.size()),
158 // |Close()| takes ownership of the handle, since it'll invalidate it.
159 // Note: There's nothing to do, since the argument will be destroyed when it
160 // goes out of scope.
161 template <class HandleType>
162 inline void Close(ScopedHandleBase<HandleType> /*handle*/) {}
164 // Most users should typically use |Close()| (above) instead.
165 inline MojoResult CloseRaw(Handle handle) {
166 return MojoClose(handle.value());
169 // Strict weak ordering, so that |Handle|s can be used as keys in |std::map|s,
171 inline bool operator<(const Handle& a, const Handle& b) {
172 return a.value() < b.value();
175 // MessagePipeHandle -----------------------------------------------------------
177 class MessagePipeHandle : public Handle {
179 MessagePipeHandle() {}
180 explicit MessagePipeHandle(MojoHandle value) : Handle(value) {}
182 // Copying and assignment allowed.
185 MOJO_COMPILE_ASSERT(sizeof(MessagePipeHandle) == sizeof(Handle),
186 bad_size_for_cpp_MessagePipeHandle);
188 typedef ScopedHandleBase<MessagePipeHandle> ScopedMessagePipeHandle;
189 MOJO_COMPILE_ASSERT(sizeof(ScopedMessagePipeHandle) ==
190 sizeof(MessagePipeHandle),
191 bad_size_for_cpp_ScopedMessagePipeHandle);
193 inline MojoResult CreateMessagePipe(ScopedMessagePipeHandle* message_pipe0,
194 ScopedMessagePipeHandle* message_pipe1) {
195 assert(message_pipe0);
196 assert(message_pipe1);
197 MessagePipeHandle handle0;
198 MessagePipeHandle handle1;
199 MojoResult rv = MojoCreateMessagePipe(handle0.mutable_value(),
200 handle1.mutable_value());
201 // Reset even on failure (reduces the chances that a "stale"/incorrect handle
203 message_pipe0->reset(handle0);
204 message_pipe1->reset(handle1);
208 // These "raw" versions fully expose the underlying API, but don't help with
209 // ownership of handles (especially when writing messages).
210 // TODO(vtl): Write "baked" versions.
211 inline MojoResult WriteMessageRaw(MessagePipeHandle message_pipe,
214 const MojoHandle* handles,
215 uint32_t num_handles,
216 MojoWriteMessageFlags flags) {
217 return MojoWriteMessage(message_pipe.value(), bytes, num_bytes, handles,
221 inline MojoResult ReadMessageRaw(MessagePipeHandle message_pipe,
225 uint32_t* num_handles,
226 MojoReadMessageFlags flags) {
227 return MojoReadMessage(message_pipe.value(), bytes, num_bytes, handles,
231 // A wrapper class that automatically creates a message pipe and owns both
238 ScopedMessagePipeHandle handle0;
239 ScopedMessagePipeHandle handle1;
242 inline MessagePipe::MessagePipe() {
243 MojoResult result MOJO_ALLOW_UNUSED = CreateMessagePipe(&handle0, &handle1);
244 assert(result == MOJO_RESULT_OK);
247 inline MessagePipe::~MessagePipe() {
250 // DataPipeProducerHandle and DataPipeConsumerHandle ---------------------------
252 class DataPipeProducerHandle : public Handle {
254 DataPipeProducerHandle() {}
255 explicit DataPipeProducerHandle(MojoHandle value) : Handle(value) {}
257 // Copying and assignment allowed.
260 MOJO_COMPILE_ASSERT(sizeof(DataPipeProducerHandle) == sizeof(Handle),
261 bad_size_for_cpp_DataPipeProducerHandle);
263 typedef ScopedHandleBase<DataPipeProducerHandle> ScopedDataPipeProducerHandle;
264 MOJO_COMPILE_ASSERT(sizeof(ScopedDataPipeProducerHandle) ==
265 sizeof(DataPipeProducerHandle),
266 bad_size_for_cpp_ScopedDataPipeProducerHandle);
268 class DataPipeConsumerHandle : public Handle {
270 DataPipeConsumerHandle() {}
271 explicit DataPipeConsumerHandle(MojoHandle value) : Handle(value) {}
273 // Copying and assignment allowed.
276 MOJO_COMPILE_ASSERT(sizeof(DataPipeConsumerHandle) == sizeof(Handle),
277 bad_size_for_cpp_DataPipeConsumerHandle);
279 typedef ScopedHandleBase<DataPipeConsumerHandle> ScopedDataPipeConsumerHandle;
280 MOJO_COMPILE_ASSERT(sizeof(ScopedDataPipeConsumerHandle) ==
281 sizeof(DataPipeConsumerHandle),
282 bad_size_for_cpp_ScopedDataPipeConsumerHandle);
284 inline MojoResult CreateDataPipe(
285 const MojoCreateDataPipeOptions* options,
286 ScopedDataPipeProducerHandle* data_pipe_producer,
287 ScopedDataPipeConsumerHandle* data_pipe_consumer) {
288 assert(data_pipe_producer);
289 assert(data_pipe_consumer);
290 DataPipeProducerHandle producer_handle;
291 DataPipeConsumerHandle consumer_handle;
292 MojoResult rv = MojoCreateDataPipe(options,
293 producer_handle.mutable_value(),
294 consumer_handle.mutable_value());
295 // Reset even on failure (reduces the chances that a "stale"/incorrect handle
297 data_pipe_producer->reset(producer_handle);
298 data_pipe_consumer->reset(consumer_handle);
302 inline MojoResult WriteDataRaw(DataPipeProducerHandle data_pipe_producer,
303 const void* elements,
305 MojoWriteDataFlags flags) {
306 return MojoWriteData(data_pipe_producer.value(), elements, num_bytes, flags);
309 inline MojoResult BeginWriteDataRaw(DataPipeProducerHandle data_pipe_producer,
311 uint32_t* buffer_num_bytes,
312 MojoWriteDataFlags flags) {
313 return MojoBeginWriteData(data_pipe_producer.value(), buffer,
314 buffer_num_bytes, flags);
317 inline MojoResult EndWriteDataRaw(DataPipeProducerHandle data_pipe_producer,
318 uint32_t num_bytes_written) {
319 return MojoEndWriteData(data_pipe_producer.value(), num_bytes_written);
322 inline MojoResult ReadDataRaw(DataPipeConsumerHandle data_pipe_consumer,
325 MojoReadDataFlags flags) {
326 return MojoReadData(data_pipe_consumer.value(), elements, num_bytes, flags);
329 inline MojoResult BeginReadDataRaw(DataPipeConsumerHandle data_pipe_consumer,
331 uint32_t* buffer_num_bytes,
332 MojoReadDataFlags flags) {
333 return MojoBeginReadData(data_pipe_consumer.value(), buffer, buffer_num_bytes,
337 inline MojoResult EndReadDataRaw(DataPipeConsumerHandle data_pipe_consumer,
338 uint32_t num_bytes_read) {
339 return MojoEndReadData(data_pipe_consumer.value(), num_bytes_read);
342 // A wrapper class that automatically creates a data pipe and owns both handles.
343 // TODO(vtl): Make an even more friendly version? (Maybe templatized for a
344 // particular type instead of some "element"? Maybe functions that take
349 explicit DataPipe(const MojoCreateDataPipeOptions& options);
352 ScopedDataPipeProducerHandle producer_handle;
353 ScopedDataPipeConsumerHandle consumer_handle;
356 inline DataPipe::DataPipe() {
357 MojoResult result MOJO_ALLOW_UNUSED =
358 CreateDataPipe(NULL, &producer_handle, &consumer_handle);
359 assert(result == MOJO_RESULT_OK);
362 inline DataPipe::DataPipe(const MojoCreateDataPipeOptions& options) {
363 MojoResult result MOJO_ALLOW_UNUSED =
364 CreateDataPipe(&options, &producer_handle, &consumer_handle);
365 assert(result == MOJO_RESULT_OK);
368 inline DataPipe::~DataPipe() {
373 #endif // MOJO_PUBLIC_SYSTEM_CORE_CPP_H_