c - Collecting a list of function pointers and other ancillary data via distinct macro invocations -


i want declare number of functions same signature, possibly spread across different header files, , use preprocessor collect array pointers such functions, , create an array of corresponding function names.

so, example, assuming common signature int func(int x) have following declarations:

func(a) {   return x + x; }  func(b) {   return x >> 1; }  func(c) {   return x ^ 123; } 

... , through macro magic, end after preprocessing looks like:

int a(int x) {   return x + x; }  int b(int x) {   return x >> 1; }  int c(int x) {   return x ^ 123; }  typedef int (*intfunc_t)(int)  const intfunc_t func_ptrs[] = { &a, &b, &c };  const char *func_names[] = { "a", "b", "c" }; 

a worse version of possible using x-macros, requires list like:

#define x_macro \   x(a) \   x(b) \   x(c) 

this duplicates information encoded in func(a) definitions , makes tough use approach unknown number of functions spread across many headers, since central list needs kept in sync.

so i'm hoping there way have func macro automatically generate lists.

it doesn't have compile-time constructed arrays shown, either. allows me iterate on function pointers, , name , possibly other ancillary information (added via additional args func) work, e.g., linked list initialized @ startup. can assume func macros appear in same compilation unit.

while i'm not interested in gcc-specific solutions __attribute__((constructor)).

here attempt solve problem.
minimal adaptation of x-macros. difference might make useable you:

  • the actual functional code stays in distributed headers.
  • the core list needs adapted total number of such functions
    (and names, if not single letter alphabet),
    i.e. not mirror functionality.
  • that adapation number of functions not require editing core implementation.

here code, in 1 "file", hints files split.

/* content of distributed header a.h */ #define func_a_implementation()              \ {                                            \     return x+x;                              \ }   /* content of distributed header b.h */ #define func_b_implementation()              \ {                                            \     return x>>1   ;                          \ }   /* content of distributed header c.h */ #define func_c_implementation()              \ {                                            \     return parcode;                          \ }   /* content of core editable header */ /* #include here headers a.h, b.h c.h    potentially d.h, e.h ,  f.h in future  */      #define magic_list(parflavor) \ entry_##parflavor(a) \ entry_##parflavor(b) \ entry_##parflavor(c) \  /* add these when needed entry_##parflavor(d)  \ entry_##parflavor(e)  \ entry_##parflavor(f)  \ */   /* content of core implementation */ /* include editable core header   */ #define entry_implementation(parid) \ int func##parid (int x) \ func_##parid##_implementation()  #define entry_pointer(parid)        \ &func##parid,  #define entry_name(parid)        \  "func"#parid,  magic_list(implementation)  const intfunc_t func_ptrs[] = {     magic_list(pointer)     null }  const char *func_names[] = {     magic_list(name)     "" } 

when preprocessing this, result is:

$mingw\bin\gcc -e macros.c

# 1 "macros.c" # 1 "<built-in>" # 1 "<command-line>" # 1 "macros.c" # 50 "macros.c" int funca (int x) { return x+x; } int funcb (int x) { return x>>1 ; } int funcc (int x) { return x^123; }  const intfunc_t func_ptrs[] = {  &funca, &funcb, &funcc,  null }  const char *func_names[] = {  "func""a", "func""b", "func""c",  "" } 

i added final entry arrays, avoid intialisation ending in ",". think compilers allow that.
expect compilers replace "func""a" "funca". not short identifiers. if compiler not expected, remove "func" everywhere , require ids long enough.

expanded macros never have pretty indentation...


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -