cista
v0.15
CISTA ++是一个简单的开源(MIT许可证)C ++ 17兼容(DE-)序列化C ++数据结构的方式。
单头 - 无依赖项。没有宏。没有源代码生成。
基本的反射机制也可以以其他方式使用!
示例:
下载最新版本并尝试一下。
简单的示例写入缓冲区:
namespace data = cista::raw;
struct my_struct { // Define your struct.
int a_{ 0 };
struct inner {
data::string b_;
} j;
};
std::vector< unsigned char > buf;
{ // Serialize.
my_struct obj{ 1 , {data::string{ " test " }}};
buf = cista::serialize (obj);
}
// Deserialize.
auto deserialized = cista::deserialize<my_struct>(buf);
assert (deserialized->j.b_ == data::string{ " test " });高级示例将哈希地图写入内存映射文件:
namespace data = cista::offset;
constexpr auto const MODE = // opt. versioning + check sum
cista::mode::WITH_VERSION | cista::mode::WITH_INTEGRITY;
struct pos { int x, y; };
using pos_map = // Automatic deduction of hash & equality
data::hash_map<data::vector<pos>,
data::hash_set<data::string>>;
{ // Serialize.
auto positions =
pos_map{{{{ 1 , 2 }, { 3 , 4 }}, { " hello " , " cista " }},
{{{ 5 , 6 }, { 7 , 8 }}, { " hello " , " world " }}};
cista::buf mmap{cista::mmap{ " data " }};
cista::serialize<MODE>(mmap, positions);
}
// Deserialize.
auto b = cista::mmap( " data " , cista::mmap::protection::READ);
auto positions = cista::deserialize<pos_map, MODE>(b);高级示例显示了对非聚集类型的支持,例如派生类或具有自定义构造函数的类:
namespace data = cista::offset;
constexpr auto MODE = cista::mode::WITH_VERSION;
struct parent {
parent () = default ;
explicit parent ( int a) : x_{a}, y_{a} {}
auto cista_members () { return std::tie (x_, y_); }
int x_, y_;
};
struct child : parent {
child () = default ;
explicit child ( int a) : parent{a}, z_{a} {}
auto cista_members () {
return std::tie (* static_cast <parent*>( this ), z_);
}
int z_;
};
/*
* Automatically defaulted for you:
* - de/serialization
* - hashing (use child in hash containers)
* - equality comparison
* - data structure version ("type hash")
*/
using t = data::hash_map<child, int >;
// ... usage, serialization as in the previous examples请查看基准存储库以获取更多详细信息。
| 图书馆 | 连载 | 避免 | 快速挑选 | 遍历 | 避免和遍历 | 尺寸 |
|---|---|---|---|---|---|---|
| Cap'n Proto | 105毫秒 | 0.002毫秒 | 0.0 ms | 356毫秒 | 353毫秒 | 50.5m |
| 谷物 | 239毫秒 | 197.000毫秒 | - | 125毫秒 | 322毫秒 | 3780万 |
CISTA ++ offset | 72 ms | 0.053毫秒 | 0.0 ms | 132 ms | 132 ms | 253m |
CISTA ++ raw | 3555毫秒 | 68.900毫秒 | 21.5 ms | 112毫秒 | 133 ms | 176.4m |
| Flatbuffers | 2349毫秒 | 15.400毫秒 | 0.0 ms | 136毫秒 | 133 ms | 63.0m |
读者和作家应具有相同的指针宽度。支持具有不同字节顺序(endianess)的系统上的数据。示例:
当前,只有C ++ 17软件才能读取/写入数据。但是也应该为其他编程语言生成访问者。
如果您需要与其他编程语言兼容或需要协议演变(向下兼容性),则应寻找另一个解决方案:
随时贡献(错误报告,拉请请求等)!