| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 // This file provides a C++ wrapping around the Mojo C API for data pipes, | |
| 6 // replacing the prefix of "Mojo" with a "mojo" namespace, and using more | |
| 7 // strongly-typed representations of |MojoHandle|s. | |
| 8 // | |
| 9 // Please see "mojo/public/c/include/mojo/system/data_pipe.h" for complete | |
| 10 // documentation of the API. | |
| 11 | |
| 12 #ifndef MOJO_PUBLIC_CPP_SYSTEM_DATA_PIPE_H_ | |
| 13 #define MOJO_PUBLIC_CPP_SYSTEM_DATA_PIPE_H_ | |
| 14 | |
| 15 #include <assert.h> | |
| 16 #include <mojo/system/data_pipe.h> | |
| 17 | |
| 18 #include "mojo/public/cpp/system/handle.h" | |
| 19 #include "mojo/public/cpp/system/macros.h" | |
| 20 | |
| 21 namespace mojo { | |
| 22 | |
| 23 // A strongly-typed representation of a |MojoHandle| to the producer end of a | |
| 24 // data pipe. | |
| 25 class DataPipeProducerHandle : public Handle { | |
| 26 public: | |
| 27 DataPipeProducerHandle() {} | |
| 28 explicit DataPipeProducerHandle(MojoHandle value) : Handle(value) {} | |
| 29 | |
| 30 // Copying and assignment allowed. | |
| 31 }; | |
| 32 | |
| 33 static_assert(sizeof(DataPipeProducerHandle) == sizeof(Handle), | |
| 34 "Bad size for C++ DataPipeProducerHandle"); | |
| 35 | |
| 36 typedef ScopedHandleBase<DataPipeProducerHandle> ScopedDataPipeProducerHandle; | |
| 37 static_assert(sizeof(ScopedDataPipeProducerHandle) == | |
| 38 sizeof(DataPipeProducerHandle), | |
| 39 "Bad size for C++ ScopedDataPipeProducerHandle"); | |
| 40 | |
| 41 // A strongly-typed representation of a |MojoHandle| to the consumer end of a | |
| 42 // data pipe. | |
| 43 class DataPipeConsumerHandle : public Handle { | |
| 44 public: | |
| 45 DataPipeConsumerHandle() {} | |
| 46 explicit DataPipeConsumerHandle(MojoHandle value) : Handle(value) {} | |
| 47 | |
| 48 // Copying and assignment allowed. | |
| 49 }; | |
| 50 | |
| 51 static_assert(sizeof(DataPipeConsumerHandle) == sizeof(Handle), | |
| 52 "Bad size for C++ DataPipeConsumerHandle"); | |
| 53 | |
| 54 typedef ScopedHandleBase<DataPipeConsumerHandle> ScopedDataPipeConsumerHandle; | |
| 55 static_assert(sizeof(ScopedDataPipeConsumerHandle) == | |
| 56 sizeof(DataPipeConsumerHandle), | |
| 57 "Bad size for C++ ScopedDataPipeConsumerHandle"); | |
| 58 | |
| 59 // Creates a new data pipe. See |MojoCreateDataPipe()| for complete | |
| 60 // documentation. | |
| 61 inline MojoResult CreateDataPipe( | |
| 62 const MojoCreateDataPipeOptions* options, | |
| 63 ScopedDataPipeProducerHandle* data_pipe_producer, | |
| 64 ScopedDataPipeConsumerHandle* data_pipe_consumer) { | |
| 65 assert(data_pipe_producer); | |
| 66 assert(data_pipe_consumer); | |
| 67 DataPipeProducerHandle producer_handle; | |
| 68 DataPipeConsumerHandle consumer_handle; | |
| 69 MojoResult rv = MojoCreateDataPipe(options, | |
| 70 producer_handle.mutable_value(), | |
| 71 consumer_handle.mutable_value()); | |
| 72 // Reset even on failure (reduces the chances that a "stale"/incorrect handle | |
| 73 // will be used). | |
| 74 data_pipe_producer->reset(producer_handle); | |
| 75 data_pipe_consumer->reset(consumer_handle); | |
| 76 return rv; | |
| 77 } | |
| 78 | |
| 79 // Writes to a data pipe. See |MojoWriteData| for complete documentation. | |
| 80 inline MojoResult WriteDataRaw(DataPipeProducerHandle data_pipe_producer, | |
| 81 const void* elements, | |
| 82 uint32_t* num_bytes, | |
| 83 MojoWriteDataFlags flags) { | |
| 84 return MojoWriteData(data_pipe_producer.value(), elements, num_bytes, flags); | |
| 85 } | |
| 86 | |
| 87 // Begins a two-phase write to a data pipe. See |MojoBeginWriteData()| for | |
| 88 // complete documentation. | |
| 89 inline MojoResult BeginWriteDataRaw(DataPipeProducerHandle data_pipe_producer, | |
| 90 void** buffer, | |
| 91 uint32_t* buffer_num_bytes, | |
| 92 MojoWriteDataFlags flags) { | |
| 93 return MojoBeginWriteData( | |
| 94 data_pipe_producer.value(), buffer, buffer_num_bytes, flags); | |
| 95 } | |
| 96 | |
| 97 // Completes a two-phase write to a data pipe. See |MojoEndWriteData()| for | |
| 98 // complete documentation. | |
| 99 inline MojoResult EndWriteDataRaw(DataPipeProducerHandle data_pipe_producer, | |
| 100 uint32_t num_bytes_written) { | |
| 101 return MojoEndWriteData(data_pipe_producer.value(), num_bytes_written); | |
| 102 } | |
| 103 | |
| 104 // Sets data pipe consumer options to their defaults. See | |
| 105 // |MojoGetDataPipeConsumerOptions()| for complete documentation. | |
| 106 inline MojoResult SetDataPipeConsumerOptionsToDefault( | |
| 107 DataPipeConsumerHandle data_pipe_consumer) { | |
| 108 return MojoSetDataPipeConsumerOptions(data_pipe_consumer.value(), nullptr); | |
| 109 } | |
| 110 | |
| 111 // Sets data pipe consumer options (in an "unwrapped" format). See | |
| 112 // |MojoGetDataPipeConsumerOptions()| for complete documentation. | |
| 113 inline MojoResult SetDataPipeConsumerOptions( | |
| 114 DataPipeConsumerHandle data_pipe_consumer, | |
| 115 uint32_t read_threshold_num_bytes) { | |
| 116 MojoDataPipeConsumerOptions options = { | |
| 117 static_cast<uint32_t>(sizeof(MojoDataPipeConsumerOptions)), | |
| 118 read_threshold_num_bytes}; | |
| 119 return MojoSetDataPipeConsumerOptions(data_pipe_consumer.value(), &options); | |
| 120 } | |
| 121 | |
| 122 // Gets data pipe consumer options (in an "unwrapped" format). See | |
| 123 // |MojoGetDataPipeConsumerOptions()| for complete documentation. | |
| 124 inline MojoResult GetDataPipeConsumerOptions( | |
| 125 DataPipeConsumerHandle data_pipe_consumer, | |
| 126 uint32_t* read_threshold_num_bytes) { | |
| 127 MojoDataPipeConsumerOptions options = {}; | |
| 128 MojoResult rv = | |
| 129 MojoGetDataPipeConsumerOptions(data_pipe_consumer.value(), &options, | |
| 130 static_cast<uint32_t>(sizeof(options))); | |
| 131 if (rv == MOJO_RESULT_OK) { | |
| 132 // No need to check |struct_size|, since all versions of the struct has this | |
| 133 // field. | |
| 134 *read_threshold_num_bytes = options.read_threshold_num_bytes; | |
| 135 } | |
| 136 return rv; | |
| 137 } | |
| 138 | |
| 139 // Reads from a data pipe. See |MojoReadData()| for complete documentation. | |
| 140 inline MojoResult ReadDataRaw(DataPipeConsumerHandle data_pipe_consumer, | |
| 141 void* elements, | |
| 142 uint32_t* num_bytes, | |
| 143 MojoReadDataFlags flags) { | |
| 144 return MojoReadData(data_pipe_consumer.value(), elements, num_bytes, flags); | |
| 145 } | |
| 146 | |
| 147 // Begins a two-phase read from a data pipe. See |MojoBeginReadData()| for | |
| 148 // complete documentation. | |
| 149 inline MojoResult BeginReadDataRaw(DataPipeConsumerHandle data_pipe_consumer, | |
| 150 const void** buffer, | |
| 151 uint32_t* buffer_num_bytes, | |
| 152 MojoReadDataFlags flags) { | |
| 153 return MojoBeginReadData( | |
| 154 data_pipe_consumer.value(), buffer, buffer_num_bytes, flags); | |
| 155 } | |
| 156 | |
| 157 // Completes a two-phase read from a data pipe. See |MojoEndReadData()| for | |
| 158 // complete documentation. | |
| 159 inline MojoResult EndReadDataRaw(DataPipeConsumerHandle data_pipe_consumer, | |
| 160 uint32_t num_bytes_read) { | |
| 161 return MojoEndReadData(data_pipe_consumer.value(), num_bytes_read); | |
| 162 } | |
| 163 | |
| 164 // A wrapper class that automatically creates a data pipe and owns both handles. | |
| 165 // TODO(vtl): Make an even more friendly version? (Maybe templatized for a | |
| 166 // particular type instead of some "element"? Maybe functions that take | |
| 167 // vectors?) | |
| 168 class DataPipe { | |
| 169 public: | |
| 170 DataPipe(); | |
| 171 explicit DataPipe(const MojoCreateDataPipeOptions& options); | |
| 172 ~DataPipe(); | |
| 173 | |
| 174 ScopedDataPipeProducerHandle producer_handle; | |
| 175 ScopedDataPipeConsumerHandle consumer_handle; | |
| 176 }; | |
| 177 | |
| 178 inline DataPipe::DataPipe() { | |
| 179 MojoResult result = | |
| 180 CreateDataPipe(nullptr, &producer_handle, &consumer_handle); | |
| 181 MOJO_ALLOW_UNUSED_LOCAL(result); | |
| 182 assert(result == MOJO_RESULT_OK); | |
| 183 } | |
| 184 | |
| 185 inline DataPipe::DataPipe(const MojoCreateDataPipeOptions& options) { | |
| 186 MojoResult result = | |
| 187 CreateDataPipe(&options, &producer_handle, &consumer_handle); | |
| 188 MOJO_ALLOW_UNUSED_LOCAL(result); | |
| 189 assert(result == MOJO_RESULT_OK); | |
| 190 } | |
| 191 | |
| 192 inline DataPipe::~DataPipe() { | |
| 193 } | |
| 194 | |
| 195 } // namespace mojo | |
| 196 | |
| 197 #endif // MOJO_PUBLIC_CPP_SYSTEM_DATA_PIPE_H_ | |
| OLD | NEW |