2023-10-24 14:54:00 -07:00
|
|
|
// Generated file -- DO NOT MODIFY.
|
|
|
|
#include "{{file}}.h"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
const uint64_t header_size = 24; // 4x uint32, 1x uint64
|
|
|
|
|
|
|
|
struct ExtPointer {
|
|
|
|
uint32_t offset;
|
|
|
|
uint32_t length;
|
|
|
|
};
|
|
|
|
|
|
|
|
void CheckHeader(const glcr::ByteBuffer& bytes) {
|
|
|
|
// TODO: Check ident.
|
|
|
|
// TODO: Parse core size.
|
|
|
|
// TODO: Parse extension size.
|
|
|
|
// TODO: Check CRC32
|
|
|
|
// TODO: Parse options.
|
|
|
|
}
|
|
|
|
|
|
|
|
void WriteHeader(glcr::ByteBuffer& bytes, uint64_t offset, uint32_t core_size, uint32_t extension_size) {
|
|
|
|
bytes.WriteAt<uint32_t>(offset + 0, 0xDEADBEEF); // TODO: Chose a more unique ident sequence.
|
|
|
|
bytes.WriteAt<uint32_t>(offset + 4, core_size);
|
|
|
|
bytes.WriteAt<uint32_t>(offset + 8, extension_size);
|
|
|
|
bytes.WriteAt<uint32_t>(offset + 12, 0); // TODO: Calculate CRC32.
|
|
|
|
bytes.WriteAt<uint64_t>(offset + 16, 0); // TODO: Add options.
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
{%- for message in messages %}
|
|
|
|
void {{message.name}}::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t offset) {
|
2023-11-10 12:51:23 -08:00
|
|
|
ParseFromBytesInternal(bytes, offset);
|
2023-10-24 14:54:00 -07:00
|
|
|
|
|
|
|
{%- for field in message.fields %}
|
2023-11-10 12:51:23 -08:00
|
|
|
{%- if field.type == Type.CAPABILITY %}
|
2023-10-24 14:54:00 -07:00
|
|
|
// Parse {{field.name}}.
|
|
|
|
// FIXME: Implement in-buffer capabilities for inprocess serialization.
|
|
|
|
set_{{field.name}}(0);
|
|
|
|
{%- endif %}
|
|
|
|
{%- endfor %}
|
|
|
|
}
|
|
|
|
|
|
|
|
void {{message.name}}::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t offset, const glcr::CapBuffer& caps) {
|
2023-11-10 12:51:23 -08:00
|
|
|
ParseFromBytesInternal(bytes, offset);
|
|
|
|
|
|
|
|
{%- for field in message.fields %}
|
|
|
|
{%- if field.type == Type.CAPABILITY %}
|
|
|
|
// Parse {{field.name}}.
|
|
|
|
uint64_t {{field.name}}_ptr = bytes.At<uint64_t>(offset + header_size + (8 * {{loop.index0}}));
|
|
|
|
|
|
|
|
set_{{field.name}}(caps.At({{field.name}}_ptr));
|
|
|
|
{%- endif %}
|
|
|
|
{%- endfor %}
|
|
|
|
}
|
|
|
|
|
|
|
|
void {{message.name}}::ParseFromBytesInternal(const glcr::ByteBuffer& bytes, uint64_t offset) {
|
2023-10-24 14:54:00 -07:00
|
|
|
CheckHeader(bytes);
|
|
|
|
|
|
|
|
{%- for field in message.fields %}
|
|
|
|
// Parse {{field.name}}.
|
2023-11-10 12:26:59 -08:00
|
|
|
{%- if not field.repeated %}
|
2023-10-24 14:54:00 -07:00
|
|
|
{%- if field.type == Type.U64 %}
|
|
|
|
set_{{field.name}}(bytes.At<uint64_t>(offset + header_size + (8 * {{loop.index0}})));
|
|
|
|
{%- elif field.type == Type.I64 %}
|
|
|
|
set_{{field.name}}(bytes.At<int64_t>(offset + header_size + (8 * {{loop.index0}})));
|
|
|
|
{%- elif field.type == Type.STRING %}
|
|
|
|
auto {{field.name}}_pointer = bytes.At<ExtPointer>(offset + header_size + (8 * {{loop.index0}}));
|
|
|
|
|
|
|
|
set_{{field.name}}(bytes.StringAt(offset + {{field.name}}_pointer.offset, {{field.name}}_pointer.length));
|
|
|
|
{%- elif field.type == Type.CAPABILITY %}
|
2023-11-10 12:51:23 -08:00
|
|
|
// Skip Cap.
|
2023-10-24 14:54:00 -07:00
|
|
|
{%- else %}
|
|
|
|
// TODO: Unimplemented parsing {{field.type}}
|
|
|
|
{%- endif %}
|
2023-11-10 12:26:59 -08:00
|
|
|
{%- else %}
|
|
|
|
auto {{field.name}}_pointer = bytes.At<ExtPointer>(offset + header_size + (8 * {{loop.index0}}));
|
|
|
|
|
2023-11-14 21:08:00 -08:00
|
|
|
{{field.name}}_.Resize({{field.name}}_pointer.length / sizeof({{field.cpp_type()}}));
|
2023-11-10 12:26:59 -08:00
|
|
|
for (uint64_t i = offset + {{field.name}}_pointer.offset;
|
2023-11-14 21:08:00 -08:00
|
|
|
i < offset + {{field.name}}_pointer.offset + {{field.name}}_pointer.length;
|
2023-11-10 12:26:59 -08:00
|
|
|
i += sizeof({{field.cpp_type()}})) {
|
|
|
|
{{field.name}}_.PushBack(bytes.At<{{field.cpp_type()}}>(i));
|
|
|
|
}
|
|
|
|
{% endif %}
|
2023-10-24 14:54:00 -07:00
|
|
|
{%- endfor %}
|
2023-11-10 12:51:23 -08:00
|
|
|
|
2023-10-24 14:54:00 -07:00
|
|
|
}
|
|
|
|
|
2023-10-24 18:19:12 -07:00
|
|
|
uint64_t {{message.name}}::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const {
|
2023-10-24 14:54:00 -07:00
|
|
|
uint32_t next_extension = header_size + 8 * {{ message.fields | length }};
|
|
|
|
const uint32_t core_size = next_extension;
|
|
|
|
|
|
|
|
{%- for field in message.fields %}
|
|
|
|
// Write {{field.name}}.
|
2023-11-10 12:26:59 -08:00
|
|
|
{%- if not field.repeated %}
|
2023-10-24 14:54:00 -07:00
|
|
|
{%- if field.type == Type.U64 %}
|
|
|
|
bytes.WriteAt<uint64_t>(offset + header_size + (8 * {{loop.index0}}), {{field.name}}());
|
|
|
|
{%- elif field.type == Type.I64 %}
|
|
|
|
bytes.WriteAt<int64_t>(offset + header_size + (8 * {{loop.index0}}), {{field.name}}());
|
|
|
|
{%- elif field.type == Type.STRING %}
|
|
|
|
ExtPointer {{field.name}}_ptr{
|
|
|
|
.offset = next_extension,
|
|
|
|
// FIXME: Check downcast of str length.
|
|
|
|
.length = (uint32_t){{field.name}}().length(),
|
|
|
|
};
|
|
|
|
|
|
|
|
bytes.WriteStringAt(offset + next_extension, {{field.name}}());
|
|
|
|
next_extension += {{field.name}}_ptr.length;
|
|
|
|
|
|
|
|
bytes.WriteAt<ExtPointer>(offset + header_size + (8 * {{loop.index0}}), {{field.name}}_ptr);
|
|
|
|
{%- elif field.type == Type.CAPABILITY %}
|
|
|
|
// FIXME: Implement inbuffer capabilities.
|
|
|
|
bytes.WriteAt<uint64_t>(offset + header_size + (8 * {{loop.index0}}), 0);
|
|
|
|
{%- else %}
|
|
|
|
// TODO: Unimplemented serialization {{field.type}}
|
|
|
|
{%- endif %}
|
2023-11-10 12:26:59 -08:00
|
|
|
{%- else %}
|
|
|
|
ExtPointer {{field.name}}_ptr{
|
|
|
|
.offset = next_extension,
|
|
|
|
.length = (uint32_t)({{field.name}}().size() * sizeof({{field.cpp_type()}})),
|
|
|
|
};
|
|
|
|
|
|
|
|
next_extension += {{field.name}}_ptr.length;
|
|
|
|
bytes.WriteAt<ExtPointer>(offset + header_size + (8 * {{loop.index0}}), {{field.name}}_ptr);
|
|
|
|
|
|
|
|
for (uint64_t i = 0; i < {{field.name}}().size(); i++) {
|
|
|
|
uint32_t ext_offset = offset + {{field.name}}_ptr.offset + (i * sizeof({{field.cpp_type()}}));
|
|
|
|
bytes.WriteAt<{{field.cpp_type()}}>(ext_offset, {{field.name}}().at(i));
|
|
|
|
}
|
|
|
|
{%- endif %}
|
2023-10-24 14:54:00 -07:00
|
|
|
{%- endfor %}
|
|
|
|
|
|
|
|
// The next extension pointer is the length of the message.
|
|
|
|
WriteHeader(bytes, offset, core_size, next_extension);
|
|
|
|
|
|
|
|
return next_extension;
|
|
|
|
}
|
|
|
|
|
2023-10-24 18:19:12 -07:00
|
|
|
uint64_t {{message.name}}::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset, glcr::CapBuffer& caps) const {
|
2023-10-24 14:54:00 -07:00
|
|
|
uint32_t next_extension = header_size + 8 * {{ message.fields | length}};
|
|
|
|
const uint32_t core_size = next_extension;
|
|
|
|
uint64_t next_cap = 0;
|
|
|
|
|
|
|
|
{%- for field in message.fields %}
|
|
|
|
// Write {{field.name}}.
|
2023-11-10 12:26:59 -08:00
|
|
|
{%- if not field.repeated %}
|
2023-10-24 14:54:00 -07:00
|
|
|
{%- if field.type == Type.U64 %}
|
|
|
|
bytes.WriteAt<uint64_t>(offset + header_size + (8 * {{loop.index0}}), {{field.name}}());
|
|
|
|
{%- elif field.type == Type.I64 %}
|
|
|
|
bytes.WriteAt<int64_t>(offset + header_size + (8 * {{loop.index0}}), {{field.name}}());
|
|
|
|
{%- elif field.type == Type.STRING %}
|
|
|
|
ExtPointer {{field.name}}_ptr{
|
|
|
|
.offset = next_extension,
|
|
|
|
// FIXME: Check downcast of str length.
|
|
|
|
.length = (uint32_t){{field.name}}().length(),
|
|
|
|
};
|
|
|
|
|
|
|
|
bytes.WriteStringAt(offset + next_extension, {{field.name}}());
|
|
|
|
next_extension += {{field.name}}_ptr.length;
|
|
|
|
|
|
|
|
bytes.WriteAt<ExtPointer>(offset + header_size + (8 * {{loop.index0}}), {{field.name}}_ptr);
|
|
|
|
{%- elif field.type == Type.CAPABILITY %}
|
|
|
|
caps.WriteAt(next_cap, {{field.name}}());
|
|
|
|
bytes.WriteAt<uint64_t>(offset + header_size + (8 * {{loop.index0}}), next_cap++);
|
|
|
|
{%- else %}
|
|
|
|
// TODO: Unimplemented serialization {{field.type}}
|
|
|
|
{%- endif %}
|
2023-11-10 12:26:59 -08:00
|
|
|
{%- else %}
|
|
|
|
ExtPointer {{field.name}}_ptr{
|
|
|
|
.offset = next_extension,
|
|
|
|
.length = (uint32_t)({{field.name}}().size() * sizeof({{field.cpp_type()}})),
|
|
|
|
};
|
|
|
|
|
|
|
|
next_extension += {{field.name}}_ptr.length;
|
|
|
|
bytes.WriteAt<ExtPointer>(offset + header_size + (8 * {{loop.index0}}), {{field.name}}_ptr);
|
|
|
|
|
|
|
|
for (uint64_t i = 0; i < {{field.name}}().size(); i++) {
|
|
|
|
uint32_t ext_offset = offset + {{field.name}}_ptr.offset + (i * sizeof({{field.cpp_type()}}));
|
|
|
|
bytes.WriteAt<{{field.cpp_type()}}>(ext_offset, {{field.name}}().at(i));
|
|
|
|
}
|
|
|
|
{%- endif %}
|
2023-10-24 14:54:00 -07:00
|
|
|
{%- endfor %}
|
|
|
|
|
|
|
|
// The next extension pointer is the length of the message.
|
|
|
|
WriteHeader(bytes, offset, core_size, next_extension);
|
|
|
|
|
|
|
|
return next_extension;
|
|
|
|
}
|
|
|
|
{%- endfor %}
|