Real-world use of X-Macros

前端 未结 7 1269
花落未央
花落未央 2020-11-22 12:21

I just learned of X-Macros. What real-world uses of X-Macros have you seen? When are they the right tool for the job?

7条回答
  •  被撕碎了的回忆
    2020-11-22 12:32

    Chromium has an interesting variation of a X-macro at dom_code_data.inc. Except it's not just a macro, but an entirely separate file. This file is intended for keyboard input mapping between different platforms' scancodes, USB HID codes, and string-like names.

    The file contains code like:

    DOM_CODE_DECLARATION {
    
      //            USB     evdev    XKB     Win     Mac   Code
      DOM_CODE(0x000000, 0x0000, 0x0000, 0x0000, 0xffff, NULL, NONE), // Invalid
    ...
    };
    

    Each macro invocation actually passes in 7 arguments, and the macro can choose which arguments to use and which to ignore. One usage is to map between OS keycodes and platform-independent scancodes and DOM strings. Different macros are used on different OSes to pick the keycodes appropriate for that OS.

    // Table of USB codes (equivalent to DomCode values), native scan codes,
    // and DOM Level 3 |code| strings.
    #if defined(OS_WIN)
    #define DOM_CODE(usb, evdev, xkb, win, mac, code, id) \
      { usb, win, code }
    #elif defined(OS_LINUX)
    #define DOM_CODE(usb, evdev, xkb, win, mac, code, id) \
      { usb, xkb, code }
    #elif defined(OS_MACOSX)
    #define DOM_CODE(usb, evdev, xkb, win, mac, code, id) \
      { usb, mac, code }
    #elif defined(OS_ANDROID)
    #define DOM_CODE(usb, evdev, xkb, win, mac, code, id) \
      { usb, evdev, code }
    #else
    #define DOM_CODE(usb, evdev, xkb, win, mac, code, id) \
      { usb, 0, code }
    #endif
    #define DOM_CODE_DECLARATION const KeycodeMapEntry usb_keycode_map[] =
    #include "ui/events/keycodes/dom/dom_code_data.inc"
    #undef DOM_CODE
    #undef DOM_CODE_DECLARATION
    

提交回复
热议问题