OLD | NEW |
| (Empty) |
1 {%- import "struct_macros.tmpl" as struct_macros %} | |
2 | |
3 size_t {{struct.name}}::GetSerializedSize() const { | |
4 return GetSerializedSize_(*this); | |
5 } | |
6 | |
7 bool {{struct.name}}::Serialize(void* buf, | |
8 size_t buf_size, | |
9 size_t* bytes_written) { | |
10 MOJO_DCHECK(buf); | |
11 | |
12 mojo::internal::FixedBuffer overlay_buf; | |
13 overlay_buf.Initialize(buf, buf_size); | |
14 | |
15 internal::{{struct.name}}_Data* output_ptr; | |
16 auto err = Serialize_(this, &overlay_buf, &output_ptr); | |
17 if (err != mojo::internal::ValidationError::NONE) { | |
18 // TODO(vardhan): Once Serialize_() outputs handles that it serialized | |
19 // (even partially, if there are failures), we should CHECK fail here if | |
20 // handles are non-empty. | |
21 MOJO_DLOG(ERROR) << "Could not serialize: " << | |
22 mojo::internal::ValidationErrorToString(err); | |
23 | |
24 if (bytes_written) | |
25 *bytes_written = overlay_buf.BytesUsed(); | |
26 return false; | |
27 } | |
28 | |
29 std::vector<mojo::Handle> handles; | |
30 output_ptr->EncodePointersAndHandles(&handles); | |
31 MOJO_CHECK(handles.empty()) << "Serialize() does not support handles."; | |
32 | |
33 if (bytes_written) | |
34 *bytes_written = overlay_buf.BytesUsed(); | |
35 return true; | |
36 } | |
37 | |
38 bool {{struct.name}}::Deserialize(void* buf, size_t buf_size) { | |
39 MOJO_DCHECK(buf); | |
40 | |
41 mojo::internal::BoundsChecker checker(buf, buf_size, 0); | |
42 | |
43 std::string* err_str = nullptr; | |
44 #if !defined(NDEBUG) | |
45 std::string err_str2; | |
46 err_str = &err_str2; | |
47 #endif | |
48 | |
49 mojo::internal::ValidationError err = | |
50 internal::{{struct.name}}_Data::Validate(buf, &checker, err_str); | |
51 if (err != mojo::internal::ValidationError::NONE) { | |
52 MOJO_DLOG(ERROR) << "Deserialization error " | |
53 << mojo::internal::ValidationErrorToString(err) | |
54 << ": " << *err_str; | |
55 return false; | |
56 } | |
57 | |
58 DeserializeWithoutValidation(buf); | |
59 return true; | |
60 } | |
61 | |
62 // TODO(vardhan): Make this |buf| a |const void*| once deserialization becomes | |
63 // immutable. | |
64 void {{struct.name}}::DeserializeWithoutValidation(void* buf) { | |
65 MOJO_DCHECK(buf); | |
66 | |
67 internal::{{struct.name}}_Data* input = | |
68 static_cast<internal::{{struct.name}}_Data*>(buf); | |
69 std::vector<mojo::Handle> handles; | |
70 input->DecodePointersAndHandles(&handles); | |
71 MOJO_CHECK(handles.empty()) << "Deserialization does not support handles."; | |
72 | |
73 Deserialize_(input, this); | |
74 } | |
75 | |
76 size_t GetSerializedSize_(const {{struct.name}}& input) { | |
77 {{struct_macros.get_serialized_size(struct, "input.%s")}} | |
78 return size; | |
79 } | |
80 | |
81 mojo::internal::ValidationError Serialize_( | |
82 {{struct.name}}* input, | |
83 mojo::internal::Buffer* buf, | |
84 internal::{{struct.name}}_Data** output) { | |
85 if (input) { | |
86 {{struct_macros.serialize(struct, struct.name ~ " struct", "input->%s", "res
ult", "buf", true)|indent(2)}} | |
87 *output = result; | |
88 } else { | |
89 *output = nullptr; | |
90 } | |
91 return mojo::internal::ValidationError::NONE; | |
92 } | |
93 | |
94 void Deserialize_(internal::{{struct.name}}_Data* input, | |
95 {{struct.name}}* result) { | |
96 if (input) { | |
97 {{struct_macros.deserialize(struct, "input", "result->%s")|indent(2)}} | |
98 } | |
99 } | |
OLD | NEW |