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"
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;
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
)
38 MessageWithRequestIDBuilder::MessageWithRequestIDBuilder(uint32_t name
,
42 : MessageBuilder(sizeof(MessageHeaderWithRequestID
) + payload_size
) {
43 MessageHeaderWithRequestID
* header
;
44 Allocate(&buf_
, &header
);
45 header
->num_fields
= 3;
47 header
->flags
= flags
;
48 header
->request_id
= request_id
;
51 } // namespace internal