Skip to content

[DX L2] Serialization adapter over BitStream #19

Description

@Segfaultd

Context

BitStream already has the good bits — operator overloads at BitStream.h:1914-1928:

template <class T> BitStream& operator<<(BitStream& out, const T& c) { out.Write(c); return out; }
template <class T> BitStream& operator>>(BitStream& in,  T& c)       { in.Read(c); return in; }

and a bidirectional Serialize(bool writeToBitstream, T& inOut) at BitStream.h:89. But message code still (de)serializes field-by-field and manually skips the header with IgnoreBytes(sizeof(MessageID)) (BitStream.h:513).

Goal

A single serialize convention so a user type describes its wire format once and works in both directions:

struct ChatMessage {
    std::string text;
    int channel;
    template <class Ar> void serialize(Ar& ar) { ar & text & channel; }
};

with an Archive adapter:

namespace MafiaNet {
class WriteArchive { BitStream& bs; public:
    template <class T> WriteArchive& operator&(const T& v) { bs << v; return *this; } };
class ReadArchive  { BitStream& bs; public:
    template <class T> ReadArchive&  operator&(T& v)       { bs >> v; return *this; } };
}

Where

New header Source/include/mafianet/Archive.h, built on BitStream.h. Export from umbrella (#12).

Notes

  • This is only the wire convention — message-ID assignment and dispatch live in [DX L3] Typed message dispatcher #20.
  • Reuse the existing operator<< / operator>> and BitStream's type specializations (it already handles std::string, SystemAddress, RakNetGUID, etc.) — don't reimplement primitives.
  • A type with serialize() should round-trip; consider also accepting types that already have operator<</>>.

Acceptance criteria

  • A struct with template<class Ar> serialize(Ar&) round-trips through a BitStream (write then read yields equal value).
  • Works for nested types and std::string.

Dependencies

Metadata

Metadata

Assignees

No one assigned

    Labels

    dxDeveloper experience / public API ergonomicsenhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions