Integrate Mojo JS validation bindings with the Router
[chromium-blink-merge.git] / mojo / public / cpp / system / buffer.h
blob80b03c660964cfd7b824eaaca49a3ad724556691
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_BUFFER_H_
6 #define MOJO_PUBLIC_CPP_SYSTEM_BUFFER_H_
8 #include <assert.h>
10 #include "mojo/public/c/system/buffer.h"
11 #include "mojo/public/cpp/system/handle.h"
12 #include "mojo/public/cpp/system/macros.h"
14 namespace mojo {
16 // SharedBufferHandle ----------------------------------------------------------
18 class SharedBufferHandle : public Handle {
19 public:
20 SharedBufferHandle() {}
21 explicit SharedBufferHandle(MojoHandle value) : Handle(value) {}
23 // Copying and assignment allowed.
26 MOJO_COMPILE_ASSERT(sizeof(SharedBufferHandle) == sizeof(Handle),
27 bad_size_for_cpp_SharedBufferHandle);
29 typedef ScopedHandleBase<SharedBufferHandle> ScopedSharedBufferHandle;
30 MOJO_COMPILE_ASSERT(sizeof(ScopedSharedBufferHandle) ==
31 sizeof(SharedBufferHandle),
32 bad_size_for_cpp_ScopedSharedBufferHandle);
34 inline MojoResult CreateSharedBuffer(
35 const MojoCreateSharedBufferOptions* options,
36 uint64_t num_bytes,
37 ScopedSharedBufferHandle* shared_buffer) {
38 assert(shared_buffer);
39 SharedBufferHandle handle;
40 MojoResult rv = MojoCreateSharedBuffer(options, num_bytes,
41 handle.mutable_value());
42 // Reset even on failure (reduces the chances that a "stale"/incorrect handle
43 // will be used).
44 shared_buffer->reset(handle);
45 return rv;
48 // TODO(vtl): This (and also the functions below) are templatized to allow for
49 // future/other buffer types. A bit "safer" would be to overload this function
50 // manually. (The template enforces that the in and out handles to be of the
51 // same type.)
52 template <class BufferHandleType>
53 inline MojoResult DuplicateBuffer(
54 BufferHandleType buffer,
55 const MojoDuplicateBufferHandleOptions* options,
56 ScopedHandleBase<BufferHandleType>* new_buffer) {
57 assert(new_buffer);
58 BufferHandleType handle;
59 MojoResult rv = MojoDuplicateBufferHandle(
60 buffer.value(), options, handle.mutable_value());
61 // Reset even on failure (reduces the chances that a "stale"/incorrect handle
62 // will be used).
63 new_buffer->reset(handle);
64 return rv;
67 template <class BufferHandleType>
68 inline MojoResult MapBuffer(BufferHandleType buffer,
69 uint64_t offset,
70 uint64_t num_bytes,
71 void** pointer,
72 MojoMapBufferFlags flags) {
73 assert(buffer.is_valid());
74 return MojoMapBuffer(buffer.value(), offset, num_bytes, pointer, flags);
77 inline MojoResult UnmapBuffer(void* pointer) {
78 assert(pointer);
79 return MojoUnmapBuffer(pointer);
82 // A wrapper class that automatically creates a shared buffer and owns the
83 // handle.
84 class SharedBuffer {
85 public:
86 explicit SharedBuffer(uint64_t num_bytes);
87 SharedBuffer(uint64_t num_bytes,
88 const MojoCreateSharedBufferOptions& options);
89 ~SharedBuffer();
91 ScopedSharedBufferHandle handle;
94 inline SharedBuffer::SharedBuffer(uint64_t num_bytes) {
95 MojoResult result MOJO_ALLOW_UNUSED =
96 CreateSharedBuffer(NULL, num_bytes, &handle);
97 assert(result == MOJO_RESULT_OK);
100 inline SharedBuffer::SharedBuffer(
101 uint64_t num_bytes,
102 const MojoCreateSharedBufferOptions& options) {
103 MojoResult result MOJO_ALLOW_UNUSED =
104 CreateSharedBuffer(&options, num_bytes, &handle);
105 assert(result == MOJO_RESULT_OK);
108 inline SharedBuffer::~SharedBuffer() {
111 } // namespace mojo
113 #endif // MOJO_PUBLIC_CPP_SYSTEM_BUFFER_H_