| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 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/system/data_pipe.h" for complete documentation of |
| 10 // the API. |
| 11 |
| 5 #ifndef MOJO_PUBLIC_CPP_SYSTEM_DATA_PIPE_H_ | 12 #ifndef MOJO_PUBLIC_CPP_SYSTEM_DATA_PIPE_H_ |
| 6 #define MOJO_PUBLIC_CPP_SYSTEM_DATA_PIPE_H_ | 13 #define MOJO_PUBLIC_CPP_SYSTEM_DATA_PIPE_H_ |
| 7 | 14 |
| 8 #include <assert.h> | 15 #include <assert.h> |
| 9 | 16 |
| 10 #include "mojo/public/c/system/data_pipe.h" | 17 #include "mojo/public/c/system/data_pipe.h" |
| 11 #include "mojo/public/cpp/system/handle.h" | 18 #include "mojo/public/cpp/system/handle.h" |
| 12 #include "mojo/public/cpp/system/macros.h" | 19 #include "mojo/public/cpp/system/macros.h" |
| 13 | 20 |
| 14 namespace mojo { | 21 namespace mojo { |
| 15 | 22 |
| 16 // DataPipeProducerHandle and DataPipeConsumerHandle --------------------------- | 23 // A strongly-typed representation of a |MojoHandle| to the producer end of a |
| 17 | 24 // data pipe. |
| 18 class DataPipeProducerHandle : public Handle { | 25 class DataPipeProducerHandle : public Handle { |
| 19 public: | 26 public: |
| 20 DataPipeProducerHandle() {} | 27 DataPipeProducerHandle() {} |
| 21 explicit DataPipeProducerHandle(MojoHandle value) : Handle(value) {} | 28 explicit DataPipeProducerHandle(MojoHandle value) : Handle(value) {} |
| 22 | 29 |
| 23 // Copying and assignment allowed. | 30 // Copying and assignment allowed. |
| 24 }; | 31 }; |
| 25 | 32 |
| 26 static_assert(sizeof(DataPipeProducerHandle) == sizeof(Handle), | 33 static_assert(sizeof(DataPipeProducerHandle) == sizeof(Handle), |
| 27 "Bad size for C++ DataPipeProducerHandle"); | 34 "Bad size for C++ DataPipeProducerHandle"); |
| 28 | 35 |
| 29 typedef ScopedHandleBase<DataPipeProducerHandle> ScopedDataPipeProducerHandle; | 36 typedef ScopedHandleBase<DataPipeProducerHandle> ScopedDataPipeProducerHandle; |
| 30 static_assert(sizeof(ScopedDataPipeProducerHandle) == | 37 static_assert(sizeof(ScopedDataPipeProducerHandle) == |
| 31 sizeof(DataPipeProducerHandle), | 38 sizeof(DataPipeProducerHandle), |
| 32 "Bad size for C++ ScopedDataPipeProducerHandle"); | 39 "Bad size for C++ ScopedDataPipeProducerHandle"); |
| 33 | 40 |
| 41 // A strongly-typed representation of a |MojoHandle| to the consumer end of a |
| 42 // data pipe. |
| 34 class DataPipeConsumerHandle : public Handle { | 43 class DataPipeConsumerHandle : public Handle { |
| 35 public: | 44 public: |
| 36 DataPipeConsumerHandle() {} | 45 DataPipeConsumerHandle() {} |
| 37 explicit DataPipeConsumerHandle(MojoHandle value) : Handle(value) {} | 46 explicit DataPipeConsumerHandle(MojoHandle value) : Handle(value) {} |
| 38 | 47 |
| 39 // Copying and assignment allowed. | 48 // Copying and assignment allowed. |
| 40 }; | 49 }; |
| 41 | 50 |
| 42 static_assert(sizeof(DataPipeConsumerHandle) == sizeof(Handle), | 51 static_assert(sizeof(DataPipeConsumerHandle) == sizeof(Handle), |
| 43 "Bad size for C++ DataPipeConsumerHandle"); | 52 "Bad size for C++ DataPipeConsumerHandle"); |
| 44 | 53 |
| 45 typedef ScopedHandleBase<DataPipeConsumerHandle> ScopedDataPipeConsumerHandle; | 54 typedef ScopedHandleBase<DataPipeConsumerHandle> ScopedDataPipeConsumerHandle; |
| 46 static_assert(sizeof(ScopedDataPipeConsumerHandle) == | 55 static_assert(sizeof(ScopedDataPipeConsumerHandle) == |
| 47 sizeof(DataPipeConsumerHandle), | 56 sizeof(DataPipeConsumerHandle), |
| 48 "Bad size for C++ ScopedDataPipeConsumerHandle"); | 57 "Bad size for C++ ScopedDataPipeConsumerHandle"); |
| 49 | 58 |
| 59 // Creates a new data pipe. See |MojoCreateDataPipe()| for complete |
| 60 // documentation. |
| 50 inline MojoResult CreateDataPipe( | 61 inline MojoResult CreateDataPipe( |
| 51 const MojoCreateDataPipeOptions* options, | 62 const MojoCreateDataPipeOptions* options, |
| 52 ScopedDataPipeProducerHandle* data_pipe_producer, | 63 ScopedDataPipeProducerHandle* data_pipe_producer, |
| 53 ScopedDataPipeConsumerHandle* data_pipe_consumer) { | 64 ScopedDataPipeConsumerHandle* data_pipe_consumer) { |
| 54 assert(data_pipe_producer); | 65 assert(data_pipe_producer); |
| 55 assert(data_pipe_consumer); | 66 assert(data_pipe_consumer); |
| 56 DataPipeProducerHandle producer_handle; | 67 DataPipeProducerHandle producer_handle; |
| 57 DataPipeConsumerHandle consumer_handle; | 68 DataPipeConsumerHandle consumer_handle; |
| 58 MojoResult rv = MojoCreateDataPipe(options, | 69 MojoResult rv = MojoCreateDataPipe(options, |
| 59 producer_handle.mutable_value(), | 70 producer_handle.mutable_value(), |
| 60 consumer_handle.mutable_value()); | 71 consumer_handle.mutable_value()); |
| 61 // Reset even on failure (reduces the chances that a "stale"/incorrect handle | 72 // Reset even on failure (reduces the chances that a "stale"/incorrect handle |
| 62 // will be used). | 73 // will be used). |
| 63 data_pipe_producer->reset(producer_handle); | 74 data_pipe_producer->reset(producer_handle); |
| 64 data_pipe_consumer->reset(consumer_handle); | 75 data_pipe_consumer->reset(consumer_handle); |
| 65 return rv; | 76 return rv; |
| 66 } | 77 } |
| 67 | 78 |
| 79 // Writes to a data pipe. See |MojoWriteData| for complete documentation. |
| 68 inline MojoResult WriteDataRaw(DataPipeProducerHandle data_pipe_producer, | 80 inline MojoResult WriteDataRaw(DataPipeProducerHandle data_pipe_producer, |
| 69 const void* elements, | 81 const void* elements, |
| 70 uint32_t* num_bytes, | 82 uint32_t* num_bytes, |
| 71 MojoWriteDataFlags flags) { | 83 MojoWriteDataFlags flags) { |
| 72 return MojoWriteData(data_pipe_producer.value(), elements, num_bytes, flags); | 84 return MojoWriteData(data_pipe_producer.value(), elements, num_bytes, flags); |
| 73 } | 85 } |
| 74 | 86 |
| 87 // Begins a two-phase write to a data pipe. See |MojoBeginWriteData()| for |
| 88 // complete documentation. |
| 75 inline MojoResult BeginWriteDataRaw(DataPipeProducerHandle data_pipe_producer, | 89 inline MojoResult BeginWriteDataRaw(DataPipeProducerHandle data_pipe_producer, |
| 76 void** buffer, | 90 void** buffer, |
| 77 uint32_t* buffer_num_bytes, | 91 uint32_t* buffer_num_bytes, |
| 78 MojoWriteDataFlags flags) { | 92 MojoWriteDataFlags flags) { |
| 79 return MojoBeginWriteData( | 93 return MojoBeginWriteData( |
| 80 data_pipe_producer.value(), buffer, buffer_num_bytes, flags); | 94 data_pipe_producer.value(), buffer, buffer_num_bytes, flags); |
| 81 } | 95 } |
| 82 | 96 |
| 97 // Completes a two-phase write to a data pipe. See |MojoEndWriteData()| for |
| 98 // complete documentation. |
| 83 inline MojoResult EndWriteDataRaw(DataPipeProducerHandle data_pipe_producer, | 99 inline MojoResult EndWriteDataRaw(DataPipeProducerHandle data_pipe_producer, |
| 84 uint32_t num_bytes_written) { | 100 uint32_t num_bytes_written) { |
| 85 return MojoEndWriteData(data_pipe_producer.value(), num_bytes_written); | 101 return MojoEndWriteData(data_pipe_producer.value(), num_bytes_written); |
| 86 } | 102 } |
| 87 | 103 |
| 104 // Reads from a data pipe. See |MojoReadData()| for complete documentation. |
| 88 inline MojoResult ReadDataRaw(DataPipeConsumerHandle data_pipe_consumer, | 105 inline MojoResult ReadDataRaw(DataPipeConsumerHandle data_pipe_consumer, |
| 89 void* elements, | 106 void* elements, |
| 90 uint32_t* num_bytes, | 107 uint32_t* num_bytes, |
| 91 MojoReadDataFlags flags) { | 108 MojoReadDataFlags flags) { |
| 92 return MojoReadData(data_pipe_consumer.value(), elements, num_bytes, flags); | 109 return MojoReadData(data_pipe_consumer.value(), elements, num_bytes, flags); |
| 93 } | 110 } |
| 94 | 111 |
| 112 // Begins a two-phase read from a data pipe. See |MojoBeginReadData()| for |
| 113 // complete documentation. |
| 95 inline MojoResult BeginReadDataRaw(DataPipeConsumerHandle data_pipe_consumer, | 114 inline MojoResult BeginReadDataRaw(DataPipeConsumerHandle data_pipe_consumer, |
| 96 const void** buffer, | 115 const void** buffer, |
| 97 uint32_t* buffer_num_bytes, | 116 uint32_t* buffer_num_bytes, |
| 98 MojoReadDataFlags flags) { | 117 MojoReadDataFlags flags) { |
| 99 return MojoBeginReadData( | 118 return MojoBeginReadData( |
| 100 data_pipe_consumer.value(), buffer, buffer_num_bytes, flags); | 119 data_pipe_consumer.value(), buffer, buffer_num_bytes, flags); |
| 101 } | 120 } |
| 102 | 121 |
| 122 // Completes a two-phase read from a data pipe. See |MojoEndReadData()| for |
| 123 // complete documentation. |
| 103 inline MojoResult EndReadDataRaw(DataPipeConsumerHandle data_pipe_consumer, | 124 inline MojoResult EndReadDataRaw(DataPipeConsumerHandle data_pipe_consumer, |
| 104 uint32_t num_bytes_read) { | 125 uint32_t num_bytes_read) { |
| 105 return MojoEndReadData(data_pipe_consumer.value(), num_bytes_read); | 126 return MojoEndReadData(data_pipe_consumer.value(), num_bytes_read); |
| 106 } | 127 } |
| 107 | 128 |
| 108 // A wrapper class that automatically creates a data pipe and owns both handles. | 129 // A wrapper class that automatically creates a data pipe and owns both handles. |
| 109 // TODO(vtl): Make an even more friendly version? (Maybe templatized for a | 130 // TODO(vtl): Make an even more friendly version? (Maybe templatized for a |
| 110 // particular type instead of some "element"? Maybe functions that take | 131 // particular type instead of some "element"? Maybe functions that take |
| 111 // vectors?) | 132 // vectors?) |
| 112 class DataPipe { | 133 class DataPipe { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 132 MOJO_ALLOW_UNUSED_LOCAL(result); | 153 MOJO_ALLOW_UNUSED_LOCAL(result); |
| 133 assert(result == MOJO_RESULT_OK); | 154 assert(result == MOJO_RESULT_OK); |
| 134 } | 155 } |
| 135 | 156 |
| 136 inline DataPipe::~DataPipe() { | 157 inline DataPipe::~DataPipe() { |
| 137 } | 158 } |
| 138 | 159 |
| 139 } // namespace mojo | 160 } // namespace mojo |
| 140 | 161 |
| 141 #endif // MOJO_PUBLIC_CPP_SYSTEM_DATA_PIPE_H_ | 162 #endif // MOJO_PUBLIC_CPP_SYSTEM_DATA_PIPE_H_ |
| OLD | NEW |