bux API Reference 1.9.0
Static library of whatever are seen required in general purpose but not directly supported from Modern C++. Or whatever reusable originated from my side projects.
Loading...
Searching...
No Matches
Serialize.cpp
Go to the documentation of this file.
1#include "Serialize.h"
2#include <functional> // std::hash<>
3#include <iterator> // std::istreambuf_iterator
4#include <istream> // std::istream
5#include <ostream> // std::ostream
6
7namespace bux {
8
9//
10// Implement Functions
11//
12size_t read_size(const std::string &src, size_t &off) noexcept
13{
14 size_t n;
15 read(src, off, n);
16 return n;
17}
18
19std::tuple<std::string,size_t,bool> load_hashed_str(std::istream &in)
20{
21 size_t last_hash;
22 in.read(reinterpret_cast<char*>(&last_hash), sizeof last_hash);
23 const std::string src{std::istreambuf_iterator<char>{in}, {}};
24 return {src, last_hash, std::hash<std::string>{}(src) == last_hash};
25}
26
27size_t save_hashed_str(std::ostream &out, const std::string &s)
28{
29 size_t n = std::hash<std::string>{}(s);
30 out.write(reinterpret_cast<char*>(&n), sizeof n) <<s;
31 return n;
32}
33
34} // namespace bux
THE common namespace of bux library.
Definition AtomiX.cpp:3
std::tuple< std::string, size_t, bool > load_hashed_str(std::istream &in)
Definition Serialize.cpp:19
void read(const std::string &src, size_t &off, T &data) noexcept
Definition Serialize.h:35
size_t read_size(const std::string &src, size_t &off) noexcept
Definition Serialize.cpp:12
size_t save_hashed_str(std::ostream &out, const std::string &s)
Definition Serialize.cpp:27