Skip to content

Data Stream

Hantao Sun edited this page Dec 24, 2019 · 2 revisions

Interview

Data-Stream is a series of functions for serialization and deserialization of class from or to a vector. It supports basic types, std::vector, and std::map. If a customized class or struct to be streamed by Data-Stream, there must overwrite ToDataStream and FromDataStream functions. For example:

class CDestination
{
public:
    uint8 prefix;
    uint256 data;

    void ToDataStream(xengine::CODataStream& os) const
    {
        os << prefix << data;
    }

    void FromDataStream(xengine::CIDataStream& is)
    {
        is >> prefix >> data;
    }

basic types

It looks like a continuous byte order in memory.

std::vector, std::map

  • First, serialize or deserialize the length of the variable. It is 8 bytes(sizeof(size_t))
  • Then, serialize or deserialize every item of std::vector, std::map
    • For a vector, if the item is not a basic type, recursively the rules.
    • For a map, serialize or deserialize its key and value of each item. If they are not a basic type, recursively the rules.

uint256, uint224

uint256 is a customized struct to simulate a 32-byte number by 32-byte char array. And uint224 is 28-byte.

It looks like a continuous byte order in memory.

Clone this wiki locally