1 // Copyright 2018 The Fuchsia 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.
4 
5 #include <lib/fidl/cpp/message_builder.h>
6 
7 #include <stdlib.h>
8 #include <stdio.h>
9 
10 namespace fidl {
11 
MessageBuilder(const fidl_type_t * type,uint32_t bytes_capacity,uint32_t handles_capacity)12 MessageBuilder::MessageBuilder(const fidl_type_t* type,
13                                uint32_t bytes_capacity,
14                                uint32_t handles_capacity)
15     : type_(type),
16       buffer_(bytes_capacity, handles_capacity) {
17     Reset();
18 }
19 
20 MessageBuilder::~MessageBuilder() = default;
21 
Encode(Message * message_out,const char ** error_msg_out)22 zx_status_t MessageBuilder::Encode(Message* message_out,
23                                    const char** error_msg_out) {
24     *message_out = Message(Finalize(),
25                            HandlePart(buffer_.handles(),
26                                       buffer_.handles_capacity()));
27     return message_out->Encode(type_, error_msg_out);
28 }
29 
Reset()30 void MessageBuilder::Reset() {
31     Builder::Reset(buffer_.bytes(), buffer_.bytes_capacity());
32     New<fidl_message_header_t>();
33 }
34 
35 } // namespace fidl
36