enums galore

This question can be applied equally well to C but maybe there is a unique C++ solution to this. The problem I am having deals with using hugh enumeration structures as indexers into other structures and arrays. I have been tasked with cleaning up a previous employee's C++ code (oh joy). He used a technique of using enumerations as indexers into arrays and structures. I think it's a good technique albeit a bear to maintain because of the numbers of different enumerations and affected arrays. The problem rears it's ugly head whenever a change is necessary. Changing the arrays (adding, deleting, rearranging) means the enumerations must be modified the exact same way or everything becomes a jumbled mess.

So here are my questions:
Is there a better way to incorporate enumerations as indexers while making maintenance a breeze?
Is there perhaps a better technique than using enumerations at all?
Is there some C++ specific technique that would automatically adjust enumeration indexers?

Thanks for any help you might offer.

How about using STL map containers instead of the arrays? You could keep the enums and use them as the first element of the map element with the contents of the array as the second element. As long as the correct enum and data are paired when the data is put in the std::map (which is done in a single call) you should be OK. The map automatically handles the indexing for you so you can do any searches using the enum. Downside would be quite a lot of coding to swap from one to the other but once it's done, maintenance would be a lot easier.

Have a look here for more info
http://www.cplusplus.com/reference/stl/map/
You are right about the work but it may be worth it since I'm supposed to "clean up" this project anyway and maintenance is a big factor. The thought had crossed my mind for a fleeting instant. I'm going to revisit the idea. Thanks for the reminder and your quick response.
Oops! Now I remember why using maps or anything from the STL was just a fleeting thought. I'm using IAR embedded C++ which excludes the entire STL! I suppose I could create my own map class. Any other ideas?
What platform are you on? I've always associated IAR with small stuff, microcontrollers and the like. Could you change to gcc, g++?
We're using the Atmel AT90CAN128 micro. It is a small project for marine craft CAN networked instrumentation.
Well changing compiler is out then, IAR is about the best around for the Atmel micros. I would be tempted to do what you said, write your own cut down map, maybe a linked list. It would keep the code fairly small which is always good with micros. It would probably be better on your memory resource use than large arrays as well. Sorry I can't think of anything better.
Yeah, this is one of my pet peeves about C++: that enums aren't "real" types. What I'll often do is something like this:

1
2
3
4
5
6
7
enum SomeEnum {
   FirstValue = 0,
   SecondValue = 1,
   ThirdValue = 2,
   FourthValue = 3,
   SomeEnum_NUM_VALUES
};


Then if I want to write a function that converts a SomeEnum to a human-readable string, for example, I'll do:

1
2
3
4
5
6
7
8
9
10
11
12
#include <boost/static_assert.hpp>

string ConvertSomeEnum( SomeEnum val ) {
    // If you get a compile error on the following line, it means that someone
    // changed SomeEnum (added or removed values) and forgot to update
    // this function.
    BOOST_STATIC_ASSERT( SomeEnum_NUM_VALUES == 4 );

    switch( val ) {
       ...
    }
}


If you're not familiar with BOOST_STATIC_ASSERT, it is a compile-time assertion. That is, the code will not compile if the assertion is false.
Since you more than likely don't have the Boost libraries since you don't have STL, you can implement your own static assert as follows (I'm doing this from memory because I don't have Boost installed on my windows box):

1
2
3
4
5
6
7
8
9
10
11
12
13
template< bool b >
struct StaticAssert;

template<>
struct StaticAssert< true > {};

template<>
struct StaticAssert< false > {
    char this_will_generate_compile_error[ -1 ];
};

#define MY_STATIC_ASSERT( cond ) \
 { struct StaticAssert<cond> compile_time_assertion; } 


Just some thoughts... not sure if they'll be useful to you.
Topic archived. No new replies allowed.