bux API Reference
1.12.6
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.
Toggle main menu visibility
Loading...
Searching...
No Matches
include
bux
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
8
namespace
bux
{
9
10
//
11
// Functions
12
//
13
template
<
class
T>
14
void
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
20
template
<
class
T>
21
void
append_size_of
(
const
T &src, std::string &dst)
22
{
23
append
(std::size(src), dst);
24
}
25
26
template
<
class
T>
27
void
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
34
template
<
class
T>
35
void
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
42
size_t
read_size
(
const
std::string &src,
size_t
&off)
noexcept
;
43
44
template
<
class
T>
45
void
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
53
template
<
size_t
N>
54
auto
to_str
(
const
std::array<char,N> &arr)
55
{
56
return
std::string{arr.data(), arr.size()};
57
}
58
59
template
<
size_t
N>
60
std::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
68
template
<
size_t
N,
template
<
typename
>
class
C>
69
void
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
84
template
<
size_t
N,
template
<
typename
>
class
C>
85
void
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
91
std::tuple<std::string,size_t,bool>
load_hashed_str
(std::istream &in);
92
size_t
save_hashed_str
(std::ostream &out,
const
std::string &s);
93
94
}
// namespace bux
bux
THE common namespace of bux library.
Definition
AtomiX.cpp:3
bux::load_hashed_str
std::tuple< std::string, size_t, bool > load_hashed_str(std::istream &in)
Definition
Serialize.cpp:19
bux::read
void read(const std::string &src, size_t &off, T &data) noexcept
Definition
Serialize.h:35
bux::append_size_of
void append_size_of(const T &src, std::string &dst)
Definition
Serialize.h:21
bux::to_str
auto to_str(const std::array< char, N > &arr)
Definition
Serialize.h:54
bux::read_size
size_t read_size(const std::string &src, size_t &off) noexcept
Definition
Serialize.cpp:12
bux::read_charr
std::array< char, N > read_charr(const std::string &src, size_t &off) noexcept
Definition
Serialize.h:60
bux::save_hashed_str
size_t save_hashed_str(std::ostream &out, const std::string &s)
Definition
Serialize.cpp:27
bux::append
void append(const T &src, std::string &dst)
Definition
Serialize.h:14
Generated by
1.18.0