Skip to content

C++ serialization library for storing and retrieving data

Notifications You must be signed in to change notification settings

emd22/fx-serialize

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Foxtrot Serialize

Warning

This library is designed for personal use, so there may be unforeseen bugs and issues!

A small serialization library for C++, created for foxtrot.

This is C++20 only due to the usage of concepts, but could be easily modified for older versions of C++.

Usage

To mark values in a struct as serializable, use the FX_SERIALIZABLE_MEMBERS macro.

struct ExampleStruct
{
    int SomeValue;
    float32 SomeFloat;
    std::string SomeString = "Hello, World";

    FX_SERIALIZABLE_MEMBERS(SomeValue, SomeFloat, SomeString);
};

Then, to serialize an instance of an object:

FxSerializerIO writer;

ExampleStruct example { 10, 20.0f };
example.WriteTo(FxHashStr("NameOfObject"), writer);

Which can later be read using a similar method:

FxSerializerIO reader;

ExampleStruct read_data; // Ensure that ExampleStruct
read_data.ReadFrom(FxHashStr("NameOfObject"), reader);

As well, you can serialize nested objects:

struct Vec3f
{
    int32 X, Y, Z;
    FX_SERIALIZABLE_MEMBERS(X, Y, Z);
};

struct Player
{
    std::string Name;
    Vec3f Position;
    Vec3f Rotation;

    FX_SERIALIZABLE_MEMBERS(Name, Position, Rotation);
};

void SaveData()
{
    FxSerializerIO writer;
    
    Player player{};
    // ... Set player values ...
    player.WriteTo(FxHashStr("MainPlayer"), writer);

    writer.SaveToFile("PlayerSave.fxsd");
}

File Input/Output

The current state in FxSerializerIO can be written and read from a file to the types and data.

Writing to a file:

FxSerializerIO writer;
writer.WriteToFile("MyFavoriteStruct.fxsd");

And similarly for reading:

FxSerializerIO reader;
reader.ReadFromFile("MyFavoriteStruct.fxsd");

Building the Example

cc -std=c++20 FxSerializer.cpp Example.cpp
./a.out

About

C++ serialization library for storing and retrieving data

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages