Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C macro to test: "If more than one defined"

I have several drivers using a resource in my code, of which only one can be defined. eg if I have the following defines: USB_HID, USB_SERIAL, USB_STORAGE. and I want to test that only one is defined, is there a simple way to do this? Currently I am doing it this way:

#ifdef USB_HID
  #ifdef USB_INUSE
    #error "Can only have one USB device"
  #else
    #define USB_INUSE
  #endif
#endif

#ifdef USB_SERIAL
  #ifdef USB_INUSE
    #error "Can only have one USB device"
  #else
    #define USB_INUSE
  #endif
#endif

... with one of these blocks for each USB_XXX driver. Is there more elegant way of doing this?

like image 746
stbtrax Avatar asked Sep 01 '25 10:09

stbtrax


1 Answers

#if defined(USB_HID) + defined(USB_SERIAL) + defined(USB_STORAGE) != 1
#error Define exactly one of USB_HID, USB_SERIAL, USB_STORAGE
#endif
like image 103
Jack Kelly Avatar answered Sep 03 '25 15:09

Jack Kelly