diff --git a/src/serialize.h b/src/serialize.h --- a/src/serialize.h +++ b/src/serialize.h @@ -204,6 +204,29 @@ SerializationOp(s, CSerActionUnserialize()); \ } +/** + * Implement the Serialize and Unserialize methods by delegating to a single + * templated static method that takes the to-be-(de)serialized object as a + * parameter. This approach has the advantage that the constness of the object + * becomes a template parameter, and thus allows a single implementation that + * sees the object as const for serializing and non-const for deserializing, + * without casts. + */ +#define SERIALIZE_METHODS(cls, obj) \ + template void Serialize(Stream &s) const { \ + static_assert(std::is_same::value, \ + "Serialize type mismatch"); \ + SerializationOps(*this, s, CSerActionSerialize()); \ + } \ + template void Unserialize(Stream &s) { \ + static_assert(std::is_same::value, \ + "Unserialize type mismatch"); \ + SerializationOps(*this, s, CSerActionUnserialize()); \ + } \ + template \ + static inline void SerializationOps(Type &obj, Stream &s, \ + Operation ser_action) + #ifndef CHAR_EQUALS_INT8 // TODO Get rid of bare char template inline void Serialize(Stream &s, char a) {