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
The `bit_writer` and `bit_reader` use a template trait specialization of the given type to deduce how to serialize and deserialize the object.
449
-
The only requirements of the trait is that it has (or can deduce) 2 static functions which take a `bit_writer&` and a `bit_reader&` respectively as their first argument.
448
+
The `bit_writer<T>` and `bit_reader<T>` use a template trait specialization of the given type to deduce how to serialize and deserialize the object.
449
+
The only requirements of the trait is that it has (or can deduce) 2 static functions which take a `bit_writer<T>&` and a `bit_reader<T>&` respectively as their first argument.
450
450
The 2 functions must also return a bool indicating whether the serialization was a success or not, but can otherwise take any number of additional arguments.
451
451
452
452
## Adding new serializables types
@@ -457,17 +457,21 @@ template<>
457
457
struct serialize_traits<TRAIT_TYPE> // The type to use when referencing this specific trait
458
458
{
459
459
// Will be called when writing the object to a stream
460
-
static bool serialize(bit_writer& stream, ...)
460
+
template<typename Stream>
461
+
typename utility::is_writing_t<Stream>
462
+
static serialize(Stream& stream, ...)
461
463
{ ... }
462
464
463
465
// Will be called when reading the object from a stream
464
-
static bool serialize(bit_reader& stream, ...)
466
+
template<typename Stream>
467
+
typename utility::is_reading_t<Stream>
468
+
static serialize(Stream& stream, ...)
465
469
{ ... }
466
470
};
467
471
```
468
472
469
473
As with any functions, you are free to overload them if you want to serialize an object differently, depending on any parameters you pass.
470
-
As long as their list of parameters starts with `bit_writer&` and `bit_reader&` respectively they will be able to be called.
474
+
As long as the first parameter can be deduced to `bit_writer<T>&` and `bit_reader<T>&` respectively they will be able to be called.
471
475
472
476
## Unified serialization
473
477
The serialization can also be unified with templating, if writing and reading look similar.
@@ -488,7 +492,7 @@ struct serialize_traits<TRAIT_TYPE> // The type to use when serializing
488
492
}
489
493
490
494
// A variable that differs if the stream is writing or reading
491
-
int value = Stream::reading ? 0 : 1;
495
+
int value = Stream::reading ? 500 : 200;
492
496
493
497
...
494
498
}
@@ -525,19 +529,23 @@ template<>
525
529
structserialize_traits<TRAIT_TYPE> // The type to use when referencing this specific trait
526
530
{
527
531
// The second argument is the same as TRAIT_TYPE (const and lvalue references are removed when deducing)
0 commit comments