Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / mojo / public / cpp / bindings / lib / message_builder.cc
blobc7466444d04918b488a75308f4468419bdff740f
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.
5 #include "mojo/public/cpp/bindings/lib/message_builder.h"
7 #include "mojo/public/cpp/bindings/message.h"
9 namespace mojo {
10 namespace internal {
12 template <typename Header>
13 void Allocate(Buffer* buf, Header** header) {
14 *header = static_cast<Header*>(buf->Allocate(sizeof(Header)));
15 (*header)->num_bytes = sizeof(Header);
18 MessageBuilder::MessageBuilder(uint32_t name, size_t payload_size)
19 : buf_(sizeof(MessageHeader) + payload_size) {
20 MessageHeader* header;
21 Allocate(&buf_, &header);
22 header->num_fields = 2;
23 header->name = name;
26 MessageBuilder::~MessageBuilder() {
29 void MessageBuilder::Finish(Message* message) {
30 uint32_t num_bytes = static_cast<uint32_t>(buf_.size());
31 message->AdoptData(num_bytes, static_cast<MessageData*>(buf_.Leak()));
34 MessageBuilder::MessageBuilder(size_t size)
35 : buf_(size) {
38 MessageWithRequestIDBuilder::MessageWithRequestIDBuilder(uint32_t name,
39 size_t payload_size,
40 uint32_t flags,
41 uint64_t request_id)
42 : MessageBuilder(sizeof(MessageHeaderWithRequestID) + payload_size) {
43 MessageHeaderWithRequestID* header;
44 Allocate(&buf_, &header);
45 header->num_fields = 3;
46 header->name = name;
47 header->flags = flags;
48 header->request_id = request_id;
51 } // namespace internal
52 } // namespace mojo