vendredi 10 avril 2020

Ordered class/struct design pattern

I am trying to write a generic pattern for a data struct which is created from an incoming stream ("message") of multiple parts. The message sub-parts are ordered in a standard way determined by the message "category" (to avoid use of type as not cpp type) and each sub-part relates to a cpp type. For example a message of category A might look like [int x, double y]. I would like to write a struct for each message category to read in the data. I know cpp does not have reflection which would allow it to naturally order a class/structs data members, however order/ability to iterate over a structs members with type information would be exactly what I need. What are the design pattern options which would allow to iterate over members of a struct? For example in initialisation the struct could have a std::vector<void*> which get initialised to all the data members but this does not carry type information and I cannot think of a way to generically say that a data member should be included or where in the vector it sits.

It would be ideal to have a design something like;

struct MessageA
{
    Ordered<int> x_;
    Ordered<double> y_;

    MessageA(int x, double y):
        x_(0, x), y_(1, y)
    ...
}

Perhaps with a inherited class OrderedStruct as well

Aucun commentaire:

Enregistrer un commentaire