[Yunq] Explicitly specialize serialization template functions.

While this may end up with some duplicated code, it helps avoid
accidentally generating code for a type that we didn't intend to.
This will help in the future because we will get a linker error rather
than wonky runtime behavior.
This commit is contained in:
Drew Galbraith 2024-01-11 20:41:51 -08:00
parent 7ec4f696a8
commit a71d5e9d24
2 changed files with 29 additions and 20 deletions

View File

@ -14,6 +14,12 @@ void Serializer::WriteHeader() {
buffer_.WriteAt<uint32_t>(offset_ + 12, 0); // TODO: Calculate CRC32.
}
template <>
void Serializer::WriteField<uint64_t>(uint64_t field_index,
const uint64_t& value) {
buffer_.WriteAt<uint64_t>(field_offset(field_index), value);
}
template <>
void Serializer::WriteField<glcr::String>(uint64_t field_index,
const glcr::String& value) {
@ -29,6 +35,23 @@ void Serializer::WriteField<glcr::String>(uint64_t field_index,
buffer_.WriteAt<ExtensionPointer>(field_offset(field_index), ptr);
}
template <>
void Serializer::WriteRepeated<uint64_t>(uint64_t field_index,
const glcr::Vector<uint64_t>& value) {
ExtensionPointer ptr{
.offset = (uint32_t)next_extension_,
.length = (uint32_t)(value.size() * sizeof(uint64_t)),
};
next_extension_ += ptr.length;
buffer_.WriteAt<ExtensionPointer>(field_offset(field_index), ptr);
for (uint64_t i = 0; i < value.size(); i++) {
uint32_t ext_offset = offset_ + ptr.offset + (i * sizeof(uint64_t));
buffer_.WriteAt<uint64_t>(ext_offset, value.at(i));
}
}
void Serializer::WriteCapability(uint64_t field_index, uint64_t value) {
if (caps_) {
buffer_.WriteAt<uint64_t>(field_offset(field_index), next_cap_);

View File

@ -55,30 +55,16 @@ class Serializer {
}
};
template <typename T>
void Serializer::WriteField(uint64_t field_index, const T& value) {
buffer_.WriteAt<T>(field_offset(field_index), value);
}
template <>
void Serializer::WriteField<uint64_t>(uint64_t field_index,
const uint64_t& value);
template <>
void Serializer::WriteField<glcr::String>(uint64_t field_index,
const glcr::String& value);
template <typename T>
void Serializer::WriteRepeated(uint64_t field_index,
const glcr::Vector<T>& value) {
ExtensionPointer ptr{
.offset = (uint32_t)next_extension_,
.length = (uint32_t)(value.size() * sizeof(T)),
};
next_extension_ += ptr.length;
buffer_.WriteAt<ExtensionPointer>(field_offset(field_index), ptr);
for (uint64_t i = 0; i < value.size(); i++) {
uint32_t ext_offset = offset_ + ptr.offset + (i * sizeof(T));
buffer_.WriteAt<T>(ext_offset, value.at(i));
}
}
template <>
void Serializer::WriteRepeated<uint64_t>(uint64_t field_index,
const glcr::Vector<uint64_t>& value);
} // namespace yunq