basicpp17 / co-cpp19 Goto Github PK
View Code? Open in Web Editor NEWC++20 Library with the fastest runtime and compile times
License: MIT License
C++20 Library with the fastest runtime and compile times
License: MIT License
I'm using serialize19 to create and load binary files of structured data.
But if the file format changes, e.g. because data members have been added / removed, or if the file gets corrupted in some way, the archive reader might cause a read access exception which can't be caught without messing with signal handlers.
=> is there a clean and reliable way to detect and handle this condition without application crash?
#include "serialize19/ReadArchive.h"
#include "serialize19/dynamicWrite.h"
#include "serialize19/serialize.std_string.h"
struct OldFileFormat
{
int a{};
int b{};
std::string s{};
};
struct NewFileFormat
{
int a{};
int b{};
int c{};
std::string s{};
};
template<class A>
void serialize(A& a, OldFileFormat& x)
{
serialize(a, x.a);
serialize(a, x.b);
serialize(a, x.s);
}
template<class A>
void serialize(A& a, NewFileFormat& x)
{
serialize(a, x.a);
serialize(a, x.b);
serialize(a, x.c);
serialize(a, x.s);
}
void main()
{
OldFileFormat old_data{1, 2, "test"};
const auto buffer_old = serialize19::dynamicWrite(old_data);
// the buffer contents usually get saved into a binary file:
// std::ofstream fout{filename, std::ios::binary};
// fout.write(reinterpret_cast<const char*>(slice.begin()), slice.count());
//
// ... and later on we read the file back into a buffer slice via
// std::ifstream fin{filename, std::ios::binary};
// const auto file_buffer = std::vector<uint8_t>{std::istreambuf_iterator(fin), {}}
// const auto file_buffer_slize = serialize19::BufferSlice{file_buffer.data(), file_buffer.size()});
//
// here we just skip this step and hand over the already existing buffer slice
auto reader = serialize19::ReadArchive{buffer_old.slice()};
NewFileFormat new_data;
try
{
serialize(reader, new_data);
}
catch (const std::exception& ex)
{
// We want to catch all file read exceptions in a clean way but there is only a hardware exception being thrown which requires dirty signal handler
}
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.