-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.cpp
More file actions
136 lines (106 loc) · 3.61 KB
/
example.cpp
File metadata and controls
136 lines (106 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "EndianSafeBinaryStream.hpp"
#include <iostream>
#include <string>
#include <vector>
struct ExampleStruct
{
int32_t a;
bool b;
uint64_t c;
float f;
};
// a custom extension for the struct above
namespace esbs
{
template <>
struct EndianSafeBinaryStreamExtension<ExampleStruct>
{
template <typename Stream>
static EndianSafeBinaryStream<Stream> &serialize(EndianSafeBinaryStream<Stream> &stream, const ExampleStruct &v)
{
stream << v.a << v.b << v.c << v.f;
return stream;
}
template <typename Stream>
static EndianSafeBinaryStream<Stream> &deserialize(EndianSafeBinaryStream<Stream> &stream, ExampleStruct &v)
{
stream >> v.a >> v.b >> v.c >> v.f;
return stream;
}
};
}
int main(int argc, char **args)
{
// serialize (write) data
{
// create the stream
esbs::EndianSafeBinaryStream<std::fstream> stream = esbs::output_to_file("test.bin");
// basic data types
uint32_t number1 = 123456789;
int16_t number2 = -16;
double number3 = 15.89;
stream << number1 << number2 << number3;
// advanced data type using the optional (activated by default) extensions
std::string example_message = "Hello World :)";
stream << example_message;
std::vector<uint64_t> multiples_of_two;
uint64_t multiple = 1;
for (int i = 0; i < 64; i++)
{
multiples_of_two.push_back(multiple);
multiple *= 2;
}
stream << multiples_of_two;
// using a custom implemented extension for a custom object
ExampleStruct s;
s.a = -5555555;
s.b = true,
s.c = 1357913579;
s.f = 42.42f;
stream << s;
}
// deserialize (read) data
{
// create the stream
esbs::EndianSafeBinaryStream<std::fstream> stream = esbs::input_from_file("test.bin");
// basic data types
uint32_t number1;
int16_t number2;
double number3;
stream >> number1 >> number2 >> number3;
std::cout << "number1: " << number1 << std::endl;
std::cout << "number2: " << number2 << std::endl;
std::cout << "number3: " << number3 << std::endl;
// advanced data type using the optional (activated by default) extensions
std::string example_message;
stream >> example_message;
std::cout << "example_message: " << example_message << std::endl;
std::vector<uint64_t> multiples_of_two;
stream >> multiples_of_two;
std::cout << "multiples_of_two: [";
for (int i = 0; i < multiples_of_two.size(); i++)
{
std::cout << " " << multiples_of_two[i];
}
std::cout << " ]" << std::endl;
// using a custom implemented extension for a custom object
ExampleStruct s;
stream >> s;
std::cout << "ExampleStruct s: { a: " << s.a << ", b: " << s.b << ", c: "
<< s.c << ", f: " << s.f << " }" << std::endl;
}
// deserialize (read) using ifstream
{
std::ifstream input_filestream("test.bin", std::ios_base::binary);
esbs::EndianSafeBinaryStream<std::ifstream> stream(std::move(input_filestream));
// basic data types
uint32_t number1;
int16_t number2;
double number3;
stream >> number1 >> number2 >> number3;
std::cout << "number1: " << number1 << std::endl;
std::cout << "number2: " << number2 << std::endl;
std::cout << "number3: " << number3 << std::endl;
}
return 0;
}