fixed some errors before merging to master

This commit is contained in:
Mindaugas Vinkelis
2017-09-03 19:34:08 +03:00
parent 241f1f2940
commit cba80774e4
6 changed files with 77 additions and 79 deletions

View File

@@ -105,7 +105,8 @@ int main() {
serialize(ser, data);
bw.flush();
std::cout << "size: " << buf.size() << std::endl;
auto range = bw.getWrittenRange();
std::cout << "size: " << std::distance(range.begin(), range.end()) << std::endl;
return 0;
}
```
@@ -147,10 +148,13 @@ Serialization function is very readable, and explicitly express intent what and
> learn more about why you need to write [value4 instead of value](../design/function_n.md).
Finally before sending buffer to network you must *flush* BufferWriter, it writes any remaining bits to buffer. In our case it is not required, because we only worked with whole bytes, but it is good practice to always use it after finishing serialization.
Finally, before getting serialized data you must *flush* BufferWriter, it writes any remaining bits to buffer. In our case it is not required, because we only worked with whole bytes, but it is good practice to always use it after finishing serialization.
To actually get written data you must call *getWrittenRange*, it return begin/end iterators to our buffer (*std::vector<uint8_t> buf*), for performance reasons BufferWritter always resizes underlying buffer to *capacity* so it could use containers iterator to update data, instead of back_insert_iterator to insert data.
```cpp
bw.flush();
auto range = bw.getWrittenRange();
```
# Summary

View File

@@ -81,7 +81,7 @@ int main() {
bw.flush();
BufferReader br{buf};
BufferReader br{bw.getWrittenRange()};
Deserializer<BufferReader> des{br};
serialize(des, res);
@@ -104,16 +104,18 @@ name equals: 1
We created *Deserializer* and modified *serialize* function to accept *Serializer* and *Deserializer*.
Deserialization is very similar as serialization, it also consists of three separate components:
* Buffer - container that we read data from, in our case vector<uint8_t>.
* BufferReader - reads bytes and bits from *Buffer*, it also makes sure that it is portable across Little and Big endian systems.
* Buffer - container that we read data from, in our case *vector<uint8_t>*.
* BufferReader - reads bytes and bits from *Buffer*, it also makes sure that it is portable across Little and Big endian systems.
* Deserializer - same interface as *Serializer* that use *BufferReader* to read bits and bytes, and convert to specific type. Deserializer also checks for errors at runtime, because data might come from untrusted source and can terminate program with buffer-overflow or segmentation fault if we are not careful.
Since deserialization involves error checking there are two additional functions to check if everything is correct after deserialization.
* [BufferReader.isCompleted()](../reference/buf_is_completed.md) - returns true, if whole buffer was read during deserialization.
* [Deserializer.isValid()](../reference/fnc_is_valid.md) - returns true, if there was no errors during deserialization.
One thing to note about BufferReader is that it doesn't have constructor that accepts buffer directly. Instead it only accepts begin/end iterators, because it needs to know precise data buffer length, to correctly use *isComplete* function.
```cpp
BufferReader br{buf};
BufferReader br{bw.getWrittenRange()};
Deserializer<BufferReader> des{br};
```