I'm making an online game and when I receive data from a client, i receive several 'structures' ( say 'my position', 'use potion 23', 'attack player 45', 'talk bla bla', logout, etc.), those structures are read securely and put into structs, pushed on a list and (later on, when the server has time) processed.
The thing is that the processing is a Big switch statement (switch on a sort of enum-"RTTI") and I have over 60 different structs which are all different.
So the .cpp containing the processing code starts to get fairly big and it won't shrink any time as I constantly add functionalities to the game (the c++ is somewhere between 4000-5000 lines).
I can see a simple solution to this, each case calls a function in another file but that will literally (note to harriyott, yes literally as my project and my drive will be flooded with new files) explode the number of files I need to know and keep track of.
I know I can cut it up (the cases) in several fileN.cpp and make '#include "file1.cpp" #include "file2.cpp" etc. in the processing file but that seems horribly wrong. And any other way that I have 'thought up' to circumvent this problem seems way too 'hacky'.
So the question is, how do you split up a big switch statement nicely?
You can
It seems to me that boost::variant could make your life easier. You build your main event type as a boost::variant of classes and then use static_visitor classes to do your magic. Essentially a switch, but wrapped nicely and type-safely in logic.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With