CoriEngine
Loading...
Searching...
No Matches
Cori::FileSystem::BinaryFileManager Class Reference

Saves and loads aggregate structs to/from drive. More...

#include <BinaryFileManager.hpp>

Static Public Member Functions

template<typename T>
requires std::is_aggregate_v<T>
static void SaveAggregateStruct (const T &data, const std::filesystem::path &file, const bool safeMode=false)
 Saves an instance of an aggregate struct onto the disk.
template<typename T>
requires std::is_aggregate_v<T>
static std::expected< T, Core::CoriError<> > LoadAggregateStruct (const std::filesystem::path &file, const bool backupFallback=false)
 Loads an aggregate struct from the file on the disk.

Detailed Description

Saves and loads aggregate structs to/from drive.

You can load or save aggregate struct that contain trivial types or std::vector of any type.
It has some safety mechanisms to prevent file corruption or detect it afterward, also it will not allow you to load the data that came from one aggregate struct type into another.
1: When you create the file it generates 1 128bit UUID and writes it to the header and to the footer of the file, if during reading these UUIDs in footer and header mismatch, it means that the file is corrupted, it will try to look for a backup (.bak file), if it exists it will load it.
2: During compile time an aggregate struct UID is generated for every specialization of SaveAggregateStruct method, this aggregate struct UID is saved in the header of the file, later when loading it checks if this saved aggregate struct UID is the same as of the struct type you're trying to load the data in, if it detects missmatch, it will not load the data into that struct and instead look for a backup file.
3: When saving a file it calculates a checksum of the data, when loading the file it checks the saved checksum in the header to the checksum calculated from the read data, if it detects a mismatch it will look for a backup.
4: When a file is saved it saves total data size in the header, when reading the file it compares this saved value to the actual size of read data, if it detects a mismatch it will look for a backup.

Note
Aggregate struct UID is sensitive to the order of members in the struct and to the member types.

Definition at line 33 of file BinaryFileManager.hpp.

Member Function Documentation

◆ LoadAggregateStruct()

template<typename T>
requires std::is_aggregate_v<T>
std::expected< T, Core::CoriError<> > Cori::FileSystem::BinaryFileManager::LoadAggregateStruct ( const std::filesystem::path & file,
const bool backupFallback = false )
inlinestatic

Loads an aggregate struct from the file on the disk.

Template Parameters
TAggregate struct type to load into. Should be the same type you provided in SaveAggregateStruct when saving it.
Parameters
fileFile to read from.
backupFallbackWhether to look for a backup, or no.
Returns
Expected object with aggregate struct instance with loaded data, or CoriError on faliure.

Definition at line 111 of file BinaryFileManager.hpp.

Here is the call graph for this function:

◆ SaveAggregateStruct()

template<typename T>
requires std::is_aggregate_v<T>
void Cori::FileSystem::BinaryFileManager::SaveAggregateStruct ( const T & data,
const std::filesystem::path & file,
const bool safeMode = false )
inlinestatic

Saves an instance of an aggregate struct onto the disk.

Template Parameters
TAggregate struct type to save.
Parameters
dataInstance of the aggregate struct to save.
fileFile to save to.
safeModeSafe mode selector. When enabled it will create a backup after it writes the target file.

Definition at line 43 of file BinaryFileManager.hpp.

Here is the call graph for this function:

The documentation for this class was generated from the following file: