tags:

views:

76

answers:

4

I have an application which needs to define indexes in various .h files in a heirarchical manor. There is a large set of common entries in a single file and a number of variant header files that are pulled in to specific projects.

In file: "base.h"

   #define AAA 1
   #define BBB 2
   #define CCC 3

In many files like: "variant_extended.h"

   #define XXX 4
   #define YYY 5
   #define ZZZ 6

However, this is cumbersome, is the a simple way to carry on from base.h and auto number the defines in the build process?

I am frequently editing the files to reorder or insert new entries which leads to a lot of manual managament. I need to also, ensure the numbers start at 0 and are contiguous. These will be indexes in an array (like mailboxes).

+1  A: 

In base.h, add

#define BASE_LAST CCC

then in your variant_extended.h, use

#include "base.h"

#define XXX (BASE_LAST + 1)
#define YYY (BASE_LAST + 2)
#define ZZZ (BASE_LAST + 3)
John Bode
A: 

You can remove some of the pain by doing something like this:

   //
   // base.h
   //
   #define AAA 1
   #define BBB 2
   #define CCC 3
   #define LAST_BASE CCC

and:

   //
   // extended.h
   //

   #include "base.h"

   #define FIRST_EXTENDED (LAST_BASE + 1)
   #define XXX FIRST_EXTENDED
   #define YYY (FIRST_EXTENDED + 1)
   #define ZZZ (FIRST_EXTENDED + 2)

I suspect there's probably a better way if we had more information though.

Paul R
A: 

Can you use a collection of text files instead?

ValiRossi
+3  A: 

To expand on Jerry's comment:

// base.h
enum BaseCodes
{
   AAA = 1,
   BBB,
   CCC,
   FirstExtendedCode
};

// extended.h
enum ExtendedCodes
{
  XXX = FirstExtendedCode,
  YYY,
  ZZZ
}

In this case, ZZZ is guaranteed to be 6. Insert new members into the enums automatically renumbers all the other members.

Skizz
So simple.. why didn't I think of that earlier. Doh!
JeffV