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