OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
viettrungluu
2014/07/15 18:06:14
I guess new files should have "2014" as the date.
Hajime Morrita
2014/07/15 18:22:01
I need better calendar in my mind :-(
| |
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 MOJO_COMPILE_ASSERT(sizeof(DataPipeProducerHandle) == sizeof(Handle), | |
27 bad_size_for_cpp_DataPipeProducerHandle); | |
28 | |
29 typedef ScopedHandleBase<DataPipeProducerHandle> ScopedDataPipeProducerHandle; | |
30 MOJO_COMPILE_ASSERT(sizeof(ScopedDataPipeProducerHandle) == | |
31 sizeof(DataPipeProducerHandle), | |
32 bad_size_for_cpp_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 MOJO_COMPILE_ASSERT(sizeof(DataPipeConsumerHandle) == sizeof(Handle), | |
43 bad_size_for_cpp_DataPipeConsumerHandle); | |
44 | |
45 typedef ScopedHandleBase<DataPipeConsumerHandle> ScopedDataPipeConsumerHandle; | |
46 MOJO_COMPILE_ASSERT(sizeof(ScopedDataPipeConsumerHandle) == | |
47 sizeof(DataPipeConsumerHandle), | |
48 bad_size_for_cpp_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, producer_handle.mutable_value(), | |
59 consumer_handle.mutable_value()); | |
60 // Reset even on failure (reduces the chances that a "stale"/incorrect handle | |
61 // will be used). | |
62 data_pipe_producer->reset(producer_handle); | |
63 data_pipe_consumer->reset(consumer_handle); | |
64 return rv; | |
65 } | |
66 | |
67 inline MojoResult WriteDataRaw(DataPipeProducerHandle data_pipe_producer, | |
68 const void* elements, | |
69 uint32_t* num_bytes, | |
70 MojoWriteDataFlags flags) { | |
71 return MojoWriteData(data_pipe_producer.value(), elements, num_bytes, flags); | |
72 } | |
73 | |
74 inline MojoResult BeginWriteDataRaw(DataPipeProducerHandle data_pipe_producer, | |
75 void** buffer, | |
76 uint32_t* buffer_num_bytes, | |
77 MojoWriteDataFlags flags) { | |
78 return MojoBeginWriteData(data_pipe_producer.value(), buffer, | |
79 buffer_num_bytes, flags); | |
80 } | |
81 | |
82 inline MojoResult EndWriteDataRaw(DataPipeProducerHandle data_pipe_producer, | |
83 uint32_t num_bytes_written) { | |
84 return MojoEndWriteData(data_pipe_producer.value(), num_bytes_written); | |
85 } | |
86 | |
87 inline MojoResult ReadDataRaw(DataPipeConsumerHandle data_pipe_consumer, | |
88 void* elements, | |
89 uint32_t* num_bytes, | |
90 MojoReadDataFlags flags) { | |
91 return MojoReadData(data_pipe_consumer.value(), elements, num_bytes, flags); | |
92 } | |
93 | |
94 inline MojoResult BeginReadDataRaw(DataPipeConsumerHandle data_pipe_consumer, | |
95 const void** buffer, | |
96 uint32_t* buffer_num_bytes, | |
97 MojoReadDataFlags flags) { | |
98 return MojoBeginReadData(data_pipe_consumer.value(), buffer, buffer_num_bytes, | |
99 flags); | |
100 } | |
101 | |
102 inline MojoResult EndReadDataRaw(DataPipeConsumerHandle data_pipe_consumer, | |
103 uint32_t num_bytes_read) { | |
104 return MojoEndReadData(data_pipe_consumer.value(), num_bytes_read); | |
105 } | |
106 | |
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 | |
110 // vectors?) | |
111 class DataPipe { | |
112 public: | |
113 DataPipe(); | |
114 explicit DataPipe(const MojoCreateDataPipeOptions& options); | |
115 ~DataPipe(); | |
116 | |
117 ScopedDataPipeProducerHandle producer_handle; | |
118 ScopedDataPipeConsumerHandle consumer_handle; | |
119 }; | |
120 | |
121 inline DataPipe::DataPipe() { | |
122 MojoResult result MOJO_ALLOW_UNUSED = | |
123 CreateDataPipe(NULL, &producer_handle, &consumer_handle); | |
124 assert(result == MOJO_RESULT_OK); | |
125 } | |
126 | |
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); | |
131 } | |
132 | |
133 inline DataPipe::~DataPipe() { | |
134 } | |
135 | |
136 } // namespace mojo | |
137 | |
138 #endif // MOJO_PUBLIC_CPP_SYSTEM_DATA_PIPE_H_ | |
OLD | NEW |