Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to serialize complicate struct? #57

Closed
linuxaged opened this issue Jan 9, 2020 · 1 comment
Closed

How to serialize complicate struct? #57

linuxaged opened this issue Jan 9, 2020 · 1 comment

Comments

@linuxaged
Copy link

i have a struct Group like this:

struct vec3 {
	float x, y, z;
};

struct mat4 {
	float d[16];
};

struct Vertex {
	vec3 position;
	vec3 normal;
};

struct Mesh {
	cista::raw::vector<mat4> transforms;
	cista::raw::vector<Vertex> vertices;
};
using Meshes = cista::raw::vector<Mesh>;

struct Staging {
	int stepIndex;
	cista::raw::vector<float> distances;
	cista::raw::vector<vec3> translations;
	cista::raw::vector<vec3> rotations;
};
using Stagings = cista::raw::vector<Staging>;

struct Group {
	Meshes meshes;
	Stagings stagings;
}

have not found any similar example.

@felixguendling
Copy link
Owner

felixguendling commented Jan 9, 2020

The only struct that requires custom serialization, deserialization (and hash if you want to use type hashing for versioning) is mat4 because C arrays are not supported. But I would just use cista::array<float, 16> which has exactly the same properties as a C array (inside, it is a C array) but it's supported by Cista out of the box.

With this change, there is nothing you need to do. You can just call serialize and deserialize. I made a test case from your code:

cista/test/mesh_test.cc

Lines 13 to 79 in 2855eee

struct vec3 {
float x, y, z;
};
struct mat4 {
data::array<float, 16> d;
};
struct Vertex {
vec3 position;
vec3 normal;
};
struct Mesh {
data::vector<mat4> transforms;
data::vector<Vertex> vertices;
};
using Meshes = data::vector<Mesh>;
struct Staging {
int stepIndex;
data::vector<float> distances;
data::vector<vec3> translations;
data::vector<vec3> rotations;
};
using Stagings = data::vector<Staging>;
struct Group {
Meshes meshes;
Stagings stagings;
};
TEST_CASE("mesh test") {
constexpr auto const FILENAME = "group.bin";
std::remove(FILENAME);
{
Group g;
Mesh m;
m.transforms.emplace_back(mat4{0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 0.0,
1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0});
g.meshes.emplace_back(m);
cista::buf<cista::mmap> mmap{cista::mmap{FILENAME}};
cista::serialize(mmap, g);
} // EOL graph
auto b = cista::file(FILENAME, "r").content();
auto const g = cista::deserialize<Group>(b);
CHECK(g->meshes[0].transforms[0].d[0] == 0.0);
CHECK(g->meshes[0].transforms[0].d[1] == 1.0);
CHECK(g->meshes[0].transforms[0].d[2] == 2.0);
CHECK(g->meshes[0].transforms[0].d[3] == 3.0);
CHECK(g->meshes[0].transforms[0].d[4] == 4.0);
CHECK(g->meshes[0].transforms[0].d[5] == 5.0);
CHECK(g->meshes[0].transforms[0].d[6] == 6.0);
CHECK(g->meshes[0].transforms[0].d[7] == 7.0);
CHECK(g->meshes[0].transforms[0].d[8] == 0.0);
CHECK(g->meshes[0].transforms[0].d[9] == 1.0);
CHECK(g->meshes[0].transforms[0].d[10] == 2.0);
CHECK(g->meshes[0].transforms[0].d[11] == 3.0);
CHECK(g->meshes[0].transforms[0].d[12] == 4.0);
CHECK(g->meshes[0].transforms[0].d[13] == 5.0);
CHECK(g->meshes[0].transforms[0].d[14] == 6.0);
CHECK(g->meshes[0].transforms[0].d[15] == 7.0);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants