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.h
Go to the documentation of this file.
1#include <array> // std::array<>
2#include <cstring> // memcpy()
3#include <iosfwd> // Forwarded std::istream, std::ostream
4#include <string> // std::string
5#include <tuple> // std::tuple<>
6#include <type_traits> // std::is_standard_layout_v<>
7
8namespace bux {
9
10//
11// Functions
12//
13template<class T>
14void append(const T &src, std::string &dst)
15{
16 static_assert(std::is_standard_layout_v<T>);
17 dst.append(reinterpret_cast<const char*>(&src), sizeof src);
18}
19
20template<class T>
21void append_size_of(const T &src, std::string &dst)
22{
23 append(std::size(src), dst);
24}
25
26template<class T>
27void append(const T *src, size_t argN, std::string &dst)
28{
29 static_assert(std::is_standard_layout_v<T>);
30 static_assert(sizeof(T[10]) == sizeof(T) * 10);
31 dst.append(reinterpret_cast<const char*>(src), sizeof(*src)*argN);
32}
33
34template<class T>
35void read(const std::string &src, size_t &off, T &data) noexcept
36{
37 static_assert(std::is_standard_layout_v<T>);
38 data = *reinterpret_cast<const T*>(src.data() + off);
39 off += sizeof data;
40}
41
42size_t read_size(const std::string &src, size_t &off) noexcept;
43
44template<class T>
45void read(const std::string &src, size_t &off, T *data, size_t count)
46{
47 static_assert(std::is_standard_layout_v<T>);
48 const auto bytes = sizeof(T) * count;
49 memcpy(data, src.data()+off, bytes);
50 off += bytes;
51}
52
53template<size_t N>
54auto to_str(const std::array<char,N> &arr)
55{
56 return std::string{arr.data(), arr.size()};
57}
58
59template<size_t N>
60std::array<char,N> read_charr(const std::string &src, size_t &off) noexcept
61{
62 std::array<char,N> ret;
63 memcpy(ret.data(), src.data()+off, N);
64 off += N;
65 return ret;
66}
67
68template<size_t N, template<typename> class C>
69void append(const C<std::array<char,N>> &src, std::string &dst)
70{
71 append_size_of(src, dst);
72 if constexpr (N <= 8)
73 {
74 for (auto i: src)
75 dst += to_str(i);
76 }
77 else
78 {
79 for (auto &i: src)
80 dst += to_str(i);
81 }
82}
83
84template<size_t N, template<typename> class C>
85void read(const std::string &src, size_t &off, C<std::array<char,N>> &data)
86{
87 for (size_t i = 0, n = read_size(src,off); i < n; ++i)
88 data.emplace_back(read_charr<N>(src, off));
89}
90
91std::tuple<std::string,size_t,bool> load_hashed_str(std::istream &in);
92size_t save_hashed_str(std::ostream &out, const std::string &s);
93
94} // 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
void append_size_of(const T &src, std::string &dst)
Definition Serialize.h:21
auto to_str(const std::array< char, N > &arr)
Definition Serialize.h:54
size_t read_size(const std::string &src, size_t &off) noexcept
Definition Serialize.cpp:12
std::array< char, N > read_charr(const std::string &src, size_t &off) noexcept
Definition Serialize.h:60
size_t save_hashed_str(std::ostream &out, const std::string &s)
Definition Serialize.cpp:27
void append(const T &src, std::string &dst)
Definition Serialize.h:14