how to comment values under #ifdef in one place

203 views Asked by At

let's imagine we have a C++ project that should work on several platforms (for example, arm/arm64) and we need to have target-specific values for each of them.

Right now we do:

#ifdef PLATFORM_ARM
#define TIMEOUT_VALUE 0
#define OUR_MAGIC_VALUE 1
#elif PLATFORM_ARM64
#define TIMEOUT_VALUE 2
#define OUR_MAGIC_VALUE 3
#endif 

where could I place a comment for each defined name that it could be accessed from each definition?

Note: I can't define each value in its own #ifdef like

// a comment for TIMEOUT_VALUE
#ifdef PLATFORM_ARM
#define TIMEOUT_VALUE 0
#elif PLATFORM_ARM64
#define TIMEOUT_VALUE 2
#endif 

// a comment for OUR_MAGIC_VALUE
#ifdef PLATFORM_ARM
#define OUR_MAGIC_VALUE 1
#elif PLATFORM_ARM64
#define OUR_MAGIC_VALUE 2
#endif

because I have lists and trees of such values.

Thank you.

Edit 1: for example, we have 6 targets and 4 of them support a FEATURE, so we write:

#if defined(ARM)
#define FEATURE 1
#elif defined(ARM64)
#define FEATURE 0
#elif define(MIPS)
#define FEATURE 1
etc... for other platforms.

then I have code that reads this define somewhere:

#if FEATURE
     do something. Note that this part can't be described in a target specific file, because it can have the same implementation for several targets.
#endif 

and now I want to have a place to describe in general what this FEATURE means and do.

1

There are 1 answers

4
user7860670 On BEST ANSWER

You can define a proxy macro and write a single comment for macro to be used by end user:

#ifdef PLATFORM_ARM
#define TIMEOUT_VALUE_IMPL 0
#define OUR_MAGIC_VALUE_IMPL 1
#elif PLATFORM_ARM64
#define TIMEOUT_VALUE_IMPL 2
#define OUR_MAGIC_VALUE_IMPL 3
#endif 

// a comment for TIMEOUT_VALUE
#define TIMEOUT_VALUE TIMEOUT_VALUE_IMPL

// a comment for OUR_MAGIC_VALUE
#define OUR_MAGIC_VALUE OUR_MAGIC_VALUE_IMPL

You may also consider using constants instead of macros.