Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

combine enums in different modules

Tags:

c

enums

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.

like image 931
Dill Avatar asked Oct 22 '25 11:10

Dill


2 Answers

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
like image 160
byako Avatar answered Oct 25 '25 03:10

byako


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...

like image 43
Christoph Avatar answered Oct 25 '25 01:10

Christoph



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!