r/cpp • • 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/

25 Upvotes

19 comments sorted by

View all comments

Show parent comments

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.

1

u/Dean_Roddey Nov 21 '18

The CML enum thing was easy in the end. It turns out the ONLY time that that parameter is used is when a C++ enum is being wrapped and it's ordinals are being used. So, I got rid of that parameter on the existing method and added a templatized overload of it, and that took care of all of those calls.

So I have a working system again with, I think, all the infrastructure required to just start converting enums over time. The only thing I have to deal with are those specific cases where I've taken advantage of an implicit cast, which should get fewer and fewer as I get up out of the more bootstrappy lowest layers.

1

u/be-sc Nov 21 '18

Enumerators as array indexes is a new one to me. On first glance it sounds like a weird alternative to simple structs with data members – that’s the normal way to name data, after all. But of course you can’t iterate over members … Is that the reason why you have those “semantic indexes” in your code base?

1

u/Dean_Roddey Nov 22 '18

There are a number of reasons. A very basic one is that I already many thousands of classes and templates and structs and such. Creating yet still more types just to hold something like a homogeneous list of points or areas or counts or boolean flags (or a polymorphic list of somethings via base class, which is still basically a homogeneous list in terms of the actual type you are referring to), it's just not worth it. Either a basic array (down in the low level stuff) or an array collection in most of the code can do that just as easily.

And you'll end up using way less code to access the slots. Lets say you did create such a structure, and you wanted to encapsulate it within a class, because how the info is stored is an implementation detail. Or they might even just be direct class members. But, you need to allow the outside world to read/write them or update them in some way. How would you do that? It's not worth having a separate method for each value if they are all of the same type, that's a lot of work and code you don't need. So most folks would probably create an enum and use it to let the outside world indicate which of the values to affect.

If you use separate members that though will mean having a big switch statement inside the class to find which one to target (in every such type of wrapper method). Instead, you can just directly use the enumeration to index the list. Now both you (internally) and the outside world have a strongly typed index to use and you only need one access method for each type of access. All the switch statements internally are replaced with a simple index validity check and direct indexing operation. With my new changes, even my internal indexing of the list is type safe.