jeudi 22 août 2019

constexpr for unique packet type numbers

I'm thinking about using a constexpr for generating packet id numbers in my code.

#define REGISTER_PACKET(CLASS_NAME) //Builds class name to const int relation
#define GET_PACKET_ID(CLASS_NAME) //evaluates to a const int

    class Somepacket1, Somepacket2, Somepacket3 //ext...

void main(){
//Registration would be done in a class to force same code on both sides
//But would allow for extra packets later on.
    REGISTER_PACKET(Somepacket1) //Registers as 0
    REGISTER_PACKET(Somepacket2) //Registers as 1
    REGISTER_PACKET(Somepacket3) //Registers as 2
    REGISTER_PACKET(Somepacket3) //Ignores as Packet3 is already registered

    int someval = GET_PACKET_ID(Somepacket2); //Just and example, would be pulled from data of serialised packet.
    switch(someval){ //Would be implemented in my visitor
        case GET_PACKET_ID(Somepacket1): //evauluates to 0 at compile time
        case GET_PACKET_ID(Somepacket2): //evauluates to 1 at compile time
}

This is for my networking library, id like to try and make it extendable but i'm unsure if this idea will work. Would this be a good idea? And is this even possible?

Aucun commentaire:

Enregistrer un commentaire