☰
Current Page
Main Menu
Home
Home
Editing CodeGeneration
Edit
Preview
H1
H2
H3
default
Set your preferred keybinding
default
vim
emacs
markdown
Set this page's format to
Markdown
Plain Text
Pod
RDoc
reStructuredText
Rendering unavailable for
AsciiDoc
BibTeX
Creole
MediaWiki
Org-mode
Textile
Help 1
Help 1
Help 1
Help 2
Help 3
Help 4
Help 5
Help 6
Help 7
Help 8
Autosaved text is available. Click the button to restore it.
Restore Text
--- title: CodeGeneration --- Code generation in C with the pre-processor. # Applied X-Macros * Based on [luser-droog][1]'s [answer][2] on StackOverflow * Code generation with Meta-X-Macros #include <stdio.h> #define ERRORS(_) \ _(NO_ERROR, 0x00) \ /* discontiguous */ \ _(EMPTY, 0x02) \ _(OVERFLOW, 0x03) \ _(TIMEOUT, 0x04) \ /** X-Macro to define ERROR_<FOO> enum entries */ #define AS\_ENUM(name, ID) ERROR\_##name = ID, /** X-Macro to define {"ERROR_FOO", ID} struct table entries */ #define AS_LIST(name, ID) {#name, ID}, /** Register Enum */ enum ERROR { /* Generate enum elements with X-Macro */ ERRORS(AS_ENUM) ERROR_MAX }; /** * Error List Generated by X-Macro @ref AS_LIST. * * Works with non-contiguous sets. */ static const struct ErrorEntry { char* name; enum ERROR id; } ERROR_LIST[] = { ERRORS(AS_LIST) }; void PrintAll(void) { size_t idx = ; for (idx = ; idx < sizeof(ERROR_LIST)/sizeof(ERROR_LIST[]); idx++) { const struct ErrorEntry* reg = &ERROR_LIST[idx]; printf("Error message 0x%x is \"%s\"\n", reg->id, reg->name); } } int main(void) { PrintAll(); } ## Method 1. ERRORS(AS_ENUM) expands to enum ERROR { /* Generate enum elements with X-Macro */ AS_ENUM(NO_ERROR, 0x00) \ AS_ENUM(EMPTY, 0x02) \ ... ERROR_MAX }; 2. Each of these is then expanded again by the preprocessor to enum ERROR { ERROR\_NO\_ERROR = 0x00, ERROR_EMPTY = 0x02, ... ERROR_MAX }; 3. Similar expansion applies for the ERROR_LIST struct list entries # Protocol Messages * Generate struct serialization/deserialization routines * <https://github.com/cognomen/C-MsgGen> [1]: http://stackoverflow.com/users/733077/luser-droog [2]: http://stackoverflow.com/a/6636596 [4]: CodeGeneration?action=sourceblock&num=1 [5]: CodeGeneration?action=sourceblock&num=2 [6]: CodeGeneration?action=sourceblock&num=3 <!-- vim: filetype=markdown -->
Uploading file...
Sidebar
# SideBar * [Home][1] * [Projects][2] * * * <!-- --> * [Code][3] * [Tech][4] * [Network][5] * [MediaCentre][6] * [UAV][7] * * * <!-- --> * [Travel][8] * [Music][9] * [Horse Riding][10] * [Study][11] * [Games][12] * [Other Activities][13] * * * <!-- --> * [Car][14] * [House][15] * [Watch][16] * [Clothing][17] * [Miscellany][18] * * * [1]: /Home [2]: /Projects [3]: /Code/Code [4]: /Tech/Tech [5]: /Network/Network [6]: /MediaCentre/MediaCentre [7]: /UAV/UAV [8]: /Travel/Travel [9]: /Music/Music [10]: /HorseRiding/HorseRiding [11]: /Study/Study [12]: /Games/Games [13]: /Do/Do [14]: /Car/Car [15]: /House/House [16]: /Watch/Watch [17]: /Clothing/Clothing [18]: /Miscellany/Miscellany <!-- vim: filetype=markdown -->
Edit message:
Cancel