Expert Advisors • Indicators • Scripts • Libraries

MQL.RobotFX.org is the biggest collection of MetaTrader expert advisors (MT5 & MT4), indicators, scripts and libraries that can be used to improve trading results, minimize risks or simply automate trading tasks

MetaTrader 5 Libraries | CBitBuffer Class - Data Serialization in MQL5

MetaTrader Experts, Indicators, Scripts and Libraries

CBitBuffer Class - Bit-Level Data Serialization in MQL5

The CBitBuffer class provides a robust foundation for bit-level data serialization in MQL5, offering fine-grained control over data storage and retrieval. The class includes support for various data types, including variable-length integers (VLQ with ZigZag encoding), and serialization of strings and structures, which are excellent for optimizing space. The class employs optimizations like internal buffering and exponential array growth to enhance performance and provides a comprehensive error handling system. It's particularly useful for network communication or file storage where minimizing data size is crucial (e.g., compression of tick data).

Key Features:

  • Bit-level Operations: Allows writing and reading data bit by bit, or in specified bit lengths up to 64 bits.
  • Data Type Support: Includes methods for bool, char, uchar, short, ushort, int, uint, long, ulong, datetime, float, double, string, and struct.
  • Variable-Length Integers (VLQ): Implements VLQ encoding for int, uint, long, and ulong values, which can significantly save space for frequently small integer values.
  • Error Handling: Provides an ENUM_BIT_BUFFER_ERROR enum and GetLastError()/GetLastErrorString() methods for robust error management.
  • Buffer Management: Offers functions to clear, finalize, set raw buffer content, save to file, and load from file.
  • Internal Buffering: Uses internal 64-bit buffers (m_writeBufferInternal, m_readBufferInternal) to optimize bit-level operations by accumulating bits before writing to or reading from the main ulong[] array.
//+------------------------------------------------------------------+ //| CBitBuffer Class                                                 | //| A class for reading and writing individual bits or bit sequences | //| to and from a buffer (ulong[] array).                            | //| Provides functionality for efficient bit manipulation and buffer | //| management.                                                      | //+------------------------------------------------------------------+ class CBitBuffer   { public: // Core bit operations    bool              WriteBit(bool bit);    bool              WriteBits(ulong value, int numberOfBits);    bool              ReadBit();    ulong             ReadBits(int numberOfBits);    ulong             PeekBits(int numberOfBits); // Reads bits without advancing read position  public: // Position and size management    bool              SetReadPosition(long bitPosition);    long              GetReadPosition();                  bool              ResetReadPosition();    bool              SkipBits(long bitsToSkip);    long              GetTotalWrittenBits();    long              GetTotalBytesWritten();    long              GetTotalBytesAllocated();    long              GetRemainingReadBits();  public: // Data type specific read/write operations    bool              WriteBool(bool value);    bool              WriteChar(char value);    bool              WriteUChar(uchar value);    bool              WriteShort(short value);    bool              WriteUShort(ushort value);    bool              WriteInt(int value);    bool              WriteUInt(uint value);    bool              WriteLong(long value);    bool              WriteULong(ulong value);    bool              WriteDatetime(datetime value);    bool              WriteFloat(float value);    bool              WriteDouble(double value);    bool              WriteString(string value);    template<typename T>    bool              WriteStruct(T &struct_object);     bool              ReadBool();    char              ReadChar();    uchar             ReadUChar();    short             ReadShort();    ushort            ReadUShort();    int               ReadInt();    uint              ReadUInt();    long              ReadLong();    ulong             ReadULong();    datetime          ReadDatetime();    float             ReadFloat();    double            ReadDouble();    string            ReadString();    template<typename T>    T                 ReadStruct();  public: // Variable-length encoding for integers    bool              WriteVarInt(int value);    bool              WriteVarUInt(uint value);    bool              WriteVarLong(long value);    bool              WriteVarULong(ulong value);    int               ReadVarInt();    uint              ReadVarUInt();    long              ReadVarLong();    ulong             ReadVarULong();  public: // Buffer management    void              Clear();    bool              GetFinalizedBuffer(ulong &destinationArray[]);    bool              SetRawBuffer(const ulong &sourceBuffer[]);    bool              Save(string filename);    bool              Load(string filename);  public: // Error handling    ENUM_BIT_BUFFER_ERROR GetLastError()                   const;    string            GetLastErrorString()                 const;    void              ClearLastError();   }; 
61728