Currently I'm working on writing some code that should be easily expandable. To add a new entry the user must add:
- The name of the new entry
- The size of the new entry
The name is only needed for populating an enum, the size is needed to reserve some space to store the value of it in.
Frankly I'm not really sure this is even possible as I'm effectively asking if preprocessors can properly split/separate symbols and convert it to somewhat boilerplate code.
So, for example, I'd like to add the following entry:
DECLARE_ENTRY(downlinkCounter, sizeof(uint32_t))
DECLARE_ENTRY(uplinkCounter, sizeof(uint32_t))
Or perhaps:
#define ENTRIES downlinkCounter, sizeof(uint32_t), uplinkCounter, sizeof(uint32_t)
Or:
#define NAME_ENTRIES downlinkCounter, uplinkCounter,
#define SIZE_ENTRIES sizeof(uint32_t), sizeof(uint32_t)
(The last option is not preferred, I prefer to pair the name and size closely)
I'd like this to expand to the following in the header file:
typedef enum {
downlinkCounter,
uplinkCounter,
} eEntries_t;
And to expand to this in the source file:
typedef struct {
uint8_t downlinkCounter[sizeof(uint32_t)];
uint8_t uplinkCounter[sizeof(uint32_t)];
} sEntries_t;
Can I even do this with C preprocessor? Or will I have to type this out?
Thanks for your help!