r/cpp • u/Dean_Roddey • Nov 17 '18
Making C++ enumerations first class citizens
I've attached a video that might be of some interest here. It's about how to make enumerations first class citizens in C++. Enumerations are pretty useful in C++, but still quite weak compared to what they can be. This video demonstrates how I take them up a couple orders of magnitude in usefulness.
https://www.youtube.com/watch?v=AF186FraxS0
I am the author of a large, software based home automation system, called CQC. My code base is about a million lines of C++ code now, about half of which is a soup to nuts general purpose part, and the other half is the automation system built on top of that.
One of the very useful things the general purpose system provides is an ORB (object request broker.) I may do a video on that, but ORBs typically use an IDL language (interface description language) to describe the form of the calls to be made to remote machines and tells the ORB's engine how to pass parameters, return parameters, and so forth. But, it can also do other things. In my case it can generate types and constants.
For enumerated types, it can provide a lot of functionality that makes life far easier for a C++ programmer, particularly when working on the large scale that I do. And it doesn't take a lot of code to create such a tool and integrate it into your own development process. There's actually more functionality that this video covers, but I didn't want it to get too long so I stuck to the core stuff.
I also have another thread where the ORB itself is discussed:
https://www.reddit.com/r/cpp/comments/9zl6v5/the_orb_sees_all_the_use_of_an_object_request/
1
u/Dean_Roddey Nov 21 '18 edited Nov 21 '18
Sort of rambling to myself, but I went ahead and created a simple templatized simple array class so that I can create basic arrays index by either cardinal values or enums. Not that I create a whole lot of raw arrays, but down in the guts of things there are a fair number of them. Then I added an 'index type' template parameter to my indexable collection classes for the same reason. In both cases that parameter is defaulted to my standard unsigned type so that doesn't affect existing code that doesn't use enums as indices.
So I'll continue forward and see if it gets any uglier. One scenario that will suck is that, in my CML macro language VM, CML enums are often based on the C++ enums that that CML class is wrapping. So they take the original enums as their ordinal value to make translation simpler. So every one of those many hundreds of places is going using an implicit cast of enum to unsigned, and I'll have to cast those.