[Yunq] Factor out parsing most message fields to a shared method.
This commit is contained in:
parent
0dacbb87dc
commit
83dbd18cb4
|
@ -28,31 +28,21 @@ void WriteHeader(glcr::ByteBuffer& bytes, uint64_t offset, uint32_t core_size, u
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
void OpenFileRequest::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t offset) {
|
void OpenFileRequest::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t offset) {
|
||||||
CheckHeader(bytes);
|
ParseFromBytesInternal(bytes, offset);
|
||||||
// Parse path.
|
|
||||||
auto path_pointer = bytes.At<ExtPointer>(offset + header_size + (8 * 0));
|
|
||||||
|
|
||||||
set_path(bytes.StringAt(offset + path_pointer.offset, path_pointer.length));
|
|
||||||
// Parse options.
|
|
||||||
auto options_pointer = bytes.At<ExtPointer>(offset + header_size + (8 * 1))
|
|
||||||
|
|
||||||
options_.Resize(options_pointer.length);
|
|
||||||
for (uint64_t i = offset + options_pointer.offset;
|
|
||||||
i < offset + options_pointer.offset + (sizeof(uint64_t) * options_pointer.length);
|
|
||||||
i += sizeof(uint64_t)) {
|
|
||||||
options_.PushBack(bytes.At<uint64_t>(i));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void OpenFileRequest::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t offset, const glcr::CapBuffer& caps) {
|
void OpenFileRequest::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t offset, const glcr::CapBuffer& caps) {
|
||||||
|
ParseFromBytesInternal(bytes, offset);
|
||||||
|
}
|
||||||
|
|
||||||
|
void OpenFileRequest::ParseFromBytesInternal(const glcr::ByteBuffer& bytes, uint64_t offset) {
|
||||||
CheckHeader(bytes);
|
CheckHeader(bytes);
|
||||||
// Parse path.
|
// Parse path.
|
||||||
auto path_pointer = bytes.At<ExtPointer>(offset + header_size + (8 * 0));
|
auto path_pointer = bytes.At<ExtPointer>(offset + header_size + (8 * 0));
|
||||||
|
|
||||||
set_path(bytes.StringAt(offset + path_pointer.offset, path_pointer.length));
|
set_path(bytes.StringAt(offset + path_pointer.offset, path_pointer.length));
|
||||||
// Parse options.
|
// Parse options.
|
||||||
auto options_pointer = bytes.At<ExtPointer>(offset + header_size + (8 * 1))
|
auto options_pointer = bytes.At<ExtPointer>(offset + header_size + (8 * 1));
|
||||||
|
|
||||||
options_.Resize(options_pointer.length);
|
options_.Resize(options_pointer.length);
|
||||||
for (uint64_t i = offset + options_pointer.offset;
|
for (uint64_t i = offset + options_pointer.offset;
|
||||||
|
@ -61,6 +51,7 @@ void OpenFileRequest::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t off
|
||||||
options_.PushBack(bytes.At<uint64_t>(i));
|
options_.PushBack(bytes.At<uint64_t>(i));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t OpenFileRequest::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const {
|
uint64_t OpenFileRequest::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const {
|
||||||
|
@ -132,19 +123,21 @@ uint64_t OpenFileRequest::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t off
|
||||||
return next_extension;
|
return next_extension;
|
||||||
}
|
}
|
||||||
void File::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t offset) {
|
void File::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t offset) {
|
||||||
CheckHeader(bytes);
|
ParseFromBytesInternal(bytes, offset);
|
||||||
// Parse path.
|
|
||||||
auto path_pointer = bytes.At<ExtPointer>(offset + header_size + (8 * 0));
|
|
||||||
|
|
||||||
set_path(bytes.StringAt(offset + path_pointer.offset, path_pointer.length));
|
|
||||||
// Parse attrs.
|
|
||||||
set_attrs(bytes.At<uint64_t>(offset + header_size + (8 * 1)));
|
|
||||||
// Parse mem_cap.
|
// Parse mem_cap.
|
||||||
// FIXME: Implement in-buffer capabilities for inprocess serialization.
|
// FIXME: Implement in-buffer capabilities for inprocess serialization.
|
||||||
set_mem_cap(0);
|
set_mem_cap(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void File::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t offset, const glcr::CapBuffer& caps) {
|
void File::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t offset, const glcr::CapBuffer& caps) {
|
||||||
|
ParseFromBytesInternal(bytes, offset);
|
||||||
|
// Parse mem_cap.
|
||||||
|
uint64_t mem_cap_ptr = bytes.At<uint64_t>(offset + header_size + (8 * 2));
|
||||||
|
|
||||||
|
set_mem_cap(caps.At(mem_cap_ptr));
|
||||||
|
}
|
||||||
|
|
||||||
|
void File::ParseFromBytesInternal(const glcr::ByteBuffer& bytes, uint64_t offset) {
|
||||||
CheckHeader(bytes);
|
CheckHeader(bytes);
|
||||||
// Parse path.
|
// Parse path.
|
||||||
auto path_pointer = bytes.At<ExtPointer>(offset + header_size + (8 * 0));
|
auto path_pointer = bytes.At<ExtPointer>(offset + header_size + (8 * 0));
|
||||||
|
@ -153,9 +146,8 @@ void File::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t offset, const
|
||||||
// Parse attrs.
|
// Parse attrs.
|
||||||
set_attrs(bytes.At<uint64_t>(offset + header_size + (8 * 1)));
|
set_attrs(bytes.At<uint64_t>(offset + header_size + (8 * 1)));
|
||||||
// Parse mem_cap.
|
// Parse mem_cap.
|
||||||
uint64_t mem_cap_ptr = bytes.At<uint64_t>(offset + header_size + (8 * 2));
|
// Skip Cap.
|
||||||
|
|
||||||
set_mem_cap(caps.At(mem_cap_ptr));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t File::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const {
|
uint64_t File::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const {
|
||||||
|
|
|
@ -26,6 +26,8 @@ class OpenFileRequest {
|
||||||
glcr::String path_;
|
glcr::String path_;
|
||||||
glcr::Vector<uint64_t> options_;
|
glcr::Vector<uint64_t> options_;
|
||||||
|
|
||||||
|
// Parses everything except for caps.
|
||||||
|
void ParseFromBytesInternal(const glcr::ByteBuffer&, uint64_t offset);
|
||||||
};
|
};
|
||||||
class File {
|
class File {
|
||||||
public:
|
public:
|
||||||
|
@ -50,4 +52,6 @@ class File {
|
||||||
uint64_t attrs_;
|
uint64_t attrs_;
|
||||||
z_cap_t mem_cap_;
|
z_cap_t mem_cap_;
|
||||||
|
|
||||||
|
// Parses everything except for caps.
|
||||||
|
void ParseFromBytesInternal(const glcr::ByteBuffer&, uint64_t offset);
|
||||||
};
|
};
|
|
@ -30,39 +30,31 @@ void WriteHeader(glcr::ByteBuffer& bytes, uint64_t offset, uint32_t core_size, u
|
||||||
|
|
||||||
{%- for message in messages %}
|
{%- for message in messages %}
|
||||||
void {{message.name}}::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t offset) {
|
void {{message.name}}::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t offset) {
|
||||||
CheckHeader(bytes);
|
ParseFromBytesInternal(bytes, offset);
|
||||||
|
|
||||||
{%- for field in message.fields %}
|
{%- for field in message.fields %}
|
||||||
|
{%- if field.type == Type.CAPABILITY %}
|
||||||
// Parse {{field.name}}.
|
// Parse {{field.name}}.
|
||||||
{%- if not field.repeated %}
|
|
||||||
{%- 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 %}
|
|
||||||
// FIXME: Implement in-buffer capabilities for inprocess serialization.
|
// FIXME: Implement in-buffer capabilities for inprocess serialization.
|
||||||
set_{{field.name}}(0);
|
set_{{field.name}}(0);
|
||||||
{%- else %}
|
|
||||||
// TODO: Unimplemented parsing {{field.type}}
|
|
||||||
{%- endif %}
|
{%- endif %}
|
||||||
{%- else %}
|
|
||||||
auto {{field.name}}_pointer = bytes.At<ExtPointer>(offset + header_size + (8 * {{loop.index0}}));
|
|
||||||
|
|
||||||
{{field.name}}_.Resize({{field.name}}_pointer.length);
|
|
||||||
for (uint64_t i = offset + {{field.name}}_pointer.offset;
|
|
||||||
i < offset + {{field.name}}_pointer.offset + (sizeof({{field.cpp_type()}}) * {{field.name}}_pointer.length);
|
|
||||||
i += sizeof({{field.cpp_type()}})) {
|
|
||||||
{{field.name}}_.PushBack(bytes.At<{{field.cpp_type()}}>(i));
|
|
||||||
}
|
|
||||||
{% endif %}
|
|
||||||
{%- endfor %}
|
{%- endfor %}
|
||||||
}
|
}
|
||||||
|
|
||||||
void {{message.name}}::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t offset, const glcr::CapBuffer& caps) {
|
void {{message.name}}::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t offset, const glcr::CapBuffer& caps) {
|
||||||
|
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) {
|
||||||
CheckHeader(bytes);
|
CheckHeader(bytes);
|
||||||
|
|
||||||
{%- for field in message.fields %}
|
{%- for field in message.fields %}
|
||||||
|
@ -77,9 +69,7 @@ void {{message.name}}::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t of
|
||||||
|
|
||||||
set_{{field.name}}(bytes.StringAt(offset + {{field.name}}_pointer.offset, {{field.name}}_pointer.length));
|
set_{{field.name}}(bytes.StringAt(offset + {{field.name}}_pointer.offset, {{field.name}}_pointer.length));
|
||||||
{%- elif field.type == Type.CAPABILITY %}
|
{%- elif field.type == Type.CAPABILITY %}
|
||||||
uint64_t {{field.name}}_ptr = bytes.At<uint64_t>(offset + header_size + (8 * {{loop.index0}}));
|
// Skip Cap.
|
||||||
|
|
||||||
set_{{field.name}}(caps.At({{field.name}}_ptr));
|
|
||||||
{%- else %}
|
{%- else %}
|
||||||
// TODO: Unimplemented parsing {{field.type}}
|
// TODO: Unimplemented parsing {{field.type}}
|
||||||
{%- endif %}
|
{%- endif %}
|
||||||
|
@ -94,6 +84,7 @@ void {{message.name}}::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t of
|
||||||
}
|
}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{%- endfor %}
|
{%- endfor %}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t {{message.name}}::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const {
|
uint64_t {{message.name}}::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const {
|
||||||
|
|
|
@ -40,5 +40,7 @@ class {{message.name}} {
|
||||||
{%- endif %}
|
{%- endif %}
|
||||||
{%- endfor %}
|
{%- endfor %}
|
||||||
|
|
||||||
|
// Parses everything except for caps.
|
||||||
|
void ParseFromBytesInternal(const glcr::ByteBuffer&, uint64_t offset);
|
||||||
};
|
};
|
||||||
{%- endfor %}
|
{%- endfor %}
|
||||||
|
|
Loading…
Reference in New Issue