1 // Copyright 2014 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_CPP_SYSTEM_DATA_PIPE_H_
6 #define MOJO_PUBLIC_CPP_SYSTEM_DATA_PIPE_H_
10 #include "mojo/public/c/system/data_pipe.h"
11 #include "mojo/public/cpp/system/handle.h"
12 #include "mojo/public/cpp/system/macros.h"
16 // DataPipeProducerHandle and DataPipeConsumerHandle ---------------------------
18 class DataPipeProducerHandle
: public Handle
{
20 DataPipeProducerHandle() {}
21 explicit DataPipeProducerHandle(MojoHandle value
) : Handle(value
) {}
23 // Copying and assignment allowed.
26 MOJO_COMPILE_ASSERT(sizeof(DataPipeProducerHandle
) == sizeof(Handle
),
27 bad_size_for_cpp_DataPipeProducerHandle
);
29 typedef ScopedHandleBase
<DataPipeProducerHandle
> ScopedDataPipeProducerHandle
;
30 MOJO_COMPILE_ASSERT(sizeof(ScopedDataPipeProducerHandle
) ==
31 sizeof(DataPipeProducerHandle
),
32 bad_size_for_cpp_ScopedDataPipeProducerHandle
);
34 class DataPipeConsumerHandle
: public Handle
{
36 DataPipeConsumerHandle() {}
37 explicit DataPipeConsumerHandle(MojoHandle value
) : Handle(value
) {}
39 // Copying and assignment allowed.
42 MOJO_COMPILE_ASSERT(sizeof(DataPipeConsumerHandle
) == sizeof(Handle
),
43 bad_size_for_cpp_DataPipeConsumerHandle
);
45 typedef ScopedHandleBase
<DataPipeConsumerHandle
> ScopedDataPipeConsumerHandle
;
46 MOJO_COMPILE_ASSERT(sizeof(ScopedDataPipeConsumerHandle
) ==
47 sizeof(DataPipeConsumerHandle
),
48 bad_size_for_cpp_ScopedDataPipeConsumerHandle
);
50 inline MojoResult
CreateDataPipe(
51 const MojoCreateDataPipeOptions
* options
,
52 ScopedDataPipeProducerHandle
* data_pipe_producer
,
53 ScopedDataPipeConsumerHandle
* data_pipe_consumer
) {
54 assert(data_pipe_producer
);
55 assert(data_pipe_consumer
);
56 DataPipeProducerHandle producer_handle
;
57 DataPipeConsumerHandle consumer_handle
;
58 MojoResult rv
= MojoCreateDataPipe(options
, producer_handle
.mutable_value(),
59 consumer_handle
.mutable_value());
60 // Reset even on failure (reduces the chances that a "stale"/incorrect handle
62 data_pipe_producer
->reset(producer_handle
);
63 data_pipe_consumer
->reset(consumer_handle
);
67 inline MojoResult
WriteDataRaw(DataPipeProducerHandle data_pipe_producer
,
70 MojoWriteDataFlags flags
) {
71 return MojoWriteData(data_pipe_producer
.value(), elements
, num_bytes
, flags
);
74 inline MojoResult
BeginWriteDataRaw(DataPipeProducerHandle data_pipe_producer
,
76 uint32_t* buffer_num_bytes
,
77 MojoWriteDataFlags flags
) {
78 return MojoBeginWriteData(data_pipe_producer
.value(), buffer
,
79 buffer_num_bytes
, flags
);
82 inline MojoResult
EndWriteDataRaw(DataPipeProducerHandle data_pipe_producer
,
83 uint32_t num_bytes_written
) {
84 return MojoEndWriteData(data_pipe_producer
.value(), num_bytes_written
);
87 inline MojoResult
ReadDataRaw(DataPipeConsumerHandle data_pipe_consumer
,
90 MojoReadDataFlags flags
) {
91 return MojoReadData(data_pipe_consumer
.value(), elements
, num_bytes
, flags
);
94 inline MojoResult
BeginReadDataRaw(DataPipeConsumerHandle data_pipe_consumer
,
96 uint32_t* buffer_num_bytes
,
97 MojoReadDataFlags flags
) {
98 return MojoBeginReadData(data_pipe_consumer
.value(), buffer
, buffer_num_bytes
,
102 inline MojoResult
EndReadDataRaw(DataPipeConsumerHandle data_pipe_consumer
,
103 uint32_t num_bytes_read
) {
104 return MojoEndReadData(data_pipe_consumer
.value(), num_bytes_read
);
107 // A wrapper class that automatically creates a data pipe and owns both handles.
108 // TODO(vtl): Make an even more friendly version? (Maybe templatized for a
109 // particular type instead of some "element"? Maybe functions that take
114 explicit DataPipe(const MojoCreateDataPipeOptions
& options
);
117 ScopedDataPipeProducerHandle producer_handle
;
118 ScopedDataPipeConsumerHandle consumer_handle
;
121 inline DataPipe::DataPipe() {
122 MojoResult result MOJO_ALLOW_UNUSED
=
123 CreateDataPipe(NULL
, &producer_handle
, &consumer_handle
);
124 assert(result
== MOJO_RESULT_OK
);
127 inline DataPipe::DataPipe(const MojoCreateDataPipeOptions
& options
) {
128 MojoResult result MOJO_ALLOW_UNUSED
=
129 CreateDataPipe(&options
, &producer_handle
, &consumer_handle
);
130 assert(result
== MOJO_RESULT_OK
);
133 inline DataPipe::~DataPipe() {
138 #endif // MOJO_PUBLIC_CPP_SYSTEM_DATA_PIPE_H_