diff --git a/CMakeLists.txt b/CMakeLists.txt index 9c95548e..0264bdb8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,6 +23,7 @@ else() testing meta memory + serialization math audio hid diff --git a/packages/nodegraph/CMakeLists.txt b/packages/nodegraph/CMakeLists.txt index 85803a43..639599cb 100644 --- a/packages/nodegraph/CMakeLists.txt +++ b/packages/nodegraph/CMakeLists.txt @@ -8,6 +8,7 @@ set(deps testing memory math + serialization audio hid ) diff --git a/packages/nodegraph/include/nodegraph/core/NodeGraphGroup.h b/packages/nodegraph/include/nodegraph/core/NodeGraphGroup.h index baa53250..276d952e 100644 --- a/packages/nodegraph/include/nodegraph/core/NodeGraphGroup.h +++ b/packages/nodegraph/include/nodegraph/core/NodeGraphGroup.h @@ -10,6 +10,7 @@ #include #include "math/MathConstants.h" +#include "serialization/SerializationBase.h" #include "nodegraph/core/NodeGraphBase.h" @@ -32,7 +33,7 @@ namespace l::nodegraph { void Process(int32_t numSamples, std::vector& inputs, std::vector& outputs) override; }; - class NodeGraphGroup { + class NodeGraphGroup : public l::serialization::SerializationBase { public: NodeGraphGroup() {} ~NodeGraphGroup() { diff --git a/packages/serialization/CMakeLists.txt b/packages/serialization/CMakeLists.txt new file mode 100644 index 00000000..326d3195 --- /dev/null +++ b/packages/serialization/CMakeLists.txt @@ -0,0 +1,9 @@ +project (serialization) + +set(deps + various + logging + testing +) + +bs_generate_package(serialization "tier1" "${deps}" "") diff --git a/packages/storage/include/storage/CommaSeparatedData.h b/packages/serialization/include/serialization/CommaSeparatedData.h similarity index 100% rename from packages/storage/include/storage/CommaSeparatedData.h rename to packages/serialization/include/serialization/CommaSeparatedData.h diff --git a/packages/storage/include/storage/StorageBase.h b/packages/serialization/include/serialization/SerializationBase.h similarity index 65% rename from packages/storage/include/storage/StorageBase.h rename to packages/serialization/include/serialization/SerializationBase.h index 23afc87d..e5210dc4 100644 --- a/packages/storage/include/storage/StorageBase.h +++ b/packages/serialization/include/serialization/SerializationBase.h @@ -1,20 +1,17 @@ #pragma once -#include "memory/Containers.h" +#include "logging/LoggingAll.h" #include "various/serializer/Serializer.h" -#include "filesystem/File.h" +#include #include #include -#include #include #include #include -#include #include -namespace l { -namespace storage { +namespace l::serialization { template void convert(std::stringstream& dst, std::vector& src, size_t count = 0) { @@ -35,12 +32,6 @@ namespace storage { while (src >> tmp) dst.push_back(tmp); } - bool read(const std::filesystem::path& mLocation, char* dst, size_t count, size_t position = 0); - bool write(const std::filesystem::path& mLocation, const char* src, size_t count, size_t position = 0); - - bool read(const std::filesystem::path& mLocation, std::stringstream& data); - bool write(const std::filesystem::path& mLocation, std::stringstream& data); - struct HeaderValidity { static const int32_t kIdentifier = 0x00defa00; // storage base file identifier @@ -73,12 +64,12 @@ namespace storage { int32_t mVersion; }; - class StorageBase : public zpp::serializer::polymorphic { + class SerializationBase : public zpp::serializer::polymorphic { public: using SaveArchive = zpp::serializer::archive; using LoadArchive = zpp::serializer::archive; - StorageBase() : + SerializationBase() : mIdentifier(0), mVersion(0), mLatestVersion(mVersion), @@ -86,7 +77,7 @@ namespace storage { mUseVersion(false), mUseFiletype(false) {} - StorageBase(int32_t minimumVersion, int32_t latestVersion, bool useVersion = true, bool useFiletype = false) : + SerializationBase(int32_t minimumVersion, int32_t latestVersion, bool useVersion = true, bool useFiletype = false) : mIdentifier(0), mVersion(minimumVersion), mLatestVersion(latestVersion), @@ -97,9 +88,9 @@ namespace storage { ASSERT(minimumVersion <= mLatestVersion); } } - virtual ~StorageBase() = default; + virtual ~SerializationBase() = default; - StorageBase& operator=(StorageBase&& other) noexcept { + SerializationBase& operator=(SerializationBase&& other) noexcept { mIdentifier = other.mIdentifier; mVersion = other.mVersion; mLatestVersion = other.mLatestVersion; @@ -109,7 +100,7 @@ namespace storage { mUseFiletype = other.mUseFiletype; return *this; } - StorageBase& operator=(const StorageBase& other) noexcept { + SerializationBase& operator=(const SerializationBase& other) noexcept { mIdentifier = other.mIdentifier; mVersion = other.mVersion; mLatestVersion = other.mLatestVersion; @@ -119,10 +110,10 @@ namespace storage { mUseFiletype = other.mUseFiletype; return *this; } - StorageBase(StorageBase&& other) noexcept { + SerializationBase(SerializationBase&& other) noexcept { *this = std::move(other); } - StorageBase(const StorageBase& other) noexcept { + SerializationBase(const SerializationBase& other) noexcept { *this = other; } @@ -216,12 +207,12 @@ namespace storage { } }; - class Config : public StorageBase { + class Config : public SerializationBase { public: static const int32_t LatestVersion = 1; - Config() : StorageBase(0, LatestVersion), mName{}, mDefaultUserId{} {} - Config(int32_t version, std::string name, std::string defaultUserId) : StorageBase(version, LatestVersion), mName(name), mDefaultUserId(defaultUserId) {} + Config() : SerializationBase(0, LatestVersion), mName{}, mDefaultUserId{} {} + Config(int32_t version, std::string name, std::string defaultUserId) : SerializationBase(version, LatestVersion), mName(name), mDefaultUserId(defaultUserId) {} virtual ~Config() = default; friend zpp::serializer::access; @@ -240,89 +231,4 @@ namespace storage { std::string mDefaultUserId; }; - class LocalStorage { - public: - LocalStorage(std::filesystem::path&& location); - - template> - void Set(const std::string& key, std::unique_ptr value) { - mStore.emplace(key, std::move(value)); - } - - void Erase(const std::string& key) { - mStore.erase(key); - } - - template - T* Get(const std::string& key) { - auto it = mStore.find(key); - if (it != mStore.end()) { - return reinterpret_cast(it->second.get()); - } - return nullptr; - } - - template> - bool Save(const std::string& key) { - T* data = Get(key); - std::vector bytes; - - data->GetArchiveData(bytes); - - auto name = mLocation.stem().string() + "_" + key; - filesystem::File file(mLocation / name); - - file.modeBinary(); - file.modeWriteTrunc(); - - if (!file.open()) { - return false; - } - - file.write(bytes.data(), bytes.size()); - file.close(); - - return true; - } - - template> - bool Load(const std::string& key) { - T* data = Get(key); - if (data == nullptr) { - Set(key, std::make_unique()); - data = Get(key); - } - - auto name = mLocation.stem().string() + "_" + key; - filesystem::File file(mLocation / name); - file.modeBinary(); - file.modeReadPreload(); - - if (!file.open()) { - return false; - } - - - std::vector bytes; - bytes.resize(file.fileSize()); - if (file.read(bytes.data(), bytes.size()) != bytes.size()) { - return false; - } - - file.close(); - - data->LoadArchiveData(bytes); - - return true; - } - - - private: - void CreatePath(); - - std::filesystem::path mLocation; - std::map> mStore; - }; - -} } diff --git a/packages/storage/source/common/CommaSeparatedData.cpp b/packages/serialization/source/common/CommaSeparatedData.cpp similarity index 67% rename from packages/storage/source/common/CommaSeparatedData.cpp rename to packages/serialization/source/common/CommaSeparatedData.cpp index 46e65bb1..00d2684e 100644 --- a/packages/storage/source/common/CommaSeparatedData.cpp +++ b/packages/serialization/source/common/CommaSeparatedData.cpp @@ -1,4 +1,4 @@ -#include "storage/CommaSeparatedData.h" +#include "serialization/CommaSeparatedData.h" #include "logging/LoggingAll.h" diff --git a/packages/serialization/source/common/SerializationBase.cpp b/packages/serialization/source/common/SerializationBase.cpp new file mode 100644 index 00000000..fa4bd475 --- /dev/null +++ b/packages/serialization/source/common/SerializationBase.cpp @@ -0,0 +1,13 @@ +#include "serialization/SerializationBase.h" + +#include "logging/LoggingAll.h" + +#include +#include +#include +#include +#include + +namespace l::serialization { + +} diff --git a/packages/serialization/tests/common/SerializationBaseTest.cpp b/packages/serialization/tests/common/SerializationBaseTest.cpp new file mode 100644 index 00000000..713e1c5c --- /dev/null +++ b/packages/serialization/tests/common/SerializationBaseTest.cpp @@ -0,0 +1,198 @@ +#include "testing/Test.h" + +#include "serialization/SerializationBase.h" + +#include +#include + +using namespace l; +using namespace serialization; + +class TestData : public SerializationBase { +public: + TestData(int32_t version, int a, bool useVersion) : SerializationBase(version, 5, useVersion, false), mA(a) {} + virtual ~TestData() = default; + + friend zpp::serializer::access; + virtual void Save(SaveArchive& archive) const { + archive(mA); + } + virtual void Load(LoadArchive& archive) { + archive(mA); + } + + virtual void Upgrade(int32_t) { + + } + + int mA; +}; + +class Config3 : public SerializationBase { +public: + Config3() : SerializationBase(0, 0), mA{} {} + Config3(int32_t version, int a) : SerializationBase(version, version), mA(a) {} + virtual ~Config3() = default; + + friend zpp::serializer::access; + virtual void Save(SaveArchive& archive) const { + archive(mA); + } + virtual void Load(LoadArchive& archive) { + archive(mA); + } + + virtual void Upgrade(int32_t) { + + } + + int mA; +}; + +class Config2 : public SerializationBase { +public: + static const int32_t LatestVersion = 2; + + Config2() : SerializationBase(0, LatestVersion), mId{}, mNumber{}, mId2{} {} + Config2(int32_t version, std::string id, uint64_t number, std::string id2) : + SerializationBase(version, LatestVersion), + mId(id), + mNumber(number), + mId2(id2) + {} + virtual ~Config2() = default; + + virtual void Save(SaveArchive& archive) const { + archive(mId, mData, mNumber, mId2, mConfig); + } + friend zpp::serializer::access; + virtual void Load(LoadArchive& archive) { + archive(mId, mData, mNumber, mId2, mConfig); + } + + virtual void Upgrade(int32_t) { + + } + + std::string mId; + std::vector mData; + uint64_t mNumber; + std::string mId2; + Config3 mConfig; +}; + +TEST(SerializationBase, Basics) { + + std::vector dataNoVersion = { 0xa, 0x0, 0x0, 0x0 }; + std::vector dataOnlyVersion = { 0x5, 0x0, 0x0, 0x0, 0xb, 0x0, 0x0, 0x0 }; + std::vector dataHeaderAndVersion; + + { // load simple data + TestData storage(3, 10, false); + storage.LoadArchiveData(dataNoVersion); + TEST_TRUE(dataNoVersion.empty(), ""); + TEST_TRUE(storage.GetVersion() == 5, ""); + TEST_TRUE(storage.mA == 10, ""); + + // serialize and verify header + storage.GetArchiveData(dataHeaderAndVersion); + TEST_TRUE(dataHeaderAndVersion.size() == 12, ""); + TEST_TRUE(dataHeaderAndVersion.at(0) == 0, ""); + TEST_TRUE(dataHeaderAndVersion.at(1) == 0xfa, ""); + TEST_TRUE(dataHeaderAndVersion.at(2) == 0xde, ""); + TEST_TRUE(dataHeaderAndVersion.at(3) == 0, ""); + + TestData storage2(0, 0, false); + storage2.LoadArchiveData(dataHeaderAndVersion); + TEST_TRUE(storage2.GetVersion() == 5, ""); + TEST_TRUE(storage2.mA == 10, ""); + + // serialize and verify header + storage2.GetArchiveData(dataHeaderAndVersion); + TEST_TRUE(dataHeaderAndVersion.size() == 12, ""); + TEST_TRUE(dataHeaderAndVersion.at(0) == 0, ""); + TEST_TRUE(dataHeaderAndVersion.at(1) == 0xfa, ""); + TEST_TRUE(dataHeaderAndVersion.at(2) == 0xde, ""); + TEST_TRUE(dataHeaderAndVersion.at(3) == 0, ""); + dataHeaderAndVersion.clear(); + } + { // have to explicitly set use version to true for versioned only data + TestData storage(0, 0, true); + storage.LoadArchiveData(dataOnlyVersion); + TEST_TRUE(dataOnlyVersion.empty(), ""); + TEST_TRUE(storage.GetVersion() == 5, ""); + TEST_TRUE(storage.mA == 11, ""); + + // serialize and verify header + storage.GetArchiveData(dataHeaderAndVersion); + TEST_TRUE(dataHeaderAndVersion.size() == 12, ""); + TEST_TRUE(dataHeaderAndVersion.at(0) == 0, ""); + TEST_TRUE(dataHeaderAndVersion.at(1) == 0xfa, ""); + TEST_TRUE(dataHeaderAndVersion.at(2) == 0xde, ""); + TEST_TRUE(dataHeaderAndVersion.at(3) == 0, ""); + dataHeaderAndVersion.clear(); + } + + + return 0; +} + + + +class point { +public: + point() = default; + point(int x, int y, std::string str) noexcept : m_x(x), m_y(y), m_str(str) {} + + friend zpp::serializer::access; + template + static void serialize(Archive& archive, Self& self) { + archive(self.m_x, self.m_y, self.m_str); + } + + int get_x() const noexcept { + return m_x; + } + + int get_y() const noexcept { + return m_y; + } + +private: + int m_x = 0; + int m_y = 0; + std::string m_str; +}; + +TEST(Serialization, ZppSerializer) { + std::vector data; + zpp::serializer::memory_output_archive out(data); + + std::stringstream stream; + auto path = std::filesystem::path(L"./Debug/store/test_serialization.txt"); + + { + out(point(1337, 1338, "abc")); + out(point(2, 3, "4")); + out(point(5, 6, "7")); + + serialization::convert(stream, data); + } + + data.clear(); + + { + serialization::convert(data, stream); + + point my_point0; + point my_point1; + point my_point2; + + zpp::serializer::memory_input_archive in(data); + in(my_point0, my_point1, my_point2); + + std::cout << my_point2.get_x() << ' ' << my_point2.get_y() << '\n'; + } + + return 0; +} diff --git a/packages/serialization/tests/common/SerializationTest.cpp b/packages/serialization/tests/common/SerializationTest.cpp new file mode 100644 index 00000000..6d728e9e --- /dev/null +++ b/packages/serialization/tests/common/SerializationTest.cpp @@ -0,0 +1,7 @@ +#include "testing/Test.h" + +int main(int, char* argw[]) { + TEST_RUN(argw[0]); + + return 0; +} diff --git a/packages/storage/CMakeLists.txt b/packages/storage/CMakeLists.txt index 5379b340..4c668586 100644 --- a/packages/storage/CMakeLists.txt +++ b/packages/storage/CMakeLists.txt @@ -4,6 +4,8 @@ set(deps various logging testing + serialization + filesystem memory concurrency diff --git a/packages/storage/include/storage/LocalStore.h b/packages/storage/include/storage/LocalStore.h index 3f87daf4..ffcdd0dd 100644 --- a/packages/storage/include/storage/LocalStore.h +++ b/packages/storage/include/storage/LocalStore.h @@ -1,7 +1,7 @@ #pragma once #include "memory/Containers.h" -#include "various/serializer/Serializer.h" +#include "serialization/SerializationBase.h" #include "filesystem/File.h" #include @@ -16,235 +16,17 @@ namespace l { namespace storage { - template - void convert(std::stringstream& dst, std::vector& src, size_t count = 0) { - static_assert(sizeof(T) == sizeof(char)); - dst.write(reinterpret_cast(src.data()), count > 0 ? count : src.size()); - } - - template - void convert(std::stringstream& dst, std::array& src, size_t count = 0) { - static_assert(sizeof(T) == sizeof(char)); - dst.write(reinterpret_cast(src.data()), count > 0 ? count : src.size()); - } - - template - void convert(std::vector& dst, std::stringstream& src) { - T tmp{}; - static_assert(sizeof(T) == sizeof(char)); - while (src >> tmp) dst.push_back(tmp); - } - bool read(const std::filesystem::path& mLocation, char* dst, size_t count, size_t position = 0); bool write(const std::filesystem::path& mLocation, const char* src, size_t count, size_t position = 0); bool read(const std::filesystem::path& mLocation, std::stringstream& data); bool write(const std::filesystem::path& mLocation, std::stringstream& data); - struct HeaderValidity { - static const int32_t kIdentifier; - - friend zpp::serializer::access; - template - static void serialize(Archive& archive, Self& self) { - archive(self.mIdentifier, self.mVersion); - } - - bool Peek(std::vector& data) { - if (data.size() < 4*2) { // identifier + version = 8 bytes - return false; - } - zpp::serializer::memory_input_archive_peek inPeek(data); - inPeek(*this); - return true; - } - - bool IsIdentifierValid() { - return mIdentifier == kIdentifier; - } - bool IsVersionValid(int32_t latestVersion) { - if (mIdentifier != kIdentifier) { - mVersion = mIdentifier; // we might have loaded data without identifier but with version, so check - } - return mVersion >= 0 && mVersion <= latestVersion; - } - - int32_t mIdentifier; - int32_t mVersion; - }; - - class SerializationBase : public zpp::serializer::polymorphic { - public: - using SaveArchive = zpp::serializer::archive; - using LoadArchive = zpp::serializer::archive; - - SerializationBase() : - mIdentifier(0), - mVersion(0), - mLatestVersion(mVersion), - mUseIdentifier(false), - mUseVersion(false), - mUseFiletype(false) - {} - SerializationBase(int32_t minimumVersion, int32_t latestVersion, bool useVersion = true, bool useFiletype = false) : - mIdentifier(0), - mVersion(minimumVersion), - mLatestVersion(latestVersion), - mUseIdentifier(false), - mUseVersion(useVersion), - mUseFiletype(useFiletype) { - if (useVersion) { - ASSERT(minimumVersion <= mLatestVersion); - } - } - virtual ~SerializationBase() = default; - - SerializationBase& operator=(SerializationBase&& other) noexcept { - mIdentifier = other.mIdentifier; - mVersion = other.mVersion; - mLatestVersion = other.mLatestVersion; - mFiletype = other.mFiletype; - mUseIdentifier = other.mUseIdentifier; - mUseVersion = other.mUseVersion; - mUseFiletype = other.mUseFiletype; - return *this; - } - SerializationBase& operator=(const SerializationBase& other) noexcept { - mIdentifier = other.mIdentifier; - mVersion = other.mVersion; - mLatestVersion = other.mLatestVersion; - mFiletype = other.mFiletype; - mUseIdentifier = other.mUseIdentifier; - mUseVersion = other.mUseVersion; - mUseFiletype = other.mUseFiletype; - return *this; - } - SerializationBase(SerializationBase&& other) noexcept { - *this = std::move(other); - } - SerializationBase(const SerializationBase& other) noexcept { - *this = other; - } - - void LoadArchiveData(std::vector& data) { - HeaderValidity headerValidity; - bool peekSuccessful = headerValidity.Peek(data); - if (peekSuccessful && headerValidity.IsIdentifierValid()) { - mUseIdentifier = true; - mUseVersion = true; - } - else { - mUseIdentifier = false; - if (mUseVersion) { - ASSERT(peekSuccessful && headerValidity.IsVersionValid(mLatestVersion)); - } - } - - zpp::serializer::memory_input_archive in(data); - in(*this); - - mUseIdentifier = true; - mUseVersion = true; - } - - void GetArchiveData(std::vector& data) { - zpp::serializer::memory_output_archive out(data); - out(*this); - } - - friend zpp::serializer::access; - template - static void serialize(Archive& archive, Self& self) { - if constexpr (std::is_base_of{}) { - int32_t* p = const_cast(&self.mVersion); - *p = self.mLatestVersion; - - if (self.mUseIdentifier) { - archive(HeaderValidity::kIdentifier); - } - if (self.mUseVersion) { - archive(self.mVersion); - } - if (self.mUseFiletype) { - archive(self.mFiletype); - } - self.Save(archive); - } - if constexpr (std::is_base_of{}) { - if (self.mUseIdentifier) { - int32_t fileIdentifier; - archive(fileIdentifier); - ASSERT(fileIdentifier == HeaderValidity::kIdentifier); - } - if (self.mUseVersion) { - archive(self.mVersion); - } - if (self.mUseFiletype) { - archive(self.mFiletype); - } - self.Load(archive); - self.UpgradeToLatest(); - } - } - - int32_t GetVersion() const { - return mVersion; - } - - protected: - virtual void Save(SaveArchive&) const {} - virtual void Load(LoadArchive&) {} - virtual void Upgrade(int32_t) {} - - int32_t mIdentifier = 0; - int32_t mVersion = 0; - std::string mFiletype; - - int32_t mLatestVersion = 0; - bool mUseIdentifier = true; - bool mUseVersion = true; - bool mUseFiletype = false; - - void UpgradeToLatest() { - mLatestVersion = mVersion > mLatestVersion ? mVersion : mLatestVersion; - if (mUseVersion) { - for (; mVersion < mLatestVersion; mVersion++) { - Upgrade(mVersion); - } - } - mVersion = mVersion < mLatestVersion ? mLatestVersion : mVersion; - } - }; - - class Config : public SerializationBase { - public: - static const int32_t LatestVersion = 1; - - Config() : SerializationBase(0, LatestVersion), mName{}, mDefaultUserId{} {} - Config(int32_t version, std::string name, std::string defaultUserId) : SerializationBase(version, LatestVersion), mName(name), mDefaultUserId(defaultUserId) {} - virtual ~Config() = default; - - friend zpp::serializer::access; - virtual void Save(SaveArchive& archive) const { - archive(mName, mDefaultUserId); - } - virtual void Load(LoadArchive& archive) { - archive(mName, mDefaultUserId); - } - - virtual void Upgrade(int32_t) { - - } - - std::string mName; - std::string mDefaultUserId; - }; - class LocalStorage { public: LocalStorage(std::filesystem::path&& location); - template> + template> void Set(const std::string& key, std::unique_ptr value) { mStore.emplace(key, std::move(value)); } @@ -262,7 +44,7 @@ namespace storage { return nullptr; } - template> + template> bool Save(const std::string& key) { T* data = Get(key); std::vector bytes; @@ -285,7 +67,7 @@ namespace storage { return true; } - template> + template> bool Load(const std::string& key) { T* data = Get(key); if (data == nullptr) { @@ -321,7 +103,7 @@ namespace storage { void CreatePath(); std::filesystem::path mLocation; - std::map> mStore; + std::map> mStore; }; } diff --git a/packages/storage/source/common/LocalStore.cpp b/packages/storage/source/common/LocalStore.cpp index dc9cd751..f8a1767d 100644 --- a/packages/storage/source/common/LocalStore.cpp +++ b/packages/storage/source/common/LocalStore.cpp @@ -14,8 +14,6 @@ namespace l { namespace storage { - const int32_t HeaderValidity::kIdentifier = 0x00defa00; // storage base file identifier - std::atomic_int32_t count_reads_ops = 0; std::atomic_int32_t count_write_ops = 0; std::atomic_bool abort_all_reads = false; @@ -71,7 +69,7 @@ namespace storage { break; } - storage::convert(data, buf, count); + serialization::convert(data, buf, count); } ifs.close(); count_reads_ops--; diff --git a/packages/storage/tests/common/LocalStoreTest.cpp b/packages/storage/tests/common/LocalStoreTest.cpp index 1820d190..decf4354 100644 --- a/packages/storage/tests/common/LocalStoreTest.cpp +++ b/packages/storage/tests/common/LocalStoreTest.cpp @@ -1,33 +1,15 @@ #include "testing/Test.h" +#include "serialization/SerializationBase.h" #include "storage/LocalStore.h" #include #include using namespace l; +using namespace serialization; using namespace storage; -class Storage : public SerializationBase { -public: - Storage(int32_t version, int a, bool useVersion) : SerializationBase(version, 5, useVersion, false), mA(a) {} - virtual ~Storage() = default; - - friend zpp::serializer::access; - virtual void Save(SaveArchive& archive) const { - archive(mA); - } - virtual void Load(LoadArchive& archive) { - archive(mA); - } - - virtual void Upgrade(int32_t) { - - } - - int mA; -}; - class Config3 : public SerializationBase { public: Config3() : SerializationBase(0, 0), mA{} {} @@ -81,63 +63,6 @@ class Config2 : public SerializationBase { Config3 mConfig; }; -TEST(SerializationBase, Basics) { - - std::vector dataNoVersion = { 0xa, 0x0, 0x0, 0x0 }; - std::vector dataOnlyVersion = { 0x5, 0x0, 0x0, 0x0, 0xb, 0x0, 0x0, 0x0 }; - std::vector dataHeaderAndVersion; - - { // load simple data - Storage storage(3, 10, false); - storage.LoadArchiveData(dataNoVersion); - TEST_TRUE(dataNoVersion.empty(), ""); - TEST_TRUE(storage.GetVersion() == 5, ""); - TEST_TRUE(storage.mA == 10, ""); - - // serialize and verify header - storage.GetArchiveData(dataHeaderAndVersion); - TEST_TRUE(dataHeaderAndVersion.size() == 12, ""); - TEST_TRUE(dataHeaderAndVersion.at(0) == 0, ""); - TEST_TRUE(dataHeaderAndVersion.at(1) == 0xfa, ""); - TEST_TRUE(dataHeaderAndVersion.at(2) == 0xde, ""); - TEST_TRUE(dataHeaderAndVersion.at(3) == 0, ""); - - Storage storage2(0, 0, false); - storage2.LoadArchiveData(dataHeaderAndVersion); - TEST_TRUE(storage2.GetVersion() == 5, ""); - TEST_TRUE(storage2.mA == 10, ""); - - // serialize and verify header - storage2.GetArchiveData(dataHeaderAndVersion); - TEST_TRUE(dataHeaderAndVersion.size() == 12, ""); - TEST_TRUE(dataHeaderAndVersion.at(0) == 0, ""); - TEST_TRUE(dataHeaderAndVersion.at(1) == 0xfa, ""); - TEST_TRUE(dataHeaderAndVersion.at(2) == 0xde, ""); - TEST_TRUE(dataHeaderAndVersion.at(3) == 0, ""); - dataHeaderAndVersion.clear(); - } - { // have to explicitly set use version to true for versioned only data - Storage storage(0, 0, true); - storage.LoadArchiveData(dataOnlyVersion); - TEST_TRUE(dataOnlyVersion.empty(), ""); - TEST_TRUE(storage.GetVersion() == 5, ""); - TEST_TRUE(storage.mA == 11, ""); - - // serialize and verify header - storage.GetArchiveData(dataHeaderAndVersion); - TEST_TRUE(dataHeaderAndVersion.size() == 12, ""); - TEST_TRUE(dataHeaderAndVersion.at(0) == 0, ""); - TEST_TRUE(dataHeaderAndVersion.at(1) == 0xfa, ""); - TEST_TRUE(dataHeaderAndVersion.at(2) == 0xde, ""); - TEST_TRUE(dataHeaderAndVersion.at(3) == 0, ""); - dataHeaderAndVersion.clear(); - } - - - return 0; -} - - TEST(Storage, Serialization) { auto path = std::filesystem::path(L"./Debug"); diff --git a/packages/tools/tests/common/SerializationTest.cpp b/packages/tools/tests/common/SerializationTest.cpp deleted file mode 100644 index 5079118b..00000000 --- a/packages/tools/tests/common/SerializationTest.cpp +++ /dev/null @@ -1,68 +0,0 @@ -#include "testing/Test.h" -#include "logging/Log.h" - -#include "storage/LocalStore.h" -#include "various/serializer/Serializer.h" - -using namespace l; - -class point { -public: - point() = default; - point(int x, int y, std::string str) noexcept : m_x(x), m_y(y), m_str(str) {} - - friend zpp::serializer::access; - template - static void serialize(Archive& archive, Self& self) { - archive(self.m_x, self.m_y, self.m_str); - } - - int get_x() const noexcept { - return m_x; - } - - int get_y() const noexcept { - return m_y; - } - -private: - int m_x = 0; - int m_y = 0; - std::string m_str; -}; - -TEST(Serialization, ZppSerializer) { - std::vector data; - zpp::serializer::memory_output_archive out(data); - - std::stringstream stream; - auto path = std::filesystem::path(L"./Debug/store/test_serialization.txt"); - - { - out(point(1337, 1338, "abc")); - out(point(2, 3, "4")); - out(point(5, 6, "7")); - - storage::convert(stream, data); - storage::write(path, stream); - } - - stream.clear(); - data.clear(); - - { - storage::read(path, stream); - storage::convert(data, stream); - - point my_point0; - point my_point1; - point my_point2; - - zpp::serializer::memory_input_archive in(data); - in(my_point0, my_point1, my_point2); - - std::cout << my_point2.get_x() << ' ' << my_point2.get_y() << '\n'; - } - - return 0; -}