You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
*[Adding new serializables types](#adding-new-serializables-types)
38
40
*[Unified serialization](#unified-serialization)
@@ -54,7 +56,7 @@ The header files can either be downloaded from the [releases page](https://githu
54
56
The source and header files inside the `src/` directory are only tests and should not be included into your project, unless you wish to test the library as part of your pipeline.
55
57
56
58
# Usage
57
-
The library has a global header file ([`bitstream/bitstream.h`](https://github.com/KredeGC/BitStream/tree/master/include/bitstream/bitstream.h)) which includes every other header file in the library.
59
+
The library has a main header file ([`bitstream/bitstream.h`](https://github.com/KredeGC/BitStream/tree/master/include/bitstream/bitstream.h)) which includes every other header file in the library.
58
60
59
61
If you only need certain features you can instead opt to just include the files you need.
60
62
The files are stored in categories:
@@ -76,6 +78,117 @@ You can also look at the unit tests to get a better idea about what you can expe
76
78
# Documentation
77
79
Refer to [the documentation](https://kredegc.github.io/BitStream/namespaces.html) for more information about what different classes provide.
78
80
81
+
# Serialization Examples
82
+
The examples below follow the same structure: First writing to a buffer and then reading from it. Each example is littered with comments about the procedure, as well as what outcome is expected.
83
+
84
+
Writing the first 5 bits of an int to the buffer, then reading it back:
85
+
```cpp
86
+
// Create a writer, referencing the buffer and its size
87
+
alignas(uint32_t) uint8_t buffer[4]; // Buffer must be a multiple of 4 bytes / 32 bits and 4-byte-aligned
88
+
bit_writer writer(buffer, 4);
89
+
90
+
// Write the value
91
+
uint32_t value = 27; // We can choose any value below 2^5. Otherwise we need more than 5 bits
92
+
writer.serialize_bits(value, 5);
93
+
94
+
// Flush the writer's remaining state into the buffer
95
+
uint32_t num_bits = writer.flush();
96
+
97
+
// Create a reader, referencing the buffer and bits written
98
+
bit_reader reader(buffer, num_bits);
99
+
100
+
// Read the value back
101
+
uint32_t out_value; // We don't have to initialize it yet
102
+
reader.serialize_bits(out_value, 5); // out_value should now have a value of 27
103
+
```
104
+
105
+
Writing a signed int to the buffer, within a range:
106
+
```cpp
107
+
// Create a writer, referencing the buffer and its size
108
+
byte_buffer<4> buffer; // byte_bufer is just a wrapper for a 4-byte aligned buffer
109
+
bit_writer writer(buffer);
110
+
111
+
// Write the value
112
+
int32_t value = -45; // We can choose any value within the range below
113
+
writer.serialize<int32_t>(value, -90, 40); // A lower and upper bound which the value will be quantized between
114
+
115
+
// Flush the writer's remaining state into the buffer
116
+
uint32_t num_bits = writer.flush();
117
+
118
+
// Create a reader, referencing the buffer and bits written
119
+
bit_reader reader(buffer, num_bits);
120
+
121
+
// Read the value back
122
+
int32_t out_value; // We don't have to initialize it yet
123
+
reader.serialize<int32_t>(out_value, -90, 40); // out_value should now have a value of -45
124
+
```
125
+
126
+
Writing a c-style string into the buffer:
127
+
```cpp
128
+
// Create a writer, referencing the buffer and its size
129
+
byte_buffer<32> buffer;
130
+
bit_writer writer(buffer);
131
+
132
+
// Write the value
133
+
const char* value = "Hello world!";
134
+
writer.serialize<const char*>(value, 32U); // The second argument is the maximum size we expect the string to be
135
+
136
+
// Flush the writer's remaining state into the buffer
137
+
uint32_t num_bits = writer.flush();
138
+
139
+
// Create a reader, referencing the buffer and bits written
140
+
bit_reader reader(buffer, num_bits);
141
+
142
+
// Read the value back
143
+
char out_value[32]; // Set the size to the max size
144
+
reader.serialize<const char*>(out_value, 32U); // out_value should now contain "Hello world!\0"
145
+
```
146
+
147
+
Writing a std::string into the buffer:
148
+
```cpp
149
+
// Create a writer, referencing the buffer and its size
150
+
byte_buffer<32> buffer;
151
+
bit_writer writer(buffer);
152
+
153
+
// Write the value
154
+
std::string value = "Hello world!";
155
+
writer.serialize<std::string>(value, 32U); // The second argument is the maximum size we expect the string to be
156
+
157
+
// Flush the writer's remaining state into the buffer
158
+
uint32_t num_bits = writer.flush();
159
+
160
+
// Create a reader, referencing the buffer and bits written
161
+
bit_reader reader(buffer, num_bits);
162
+
163
+
// Read the value back
164
+
std::string out_value; // The string will be resized if the output doesn't fit
165
+
reader.serialize<std::string>(out_value, 32U); // out_value should now contain "Hello world!"
166
+
```
167
+
168
+
Writing a float into the buffer with a bounded range and precision:
169
+
```cpp
170
+
// Create a writer, referencing the buffer and its size
// Flush the writer's remaining state into the buffer
180
+
uint32_t num_bits = writer.flush();
181
+
182
+
// Create a reader, referencing the buffer and bits written
183
+
bit_reader reader(buffer, num_bits);
184
+
185
+
// Read the value back
186
+
float out_value;
187
+
reader.serialize<bounded_range>(range, out_value); // out_value should now be a value close to 1.2345678f
188
+
```
189
+
190
+
These examples can also be seen in [`src/test/examples_test.cpp`](https://github.com/KredeGC/BitStream/tree/master/src/test/examples_test.cpp).
191
+
79
192
# Serializables - serialize_traits
80
193
Below is a noncomprehensive list of serializable traits.
81
194
A big feature of the library is extensibility, which is why you can add your own types as you please, or choose not to include specific types if you don't need them.
The examples below follow the same structure: First writing to a buffer and then reading from it. Each example is littered with comments about the procedure, as well as what outcome is expected.
263
-
264
-
Writing the first 5 bits of an int to the buffer, then reading it back:
265
-
```cpp
266
-
// Create a writer, referencing the buffer and its size
267
-
alignas(uint32_t) uint8_t buffer[4]; // Buffer must be a multiple of 4 bytes / 32 bits and 4-byte-aligned
268
-
bit_writer writer(buffer, 4);
269
-
270
-
// Write the value
271
-
uint32_t value = 27; // We can choose any value below 2^5. Otherwise we need more than 5 bits
272
-
writer.serialize_bits(value, 5);
273
-
274
-
// Flush the writer's remaining state into the buffer
275
-
uint32_t num_bits = writer.flush();
276
-
277
-
// Create a reader, referencing the buffer and bits written
278
-
bit_reader reader(buffer, num_bits);
279
-
280
-
// Read the value back
281
-
uint32_t out_value; // We don't have to initialize it yet
282
-
reader.serialize_bits(out_value, 5); // out_value should now have a value of 27
283
-
```
284
-
285
-
Writing a signed int to the buffer, within a range:
286
-
```cpp
287
-
// Create a writer, referencing the buffer and its size
288
-
byte_buffer<4> buffer; // byte_bufer is just a wrapper for a 4-byte aligned buffer
289
-
bit_writer writer(buffer);
290
-
291
-
// Write the value
292
-
int32_t value = -45; // We can choose any value within the range below
293
-
writer.serialize<int32_t>(value, -90, 40); // A lower and upper bound which the value will be quantized between
294
-
295
-
// Flush the writer's remaining state into the buffer
296
-
uint32_t num_bits = writer.flush();
297
-
298
-
// Create a reader, referencing the buffer and bits written
299
-
bit_reader reader(buffer, num_bits);
300
-
301
-
// Read the value back
302
-
int32_t out_value; // We don't have to initialize it yet
303
-
reader.serialize<int32_t>(out_value, -90, 40); // out_value should now have a value of -45
304
-
```
305
-
306
-
Writing a c-style string into the buffer:
307
-
```cpp
308
-
// Create a writer, referencing the buffer and its size
309
-
byte_buffer<32> buffer;
310
-
bit_writer writer(buffer);
311
-
312
-
// Write the value
313
-
const char* value = "Hello world!";
314
-
writer.serialize<const char*>(value, 32U); // The second argument is the maximum size we expect the string to be
315
-
316
-
// Flush the writer's remaining state into the buffer
317
-
uint32_t num_bits = writer.flush();
318
-
319
-
// Create a reader, referencing the buffer and bits written
320
-
bit_reader reader(buffer, num_bits);
321
-
322
-
// Read the value back
323
-
char out_value[32]; // Set the size to the max size
324
-
reader.serialize<const char*>(out_value, 32U); // out_value should now contain "Hello world!\0"
325
-
```
326
-
327
-
Writing a std::string into the buffer:
328
-
```cpp
329
-
// Create a writer, referencing the buffer and its size
330
-
byte_buffer<32> buffer;
331
-
bit_writer writer(buffer);
332
-
333
-
// Write the value
334
-
std::string value = "Hello world!";
335
-
writer.serialize<std::string>(value, 32U); // The second argument is the maximum size we expect the string to be
336
-
337
-
// Flush the writer's remaining state into the buffer
338
-
uint32_t num_bits = writer.flush();
339
-
340
-
// Create a reader, referencing the buffer and bits written
341
-
bit_reader reader(buffer, num_bits);
342
-
343
-
// Read the value back
344
-
std::string out_value; // The string will be resized if the output doesn't fit
345
-
reader.serialize<std::string>(out_value, 32U); // out_value should now contain "Hello world!"
346
-
```
347
-
348
-
Writing a float into the buffer with a bounded range and precision:
349
-
```cpp
350
-
// Create a writer, referencing the buffer and its size
0 commit comments