From 49b5c45fa8e598a8c56c37e089a7c4d529bf4cad Mon Sep 17 00:00:00 2001 From: Hannes Vogt Date: Tue, 10 Oct 2017 17:22:25 +0200 Subject: [PATCH] old archives should be compatible with newer version --- src/serialbox/core/SerializerImpl.cpp | 2 +- src/serialbox/core/Version.h | 23 +++++++++++-------- src/serialbox/core/archive/BinaryArchive.cpp | 2 +- test/serialbox/core/UnittestVersion.cpp | 5 ++-- .../core/archive/UnittestBinaryArchive.cpp | 4 ++-- 5 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/serialbox/core/SerializerImpl.cpp b/src/serialbox/core/SerializerImpl.cpp index f9d4d86f..0c91de4c 100644 --- a/src/serialbox/core/SerializerImpl.cpp +++ b/src/serialbox/core/SerializerImpl.cpp @@ -298,7 +298,7 @@ void SerializerImpl::constructMetaDataFromJson() { int serialboxVersion = jsonNode["serialbox_version"]; - if(!Version::match(serialboxVersion)) + if(!Version::isCompatible(serialboxVersion)) throw Exception( "serialbox version of MetaData (%s) does not match the version of the library (%s)", Version::toString(serialboxVersion), SERIALBOX_VERSION_STRING); diff --git a/src/serialbox/core/Version.h b/src/serialbox/core/Version.h index 8350a063..082bdc79 100644 --- a/src/serialbox/core/Version.h +++ b/src/serialbox/core/Version.h @@ -25,6 +25,12 @@ namespace serialbox { /// \brief Utility to deal with Serialbox versions struct Version { +private: + static int getVersion() { + return SERIALBOX_VERSION_MAJOR * 100 + SERIALBOX_VERSION_MINOR * 10 + SERIALBOX_VERSION_PATCH; + } + +public: Version() = delete; /// \brief Convert to string @@ -44,16 +50,15 @@ struct Version { /// /// \return Return true if the versions match /// @{ - static bool match(int version) noexcept { - int major = version / 100; - int minor = (version - major * 100) / 10; - return Version::match(major, minor, version - 100 * major - 10 * minor); - } + static bool match(int version) noexcept { return version == getVersion(); } + /// @} - static bool match(int major, int minor, int patch) noexcept { - return ((major == SERIALBOX_VERSION_MAJOR) && (minor == SERIALBOX_VERSION_MINOR) && - (patch == SERIALBOX_VERSION_PATCH)); - } + /// \brief Check if the given version is compatible with the current library version (i.e. is + /// older) + /// + /// \return Return true if the versions is compatible + /// @{ + static bool isCompatible(int version) noexcept { return version <= getVersion(); } /// @} }; diff --git a/src/serialbox/core/archive/BinaryArchive.cpp b/src/serialbox/core/archive/BinaryArchive.cpp index a2af88b3..35df3962 100644 --- a/src/serialbox/core/archive/BinaryArchive.cpp +++ b/src/serialbox/core/archive/BinaryArchive.cpp @@ -222,7 +222,7 @@ void BinaryArchive::readMetaDataFromJson() { std::string hashAlgorithm = json_["hash_algorithm"]; // Check consistency - if(!Version::match(serialboxVersion)) + if(!Version::isCompatible(serialboxVersion)) throw Exception("serialbox version of binary archive (%s) does not match the version " "of the library (%s)", Version::toString(serialboxVersion), SERIALBOX_VERSION_STRING); diff --git a/test/serialbox/core/UnittestVersion.cpp b/test/serialbox/core/UnittestVersion.cpp index f7731920..11a31da5 100644 --- a/test/serialbox/core/UnittestVersion.cpp +++ b/test/serialbox/core/UnittestVersion.cpp @@ -22,10 +22,11 @@ TEST(VersionTest, Comparison) { int minor = SERIALBOX_VERSION_MINOR; int patch = SERIALBOX_VERSION_PATCH; - EXPECT_TRUE(Version::match(major, minor, patch)); - EXPECT_FALSE(Version::match(major - 1, minor, patch)); EXPECT_TRUE(Version::match(major * 100 + minor * 10 + patch)); EXPECT_FALSE(Version::match(0)); + + EXPECT_TRUE(Version::isCompatible(major * 100 + minor * 10 + (patch - 1))); + EXPECT_FALSE(Version::isCompatible(major * 100 + minor * 10 + (patch + 1))); } TEST(VersionTest, ToString) { diff --git a/test/serialbox/core/archive/UnittestBinaryArchive.cpp b/test/serialbox/core/archive/UnittestBinaryArchive.cpp index 3162d5ae..607074f2 100644 --- a/test/serialbox/core/archive/UnittestBinaryArchive.cpp +++ b/test/serialbox/core/archive/UnittestBinaryArchive.cpp @@ -117,11 +117,11 @@ TEST_F(BinaryArchiveUtilityTest, MetaData) { }; // ----------------------------------------------------------------------------------------------- - // Invlaid serialbox version + // Invalid serialbox version // ----------------------------------------------------------------------------------------------- { json::json corrupted = j; - corrupted["serialbox_version"] = 100 * (SERIALBOX_VERSION_MAJOR - 1) + + corrupted["serialbox_version"] = 100 * (SERIALBOX_VERSION_MAJOR + 1) + 10 * SERIALBOX_VERSION_MINOR + SERIALBOX_VERSION_PATCH; toFile(corrupted);