Integrate Mojo JS validation bindings with the Router
[chromium-blink-merge.git] / mojo / public / cpp / system / data_pipe.h
blob37120666a1152de4f89be99d9b84cf259c4cfbe5
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.
5 #ifndef MOJO_PUBLIC_CPP_SYSTEM_DATA_PIPE_H_
6 #define MOJO_PUBLIC_CPP_SYSTEM_DATA_PIPE_H_
8 #include <assert.h>
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"
14 namespace mojo {
16 // DataPipeProducerHandle and DataPipeConsumerHandle ---------------------------
18 class DataPipeProducerHandle : public Handle {
19 public:
20 DataPipeProducerHandle() {}
21 explicit DataPipeProducerHandle(MojoHandle value) : Handle(value) {}
23 // Copying and assignment allowed.
26 MOJO_COMPILE_ASSERT(sizeof(DataPipeProducerHandle) == sizeof(Handle),
27 bad_size_for_cpp_DataPipeProducerHandle);
29 typedef ScopedHandleBase<DataPipeProducerHandle> ScopedDataPipeProducerHandle;
30 MOJO_COMPILE_ASSERT(sizeof(ScopedDataPipeProducerHandle) ==
31 sizeof(DataPipeProducerHandle),
32 bad_size_for_cpp_ScopedDataPipeProducerHandle);
34 class DataPipeConsumerHandle : public Handle {
35 public:
36 DataPipeConsumerHandle() {}
37 explicit DataPipeConsumerHandle(MojoHandle value) : Handle(value) {}
39 // Copying and assignment allowed.
42 MOJO_COMPILE_ASSERT(sizeof(DataPipeConsumerHandle) == sizeof(Handle),
43 bad_size_for_cpp_DataPipeConsumerHandle);
45 typedef ScopedHandleBase<DataPipeConsumerHandle> ScopedDataPipeConsumerHandle;
46 MOJO_COMPILE_ASSERT(sizeof(ScopedDataPipeConsumerHandle) ==
47 sizeof(DataPipeConsumerHandle),
48 bad_size_for_cpp_ScopedDataPipeConsumerHandle);
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;
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);
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);
82 inline MojoResult EndWriteDataRaw(DataPipeProducerHandle data_pipe_producer,
83 uint32_t num_bytes_written) {
84 return MojoEndWriteData(data_pipe_producer.value(), num_bytes_written);
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);
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);
102 inline MojoResult EndReadDataRaw(DataPipeConsumerHandle data_pipe_consumer,
103 uint32_t num_bytes_read) {
104 return MojoEndReadData(data_pipe_consumer.value(), num_bytes_read);
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();
117 ScopedDataPipeProducerHandle producer_handle;
118 ScopedDataPipeConsumerHandle consumer_handle;
121 inline DataPipe::DataPipe() {
122 MojoResult result MOJO_ALLOW_UNUSED =
123 CreateDataPipe(NULL, &producer_handle, &consumer_handle);
124 assert(result == MOJO_RESULT_OK);
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);
133 inline DataPipe::~DataPipe() {
136 } // namespace mojo
138 #endif // MOJO_PUBLIC_CPP_SYSTEM_DATA_PIPE_H_