diff --git a/README.md b/README.md index 6dc71e2..8b9d394 100644 --- a/README.md +++ b/README.md @@ -364,7 +364,7 @@ The binary (de)serializer supports *very* experimental versioning. Otherwise add members is a breaking change. The versioning looks like this:
-// enable definition of the macros shown below (otherwise use long macros defined in +// enable definition of the macros shown below (otherwise use long macros defined in // `lib/versioning.h`) #define REFLECTIVE_RAPIDJSON_SHORT_MACROS @@ -379,7 +379,7 @@ as_of_version(3): std::uint32_t bar; // will be read/written if outer scope version is >= 3 }; -// example struct where version is serialized/deserialized; defaults to version when writing +// example struct where version is serialized/deserialized; defaults to version 3 when writing struct Example : public BinarySerializable<Example, 3> { Nested nested; // will be read/written in any case, version is "propagated down" std::uint32_t a, b; // will be read/written in any case @@ -395,8 +395,11 @@ as_of_version(4): };-A mechanism to catch unsupported versions during deserialization is yet to be implemented. -Additionally, the versioning is completely untested at this point. +The version specified as template argument is also assumed to be the highest supported version. +If a higher version is encountered during deserialization, `BinaryVersionNotSupported` is thrown +and the deserialization aborted. + +Note that the versioning is mostly untested at this point. ### Remarks * Static member variables and member functions are currently ignored by the generator. diff --git a/generator/binaryserializationcodegenerator.cpp b/generator/binaryserializationcodegenerator.cpp index b8b9b9b..83bce11 100644 --- a/generator/binaryserializationcodegenerator.cpp +++ b/generator/binaryserializationcodegenerator.cpp @@ -308,10 +308,13 @@ void BinarySerializationCodeGenerator::generate(std::ostream &os) const << ">(BinaryDeserializer &deserializer, ::" << relevantClass.qualifiedName << " &customObject, BinaryVersion version)\n{\n"; if (!relevantClass.relevantBase.empty()) { os << " // read version\n" - " if constexpr (Versioning<" + " using V = Versioning<" << relevantClass.relevantBase - << ">::enabled) {\n" - " version = deserializer.readVariableLengthUIntBE();\n" + << ">;\n" + " if constexpr (V::enabled) {\n" + " V::assertVersion(version = deserializer.readVariableLengthUIntBE(), \"" + << relevantClass.qualifiedName + << "\");\n" " }\n"; } os << " // read base classes\n"; diff --git a/lib/binary/serializable.h b/lib/binary/serializable.h index ff0cd26..e5f564d 100644 --- a/lib/binary/serializable.h +++ b/lib/binary/serializable.h @@ -14,10 +14,13 @@ namespace ReflectiveRapidJSON { +using BinaryVersionNotSupported = VersionNotSupported