| OLD | NEW |
| (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_DATA_PIPE_H_ |
| 6 #define MOJO_PUBLIC_CPP_SYSTEM_DATA_PIPE_H_ |
| 7 |
| 8 #include "mojo/public/c/system/data_pipe.h" |
| 9 #include "mojo/public/cpp/system/handle.h" |
| 10 |
| 11 namespace mojo { |
| 12 |
| 13 // DataPipeProducerHandle and DataPipeConsumerHandle --------------------------- |
| 14 |
| 15 class DataPipeProducerHandle : public Handle { |
| 16 public: |
| 17 DataPipeProducerHandle() {} |
| 18 explicit DataPipeProducerHandle(MojoHandle value) : Handle(value) {} |
| 19 |
| 20 // Copying and assignment allowed. |
| 21 }; |
| 22 |
| 23 MOJO_COMPILE_ASSERT(sizeof(DataPipeProducerHandle) == sizeof(Handle), |
| 24 bad_size_for_cpp_DataPipeProducerHandle); |
| 25 |
| 26 typedef ScopedHandleBase<DataPipeProducerHandle> ScopedDataPipeProducerHandle; |
| 27 MOJO_COMPILE_ASSERT(sizeof(ScopedDataPipeProducerHandle) == |
| 28 sizeof(DataPipeProducerHandle), |
| 29 bad_size_for_cpp_ScopedDataPipeProducerHandle); |
| 30 |
| 31 class DataPipeConsumerHandle : public Handle { |
| 32 public: |
| 33 DataPipeConsumerHandle() {} |
| 34 explicit DataPipeConsumerHandle(MojoHandle value) : Handle(value) {} |
| 35 |
| 36 // Copying and assignment allowed. |
| 37 }; |
| 38 |
| 39 MOJO_COMPILE_ASSERT(sizeof(DataPipeConsumerHandle) == sizeof(Handle), |
| 40 bad_size_for_cpp_DataPipeConsumerHandle); |
| 41 |
| 42 typedef ScopedHandleBase<DataPipeConsumerHandle> ScopedDataPipeConsumerHandle; |
| 43 MOJO_COMPILE_ASSERT(sizeof(ScopedDataPipeConsumerHandle) == |
| 44 sizeof(DataPipeConsumerHandle), |
| 45 bad_size_for_cpp_ScopedDataPipeConsumerHandle); |
| 46 |
| 47 inline MojoResult CreateDataPipe( |
| 48 const MojoCreateDataPipeOptions* options, |
| 49 ScopedDataPipeProducerHandle* data_pipe_producer, |
| 50 ScopedDataPipeConsumerHandle* data_pipe_consumer) { |
| 51 assert(data_pipe_producer); |
| 52 assert(data_pipe_consumer); |
| 53 DataPipeProducerHandle producer_handle; |
| 54 DataPipeConsumerHandle consumer_handle; |
| 55 MojoResult rv = MojoCreateDataPipe(options, producer_handle.mutable_value(), |
| 56 consumer_handle.mutable_value()); |
| 57 // Reset even on failure (reduces the chances that a "stale"/incorrect handle |
| 58 // will be used). |
| 59 data_pipe_producer->reset(producer_handle); |
| 60 data_pipe_consumer->reset(consumer_handle); |
| 61 return rv; |
| 62 } |
| 63 |
| 64 inline MojoResult WriteDataRaw(DataPipeProducerHandle data_pipe_producer, |
| 65 const void* elements, |
| 66 uint32_t* num_bytes, |
| 67 MojoWriteDataFlags flags) { |
| 68 return MojoWriteData(data_pipe_producer.value(), elements, num_bytes, flags); |
| 69 } |
| 70 |
| 71 inline MojoResult BeginWriteDataRaw(DataPipeProducerHandle data_pipe_producer, |
| 72 void** buffer, |
| 73 uint32_t* buffer_num_bytes, |
| 74 MojoWriteDataFlags flags) { |
| 75 return MojoBeginWriteData(data_pipe_producer.value(), buffer, |
| 76 buffer_num_bytes, flags); |
| 77 } |
| 78 |
| 79 inline MojoResult EndWriteDataRaw(DataPipeProducerHandle data_pipe_producer, |
| 80 uint32_t num_bytes_written) { |
| 81 return MojoEndWriteData(data_pipe_producer.value(), num_bytes_written); |
| 82 } |
| 83 |
| 84 inline MojoResult ReadDataRaw(DataPipeConsumerHandle data_pipe_consumer, |
| 85 void* elements, |
| 86 uint32_t* num_bytes, |
| 87 MojoReadDataFlags flags) { |
| 88 return MojoReadData(data_pipe_consumer.value(), elements, num_bytes, flags); |
| 89 } |
| 90 |
| 91 inline MojoResult BeginReadDataRaw(DataPipeConsumerHandle data_pipe_consumer, |
| 92 const void** buffer, |
| 93 uint32_t* buffer_num_bytes, |
| 94 MojoReadDataFlags flags) { |
| 95 return MojoBeginReadData(data_pipe_consumer.value(), buffer, buffer_num_bytes, |
| 96 flags); |
| 97 } |
| 98 |
| 99 inline MojoResult EndReadDataRaw(DataPipeConsumerHandle data_pipe_consumer, |
| 100 uint32_t num_bytes_read) { |
| 101 return MojoEndReadData(data_pipe_consumer.value(), num_bytes_read); |
| 102 } |
| 103 |
| 104 // A wrapper class that automatically creates a data pipe and owns both handles. |
| 105 // TODO(vtl): Make an even more friendly version? (Maybe templatized for a |
| 106 // particular type instead of some "element"? Maybe functions that take |
| 107 // vectors?) |
| 108 class DataPipe { |
| 109 public: |
| 110 DataPipe(); |
| 111 explicit DataPipe(const MojoCreateDataPipeOptions& options); |
| 112 ~DataPipe(); |
| 113 |
| 114 ScopedDataPipeProducerHandle producer_handle; |
| 115 ScopedDataPipeConsumerHandle consumer_handle; |
| 116 }; |
| 117 |
| 118 inline DataPipe::DataPipe() { |
| 119 MojoResult result MOJO_ALLOW_UNUSED = |
| 120 CreateDataPipe(NULL, &producer_handle, &consumer_handle); |
| 121 assert(result == MOJO_RESULT_OK); |
| 122 } |
| 123 |
| 124 inline DataPipe::DataPipe(const MojoCreateDataPipeOptions& options) { |
| 125 MojoResult result MOJO_ALLOW_UNUSED = |
| 126 CreateDataPipe(&options, &producer_handle, &consumer_handle); |
| 127 assert(result == MOJO_RESULT_OK); |
| 128 } |
| 129 |
| 130 inline DataPipe::~DataPipe() { |
| 131 } |
| 132 |
| 133 } // namespace mojo |
| 134 |
| 135 #endif // MOJO_PUBLIC_CPP_SYSTEM_DATA_PIPE_H_ |
| OLD | NEW |