C++ Best Practices: Serialization and Abstract Classes without library access

Preface: Serialization libraries cannot be used due to restrictions in the development environment.

I have various struct that need to be serialized so that they may be broadcast over UDP.

I would like to define an abstract class for serializable types that has serialization and deserialization methods to be implemented by various message types. This is what I’ve been working with, but it “smells” to me. Ideally it would be nice to make Deserialize static and have it return a concrete implementation of Serializable, but virtual methods cannot be static:

class Serializable {
public:
    virtual std::size_t Serialize(char* buffer) = 0;
    virtual bool Deserialize(const char* buffer) = 0;
};

An abstract UDP specific subclass of this may be as follows:

template<typename T>
class UdpMessageBase : public Serializable {
protected:
    UDPHeader header; // struct that contains primitive types for metadata about message
    T data; // struct that contains message data, subclasses will specialize, see below
    bool populated = false;
public:
    virtual ~UdpMessageBase() = 0; //abstract class
    T getData() {return data;}
    UDPHeader getHeader() {return header;}
};

And finally a specific implementation of this for a specific message:

class MySpecificUdpMessage : public UdpMessageBase<structForMySpecificUdpMessage> {
public:
    MySpecificUdpMessage() {
        initHeader();
    }

    MySpecificUdpMessage(structForMySpecificUdpMessage data) : data(data) {
        initHeader();
        populated = true;
    }
    
    std::size_t Serialize(char* buffer) {
        if (populated) {
            // serialize the header and data into buffer to be used by caller
            // return total size of serialized data
        }
        else { // throw error };
    }

    bool Deserialize(const char* buffer) {
        if (populated) {
            // throw error
        }
        else {
            // deserialize buffer into header and data of this instance of the object
            populated = true;
            // return true or false based on success
        }
    }

private:
    void initHeader() {
        //set header values specific to this message type
    }
};

Example usage:

// for outgoing udp message where we have the data
dataForMySpecificUdpMessage someDataToSend; // assume this was passed into this method
char* buffer; // buffer we want to serialize data into
MySpecificUdpMessage messageToSend = MySpecificUdpMessage(someDataToSend);
messageToSend.Serialize(buffer);
SendUDPMessage(buffer); //arbitrary interface that sends the buffer over UDP to client

//for incoming UDP message
char* buffer; //incoming populated data buffer
MySpecificUdpMessage incomingMessage; 
incomingMessage.Deserialize(buffer);
messageProcessor(incomingMessage); //arbitrary message processor
// or
dataProcessor(incomingMessage.getData()); //arbitrary data process for the struct

One alternative approach I’ve though of is a Serializer class that has a bunch of static overloaded Serialize methods that have different implementations based on the passed in struct. They would simply populate the header based on that info, and deserialize data based on deserialized header, but that class would grow with more messages and doesn’t seem very decoupled. I just feel like I’m missing some obvious better practice.

Any tips greatly appreciated.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật