2

I prefer to define certain macros inside of my struct definitions, so it's easy to see possible values for a given member. For example:

typedef struct foo_t {
    uint16_t flags;
        #define FOO_FLAG_BELL       0x0001
        #define FOO_FLAG_BOOK       0x0002
        #define FOO_FLAG_CANDLE     0x0004
        #define FOO_FLAG_LANTERN    0x0008
}

Doxygen wants to list those macros at the top, with all of the other macros. I've make use of the grouping tags (//@{ and //@}) to group these macros together, and named the group with foo_t.flags, but I'd like to find a way to more-closely associate the values with the structure. Should I use \link and \endlink to somehow link to that group name?

1 Answer 1

5

Use enums.

typedef struct foo_t {
  enum flag_define { 
    FOO_FLAG_BELL    =  0x0001,    /**< The flag for the bell or whatever. */
    FOO_FLAG_BOOK    =  0x0002,
    FOO_FLAG_CANDLE  =  0x0004,
    FOO_FLAG_LANTERN =  0x0008,
  } flags:16;                      /**< My flag thing */
}  foo_t;
3
  • +1: that could work for some situations. In other cases, I need to define a name as a combination of other names, or two names to be the same value, or the enum values are for an 8-bit field in a frame header. Best answer so far though...
    – tomlogic
    Jun 25, 2010 at 21:34
  • you can have enums with the same value, there's no restriction there. And you can by combining with the preprocessor concatenation operator ##. You can also reuse a value to define another: enum { val1=1, val2=2, combined=val1|val2, }; should work. Jun 26, 2010 at 15:00
  • It is a beautiful construct, thanks I was looking exactly for something like this to make our new coding standard.
    – dashesy
    Mar 21, 2013 at 16:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.