Iterative Variadric Macro

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
Mysoft
Posts: 836
Joined: Jul 28, 2005 13:56
Location: Brazil, Santa Catarina, Indaial (ouch!)
Contact:

Iterative Variadric Macro

Post by Mysoft »

so... freebasic have variadric macros... and unlike C, freebasic supports #preprocessor definition inside preprocessor... the only thing blocking a Iterative Variadric Macro, is because freebasic blocks recursive defines...

BUT! you can fool it to accept that, and call a macro, with a variable number of parameters, where another macro will act on each of those parameters, that allow to build unrolled check lists, and a bunch of other things that are very useful for debugging and other advanced usages...

Code: Select all

#macro warn(_S)
  #print _S
#endmacro

#macro multi(_N,_S,_M...)  
  #macro multi2(_S2,_M2...)
    _N(#_S2)
    #if #_M2 = ""
      #undef multi2
      #define multi2 rem
    #endif
    multi2(_M2)
  #endmacro
  multi2(_S,_M)  
  #undef Multi2  
#endmacro  

multi(warn,Hi)
multi(warn,Hello,Foo,World)
multi(warn,Abra,Kadabra,Alakazam)
print "Runtime"
so this passes following parameters to a macro named on the first parameter... i.e. it will pass to warn() each of the rest of the parameters...
i'm not sure if its possible to make that simpler... but the "multi" is kinda generic to be used as a "FOR_EACH" for other macros...
Post Reply