Dill
Dill

Reputation: 2025

combine enums in different modules

I got a number of modules that contain enums. (they hold signals that are used in a state-machine).

moduleAsignals.h:

enum ModuleASignals {
  modASig1,   
  modASig2,   
  ...
  modASigN,   
};

moduleBsignals.h:

enum ModuleBSignals {
  modBSig1,   
  modBSig2,   
  ...
  modBSigM,   
};

Each module has an arbitrary number of signals.

Now i would like to combine a random choice of modules in one application. The problem is, that all signals have to be globally visible and that the signals have to stay unique. Also there is a limit to the size of one signal (8bit), so i can't just give unique offsets to the single modules' enums.

How can i combine the single enums into one without having to adapt the signal-header files for each application? One way would be to just put the elements (without "enum ... {") into a file and include these fragments, but this will result in code that the IDE can't understand and so will lead to some inconvenience.

Upvotes: 4

Views: 1756

Answers (2)

byako
byako

Reputation: 3422

If it if acceptable to have one .h include the other, you can write

enum ModuleBSignals {
  modBSig1 = modASigN+1,   
  modBSig2,   
  ...
  modBSigM,   
};

The second enum will thus start just after the first one.

Added:

You can try parameterizing all your modules with a dummy first element:

#include "sig_start.h"  // Default one contains #define START 0

enum ModuleBSignals {
  modBSig1 = START,
...

And it becomes the responsibility of your build process to create a correct sig_start.h for each one of your modules directories. The N+1 th sig_start.h will contain

#include module1signals.h
...
#include moduleNsignals.h
#define START modNSigM+1

Upvotes: 4

Christoph
Christoph

Reputation: 169673

You could use a single enum and enable specific modules via preprocessor:

enum Signals {
#ifdef USE_MODULE_A
  modASig1,   
  modASig2,   
  ...
  modASigN,   
#endif
#ifdef USE_MODULE_B
  modBSig1,   
  modBSig2,   
  ...
  modBSigM,   
#endif
};

This is mostly equivalent to your solution via code fragments, but might be more IDE friendly...

Upvotes: 2

Related Questions