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
Copy file name to clipboardExpand all lines: README.md
+71-23Lines changed: 71 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -44,6 +44,7 @@ If you really want it to work with earlier versions, you should just be able to
44
44
# Installation
45
45
As this is a header-only library, you can simply copy the header files directly into your project and include them where relevant.
46
46
The header files can either be downloaded from the [releases page](https://github.com/KredeGC/BitStream/releases) or from the [`include/`](https://github.com/KredeGC/BitStream/tree/master/include/bitstream) directory on the master branch.
47
+
47
48
The source and header files inside the `test/` directory are only tests and should not be included into your project, unless you wish to test the library as part of your pipeline.
48
49
49
50
# Usage
@@ -55,6 +56,11 @@ The files are stored in categories:
55
56
*[`stream/`](https://github.com/KredeGC/BitStream/tree/master/include/bitstream/stream/) - Files relating to streams that read and write bits
56
57
*[`traits/`](https://github.com/KredeGC/BitStream/tree/master/include/bitstream/traits/) - Files relating to various serialization traits, like serializble strings, integrals etc.
57
58
59
+
An important aspect of the serialiaztion is performance, since the library is meant to be used in a tight loop, like with networking.
60
+
This is why most operations don't use exceptions, but instead return true or false depending on whether the operation was a success.
61
+
It's important to check these return values after every operation, especially when reading.
62
+
You can check it manually or use the `BS_ASSERT(x)` macro for this, if you want your function to return false on failure.
63
+
58
64
It is also possible to dynamically put a break point or trap when a bitstream would have otherwise returned false. This can be great for debugging custom serialization code, but should generally be left out of production code. Simply `#define BS_DEBUG_BREAK` before including any of the library header files if you want to break when an operation fails.
59
65
60
66
For more examples of usage, see the [Serialization Examples](#serialization-examples) below.
int32_t value = -45; // We can choose any value within the range below
251
-
writer.serialize<int32_t>(value, -90, 40);
271
+
writer.serialize<int32_t>(value, -90, 40); // A lower and upper bound which the value will be quantized between
252
272
253
273
// Flush the writer's remaining state into the buffer
254
-
uint32_t num_bytes = writer.flush();
274
+
writer.flush();
255
275
256
276
// Create a reader by moving and invalidating the writer
257
277
bit_reader reader(std::move(writer));
@@ -272,7 +292,7 @@ const char* value = "Hello world!";
272
292
writer.serialize<const char*>(value, 32U); // The second argument is the maximum size we expect the string to be
273
293
274
294
// Flush the writer's remaining state into the buffer
275
-
uint32_t num_bytes = writer.flush();
295
+
writer.flush();
276
296
277
297
// Create a reader by moving and invalidating the writer
278
298
bit_reader reader(std::move(writer));
@@ -282,6 +302,27 @@ char out_value[32]; // Set the size to the max size
282
302
reader.serialize<const char*>(out_value, 32U); // out_value should now contain "Hello world!\0"
283
303
```
284
304
305
+
Writing a std::string into the buffer:
306
+
```cpp
307
+
// Create a writer, referencing the buffer and its size
308
+
uint8_t buffer[32];
309
+
bit_writer writer(buffer, 32);
310
+
311
+
// Write the value
312
+
std::string value = "Hello world!";
313
+
writer.serialize<std::string>(value, 32U); // The second argument is the maximum size we expect the string to be
314
+
315
+
// Flush the writer's remaining state into the buffer
316
+
writer.flush();
317
+
318
+
// Create a reader by moving and invalidating the writer
319
+
bit_reader reader(std::move(writer));
320
+
321
+
// Read the value back
322
+
std::string out_value; // The string will be resized if the output doesn't fit
323
+
reader.serialize<std::string>(out_value, 32U); // out_value should now contain "Hello world!"
324
+
```
325
+
285
326
Writing a float into the buffer with a bounded range and precision:
286
327
```cpp
287
328
// Create a writer, referencing the buffer and its size
@@ -294,7 +335,7 @@ float value = 1.2345678f;
294
335
writer.serialize<bounded_range>(range, value);
295
336
296
337
// Flush the writer's remaining state into the buffer
297
-
uint32_t num_bytes = writer.flush();
338
+
writer.flush();
298
339
299
340
// Create a reader by moving and invalidating the writer
300
341
bit_reader reader(std::move(writer));
@@ -323,13 +364,14 @@ struct serialize_traits<TRAIT_TYPE> // The type to use when serializing
323
364
};
324
365
```
325
366
326
-
Note that `TRAIT_TYPE` does not necessarily have to be part of the function definitions. It is purely used to specify which trait to use when serializing, since it cannot be deduced from the arguments.<br/>
367
+
Note that `TRAIT_TYPE` does not necessarily have to be part of the serialize function definitions.
368
+
It is purely used to specify which trait to use when serializing, since it cannot be deduced from the arguments.<br/>
327
369
To use the trait above to serialize an object you need to explicitly specify it:
328
370
```cpp
329
371
bool status = writer.serialize<TRAIT_TYPE>(...);
330
372
```
331
373
332
-
The specialization can also be unified, if writing and reading look similar:
374
+
The specialization can also be unified with templating, if writing and reading look similar:
333
375
```cpp
334
376
template<>
335
377
structserialize_traits<TRAIT_TYPE> // The type to use when serializing
@@ -348,6 +390,8 @@ It also works with `enable_if`:
More concrete examples of traits can be found in the [`traits/`](https://github.com/KredeGC/BitStream/tree/master/include/bitstream/traits/) directory.
@@ -373,7 +417,11 @@ premake5 test --config=(release | debug)
373
417
```
374
418
375
419
# 3rd party
376
-
The library has no dependencies, but does build upon some code from the [NetStack](https://github.com/nxrighthere/NetStack) library by Stanislav Denisov, which is free to use, as per their [license](https://github.com/nxrighthere/NetStack/blob/master/LICENSE). The code in question is about quantizing floats and quaternions, which has simply been translated from C# into C++ for the purposes of this library.
420
+
The library has no dependencies, but does build upon some code from the [NetStack](https://github.com/nxrighthere/NetStack) library by Stanislav Denisov, which is free to use, as per their [MIT license](https://github.com/nxrighthere/NetStack/blob/master/LICENSE).
421
+
The code in question is about quantizing floats and quaternions, which has simply been translated from C# into C++ for the purposes of this library.
422
+
423
+
If you do not wish to use float, half or quaternion quantization, you can simply remove the [`quantization/`](https://github.com/KredeGC/BitStream/tree/master/include/bitstream/quantization/) directory from the library, in which case you will not need to include or adhere to the license for NetStack.
377
424
378
425
# License
379
-
The library is licensed under the [BSD-3-Clause license](https://github.com/KredeGC/BitStream/blob/master/LICENSE) and is subject to the terms and conditions in that license as well as the [NetStack license](https://github.com/nxrighthere/NetStack/blob/master/LICENSE).
426
+
The library is licensed under the [BSD-3-Clause license](https://github.com/KredeGC/BitStream/blob/master/LICENSE) and is subject to the terms and conditions in that license.
427
+
In addition to this, everything in the [`quantization/`](https://github.com/KredeGC/BitStream/tree/master/include/bitstream/quantization/) directory is subject to the [MIT license](https://github.com/nxrighthere/NetStack/blob/master/LICENSE) from [NetStack](https://github.com/nxrighthere/NetStack).
0 commit comments