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軟件才能讀取/寫入數據。但是也應該為其他編程語言生成訪問者。
如果您需要與其他編程語言兼容或需要協議演變(向下兼容性),則應尋找另一個解決方案:
隨時貢獻(錯誤報告,拉請請求等)!