diff --git a/code/c++/tests/test_cereal.cpp b/code/c++/tests/test_cereal.cpp new file mode 100644 index 0000000..688a0d9 --- /dev/null +++ b/code/c++/tests/test_cereal.cpp @@ -0,0 +1,83 @@ + +#include +#include +#include + +#include "cereal/archives/json.hpp" +#include "cereal/types/memory.hpp" +#include "cereal/types/string.hpp" +#include "cereal/types/unordered_map.hpp" + +using namespace std; + +struct MyRecord { + int x, y; + float z; + + template + void serialize(Archive& ar) { + ar(CEREAL_NVP(x), CEREAL_NVP(y), CEREAL_NVP(z)); + } + + friend std::ostream& operator<<(std::ostream& os, const MyRecord& mr); +}; + +std::ostream& operator<<(std::ostream& os, const MyRecord& mr) { + os << "MyRecord(" << mr.x << ", " << mr.y << "," << mr.z << ")\n"; + return os; +} + +struct SomeData { + int32_t id; + std::shared_ptr> data; + + SomeData(int32_t id_ = 0) : id(id_), data(new std::unordered_map) {} + + template + void save(Archive& ar) const { + ar(CEREAL_NVP(id), CEREAL_NVP(data)); + } + + template + void load(Archive& ar) { + ar(id, data); + } + + void push(uint32_t, const MyRecord& mr) { data->insert(std::make_pair(100, mr)); } + + void print() { + std::cout << "ID : " << id << "\n"; + if (data->empty()) return; + for (auto& item : *data) { + std::cout << item.first << "\t" << item.second << "\n"; + } + } +}; + +void test() { + { + std::ofstream os("obj.json"); + cereal::JSONOutputArchive archive(os); + + MyRecord mr = {1, 2, 3.0}; + + SomeData myData(1111); + myData.push(100, mr); + + archive(myData); + } + { + std::ifstream is("obj.json"); + cereal::JSONInputArchive archive(is); + + SomeData myData; + archive(myData); + myData.print(); + } +} + +int main() { + test(); + + return 0; +} \ No newline at end of file diff --git a/code/c++/tests/xmake.lua b/code/c++/tests/xmake.lua new file mode 100644 index 0000000..6e14305 --- /dev/null +++ b/code/c++/tests/xmake.lua @@ -0,0 +1,27 @@ +add_requires("raylib") +add_requires("catch2") +add_requires("spdlog") +add_requires("cereal") + +target("test_raylib") + set_kind("binary") + add_files("test_raylib.cpp") + add_packages("raylib") + + +target("test_catch2") + set_kind("binary") + add_files("test_catch2.cpp") + add_packages("catch2") + + +target("test_spdlog") + set_kind("binary") + add_files("test_spdlog.cpp") + add_packages("spdlog") + +target("test_cereal") + set_kind("binary") + add_files("test_cereal.cpp") + add_packages("cereal") + set_languages("c++17") \ No newline at end of file diff --git a/code/c++/xmake.lua b/code/c++/xmake.lua index 7f6c978..0e4c8b3 100644 --- a/code/c++/xmake.lua +++ b/code/c++/xmake.lua @@ -1,36 +1,18 @@ -add_requires("raylib") -add_requires("catch2") -add_requires("spdlog") + add_repositories("my-repo myrepo") add_requires("raylibx") add_rules("mode.debug", "mode.release") -target("test_raylib") - set_kind("binary") - add_files("tests/test_raylib.cpp") - add_packages("raylib") - - -target("test_catch2") - set_kind("binary") - add_files("tests/test_catch2.cpp") - add_packages("catch2") - - -target("test_spdlog") - set_kind("binary") - add_files("tests/test_spdlog.cpp") - add_packages("spdlog") - +includes("tests/") includes("src/") -target("test_jsoncpp") - set_kind("binary") - add_files("tmp.cpp") - add_packages("raylibx") +-- target("test_jsoncpp") +-- set_kind("binary") +-- add_files("tmp.cpp") +-- add_packages("raylibx")