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