Created
September 12, 2021 12:46
-
-
Save Pikachuxxxx/26229a08a8be7639d04890fd2d253c93 to your computer and use it in GitHub Desktop.
Test code with cereal serialisation library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
// Std. Libraries | |
#include <fstream> | |
// Cereal Includes | |
#include <cereal/types/unordered_map.hpp> | |
#include <cereal/types/memory.hpp> | |
#include <cereal/archives/json.hpp> | |
// glm | |
// GLM | |
#include <glm/glm.hpp> | |
#include <glm/gtc/matrix_transform.hpp> | |
#include <glm/gtc/type_ptr.hpp> | |
#include <glm/gtx/string_cast.hpp> | |
namespace glm | |
{ | |
template<class Archive> void serialize(Archive& archive, glm::vec2& v) { archive(v.x, v.y); } | |
template<class Archive> void serialize(Archive& archive, glm::vec3& v) { archive(cereal::make_nvp("x", v.x), cereal::make_nvp("y", v.y), cereal::make_nvp("z", v.z)); } | |
template<class Archive> void serialize(Archive& archive, glm::vec4& v) { archive(v.x, v.y, v.z, v.w); } | |
template<class Archive> void serialize(Archive& archive, glm::ivec2& v) { archive(v.x, v.y); } | |
template<class Archive> void serialize(Archive& archive, glm::ivec3& v) { archive(v.x, v.y, v.z); } | |
template<class Archive> void serialize(Archive& archive, glm::ivec4& v) { archive(v.x, v.y, v.z, v.w); } | |
template<class Archive> void serialize(Archive& archive, glm::uvec2& v) { archive(v.x, v.y); } | |
template<class Archive> void serialize(Archive& archive, glm::uvec3& v) { archive(v.x, v.y, v.z); } | |
template<class Archive> void serialize(Archive& archive, glm::uvec4& v) { archive(v.x, v.y, v.z, v.w); } | |
template<class Archive> void serialize(Archive& archive, glm::dvec2& v) { archive(v.x, v.y); } | |
template<class Archive> void serialize(Archive& archive, glm::dvec3& v) { archive(v.x, v.y, v.z); } | |
template<class Archive> void serialize(Archive& archive, glm::dvec4& v) { archive(v.x, v.y, v.z, v.w); } | |
// glm matrices | |
template<class Archive> void serialize(Archive& archive, glm::mat2& m) { archive(m[0], m[1]); } | |
template<class Archive> void serialize(Archive& archive, glm::dmat2& m) { archive(m[0], m[1]); } | |
template<class Archive> void serialize(Archive& archive, glm::mat3& m) { archive(m[0], m[1], m[2]); } | |
template<class Archive> void serialize(Archive& archive, glm::mat4& m) { archive(m[0], m[1], m[2], m[3]); } | |
template<class Archive> void serialize(Archive& archive, glm::dmat4& m) { archive(m[0], m[1], m[2], m[3]); } | |
template<class Archive> void serialize(Archive& archive, glm::quat& q) { archive(q.x, q.y, q.z, q.w); } | |
template<class Archive> void serialize(Archive& archive, glm::dquat& q) { archive(q.x, q.y, q.z, q.w); } | |
} | |
// Entity count | |
int g_EntityCount = 0; | |
// Testing cerialization for a custom data strcuture | |
enum class Material | |
{ | |
DIFFUSE, | |
METAL, | |
SPECULAR | |
}; | |
struct Sphere | |
{ | |
glm::vec3 position; | |
float radius; | |
float something[2]; | |
glm::vec3 color; | |
Material sphereMat; | |
// This method lets cereal know which data members to serialize | |
// template<class Archive> | |
// void serialize(Archive& archive) | |
// { | |
// archive(cereal::make_nvp("Position", position)); | |
// archive(cereal::make_nvp("Radius", radius)); | |
// archive(cereal::make_nvp("Color", color)); | |
// archive(cereal::make_nvp("Material", sphereMat)); | |
// } | |
template<class Archive> | |
void save(Archive& archive) const | |
{ | |
g_EntityCount += 1; | |
archive(cereal::make_nvp("Position", position)); | |
archive(cereal::make_nvp("Radius", radius)); | |
archive(cereal::make_nvp("Color", color)); | |
archive(cereal::make_nvp("Material", sphereMat)); | |
} | |
template<class Archive> | |
void load(Archive& archive) | |
{ | |
archive(cereal::make_nvp("Position", position)); | |
archive(cereal::make_nvp("Radius", radius)); | |
archive(cereal::make_nvp("Color", color)); | |
archive(cereal::make_nvp("Material", sphereMat)); | |
} | |
}; | |
int main() { | |
// Fill the cerialization data | |
Sphere redSphere; | |
redSphere.position = glm::vec3(1.0f, 0.56f, 0.88f); | |
redSphere.radius = 3.30f; | |
redSphere.something[0] = 2.2f; | |
redSphere.something[1] = 98.2f; | |
redSphere.color = glm::vec3(1.0f, 0.0f, 0.0f); | |
redSphere.sphereMat = Material::METAL; | |
// Create the JSON serializer (make sure it's destroyed by RAII, hence use a scope for auto desctruction) | |
{ | |
std::ofstream os("./data/Spheres.cornflakes"); | |
cereal::JSONOutputArchive out_archive(os); | |
// out_archive(CEREAL_NVP(redSphere), redSphere, cereal::make_nvp("Somedata", 2.2f)); | |
out_archive(CEREAL_NVP(redSphere)); | |
redSphere.color = glm::vec3(0.0f, 1.0f, 0.0f); | |
out_archive(cereal::make_nvp("Green Sphere", redSphere)); | |
redSphere.color = glm::vec3(0.0f, 1.0f, 1.0f); | |
redSphere.sphereMat = Material::SPECULAR; | |
out_archive(redSphere); | |
out_archive(cereal::make_nvp("EntityCount", g_EntityCount)); | |
} | |
// Load from the JSON file | |
{ | |
std::ifstream is("./data/Spheres.cornflakes"); | |
cereal::JSONInputArchive archive(is); | |
Sphere greenSphere; | |
Sphere RedSphereLoad; | |
archive(cereal::make_nvp("EntityCount", g_EntityCount)); | |
archive(cereal::make_nvp("Green Sphere", greenSphere)); // Now cereal starts loading from the node this nvp was loaded from | |
archive(redSphere); | |
archive(cereal::make_nvp("redSphere", RedSphereLoad)); // resets to first node again | |
archive(greenSphere); | |
archive(greenSphere); | |
// archive(g_EntityCount); | |
// Testing the loaded data | |
std::cout << glm::to_string(greenSphere.color) << std::endl; | |
std::cout << glm::to_string(RedSphereLoad.color) << std::endl; | |
std::cout << "Entity Count : " << g_EntityCount << std::endl; | |
} | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment