I have an application which needs to define indexes in various .h files in a hierarchical manner. 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 there 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 manage开发者_开发问答ment. I need to also, ensure the numbers start at 0 and are contiguous. These will be indexes in an array (like mailboxes).
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.
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)
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.
精彩评论