turorial 2in1

This commit is contained in:
Mindaugas Vinkelis
2017-08-21 20:49:34 +03:00
parent a1104a9b95
commit 5c696f14bd
2 changed files with 32 additions and 1 deletions

View File

@@ -1,4 +1,4 @@
# The Problem
# The problem
You want to serialize *Player* structure efficiently into buffer.

View File

@@ -0,0 +1,31 @@
# The Problem
Deserialization process is the same to serialization in a sense, that all serialization/deserialization operations is in the same order, except that instead of writing to buffer you read from it, so it is very desirable to have the same code express both functionality, but is it really possible? Let's find out!
To achieve this *Deserializer* has exactly the same interface as *Serializer*, EXCEPT that all values in *Deserializer* has **T&**, but *Serializer* has **const T&**.
So one way to make this happen is to have Serializer/Deserializer as template parameter, and actual object accept as *T&* like this.
```cpp
template <typename S>
void serialize(S& s, Player& o) {
s.value4(o.pos.x);
s.value4(o.pos.y);
s.value4(o.pos.z);
s.text1(o.name);
}
```
You can use this function for serialization and deserialization, but you can`t pass *const T&*, which is huge limitation.
# Bitsery solution
In order to fix this *const T&* issue, all we need to do is use [SFINAE](http://en.cppreference.com/w/cpp/language/sfinae) technique to enable this function if T is *Object* or *const Object*, like this:
```cpp
template <typename S, typename T, typename std::enable_if<std::is_same<T, Player>::value || std::is_same<T, const Player>::value>::type* = nullptr>
void serialize (S& s, T& o) {
...
}
```