Created
May 24, 2020 14:18
-
-
Save NotAdam/c72631584e542a51ae4cae71534b3b43 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <cereal/cereal.hpp> | |
#include <cereal/archives/binary.hpp> | |
#include <cereal/types/base_class.hpp> | |
#include <cereal/types/memory.hpp> | |
#include <cereal/types/vector.hpp> | |
#include <string> | |
#include <vector> | |
#include <memory> | |
#include <iostream> | |
#include <fstream> | |
enum SceneObjectType : uint32_t | |
{ | |
None, | |
Test | |
}; | |
struct SceneObject | |
{ | |
SceneObjectType type{ None }; | |
std::string name; | |
template< class Archive > | |
void serialize( Archive& ar ) | |
{ | |
ar( type, name ); | |
} | |
}; | |
struct TestSceneObject : | |
public SceneObject | |
{ | |
std::string eventName; | |
int test; | |
template< class Archive > | |
void serialize( Archive& ar ) | |
{ | |
ar( | |
cereal::base_class< SceneObject >( this ), | |
eventName, | |
test | |
); | |
} | |
}; | |
struct SceneGroup | |
{ | |
std::string name; | |
std::vector< std::shared_ptr< SceneObject > > objects; | |
template< class Archive > | |
void serialize( Archive& ar ) | |
{ | |
ar( name, objects ); | |
} | |
}; | |
int main() | |
{ | |
{ | |
std::ofstream os( "struct_test.bin", std::ios::binary ); | |
cereal::BinaryOutputArchive archive( os ); | |
auto sceneGroup = std::make_shared< SceneGroup >(); | |
sceneGroup->name = "test group 1"; | |
auto obj1 = std::make_shared< SceneObject >(); | |
obj1->name = "regular sceneobject"; | |
auto obj2 = std::make_shared< TestSceneObject >(); | |
obj2->type = SceneObjectType::Test; | |
obj2->name = "test sceneobject"; | |
obj2->eventName = "hello world"; | |
obj2->test = 1234; | |
sceneGroup->objects.push_back( obj1 ); | |
sceneGroup->objects.push_back( obj2 ); | |
archive( sceneGroup ); | |
} | |
{ | |
std::ifstream is( "struct_test.bin", std::ios::binary ); | |
cereal::BinaryInputArchive archive( is ); | |
std::shared_ptr< SceneGroup > group; | |
archive( group ); | |
std::cout << "group: " << group->name << std::endl; | |
for( auto& obj : group->objects ) | |
{ | |
switch( obj->type ) | |
{ | |
default: | |
std::cout << " - SceneObject: " << obj->name << std::endl; | |
break; | |
case Test: | |
auto test = std::reinterpret_pointer_cast< TestSceneObject >( obj ); | |
std::cout << " - TestSceneObject: " << test->name << std::endl; | |
std::cout << " test val: " << test->test << std::endl; | |
break; | |
} | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment