__FB_ARG_COUNT__( args... )
Counts the number arguments in an argument list
- always returns a value
- if argument list is empty, returns zero (0)
- if argument list has at exactly one argument, returns one (1)
- if argument list has commas ',', return number of commas plus 1
Code: Select all
#macro m( args... )
print __FB_ARG_COUNT__( args )
#endmacro
m()
m(1)
m(1,2)
m(,2)
m(,)
'' etc
__FB_JOIN__( arg1, arg2 )
Joins two token arguments together as one, similar to token pasting operator
- will resolve arguments before joining
- compare differences in following example
- does not accept empty arguments
Code: Select all
#define PREFIX p
#define SUFFIX _T
'' this won't work - arguments not expanded
#define makename1( x ) PREFIX##x##SUFFIX
'' this will work - can do this in older versions of fbc too
#define join( a, b ) a##b
#define makename2( x ) join( PREFIX, join( x, SUFFIX ) )
'' built in __FB_JOIN__() -- works pretty much like join() above
#define makename3( x ) __FB_JOIN__( PREFIX, __FB_JOIN__( x, SUFFIX ) )
#macro dump( arg )
#print #arg
#endmacro
dump( makename1(text) )
dump( makename2(text) )
dump( makename3(text) )
Returns left or right tokens based on separator
Code: Select all
#macro count( range )
scope
dim x as integer = __FB_ARG_LEFTOF__( range, TO )
dim y as integer = __FB_ARG_RIGHTOF__( range, TO )
dim s as integer = sgn(y - x)
print "Counting " & #range
for i as integer = x to y step s
print i
next
end scope
#endmacro
count( 4 to 10 )
count( 7 to 2 )
__FB_UNIQUEID_PUSH__(ID), __FB_UNIQUEID__(ID), __FB_UNIQUEID_POP__(ID)
For a good size example see ./examples/misc/trycatch/ in the freebasic examples directory.
Code: Select all
namespace crt
#include once "crt/setjmp.bi"
end namespace
'' example how to implement a try/catch using the C runtime setjmp()/longjmp(), plus the __FB_UNIQUEID_###__() and __FB_ARG_###__() builtin macros
#macro TRY
scope
__FB_UNIQUEID_PUSH__(__tc__)
dim __FB_UNIQUEID__(__tc__) as TryCatch
if crt.setjmp(@__FB_UNIQUEID__(__tc__).buf) = 0 then
#endmacro
#macro CATCH(x)
elseif *cast(object ptr, __FB_UNIQUEID__(__tc__).ex) is __FB_ARG_RIGHTOF__(x, AS) then
var __FB_ARG_LEFTOF__(x, AS) = cast(__FB_ARG_RIGHTOF__(x, AS) ptr, __FB_UNIQUEID__(__tc__).ex)
#endmacro
#macro THROW(x)
__FB_UNIQUEID__(__tc__).throw_(x, __FILE__, __FUNCTION__, __LINE__)
#endmacro
#macro END_TRY
end if
__FB_UNIQUEID_POP__(__tc__)
end scope
#endmacro