|
Boost : |
From: Tobias Schwinger (tschwinger_at_[hidden])
Date: 2006-10-24 16:24:41
Hi,
some of you might like this pattern - it might be even practical at places...
Maybe it's new, at least I couldn't find anything about it on the web.
Regards,
Tobias
// read bottom up
// ---- "library" code
#include <boost/preprocessor/comparison/equal.hpp>
#include <boost/preprocessor/control/iif.hpp>
#include <boost/preprocessor/tuple/elem.hpp>
#include <boost/preprocessor/cat.hpp>
// public
#define ARG(i,args,deflt) \
BOOST_PP_TUPLE_ELEM(2,1,BOOST_PP_CAT(SEARCH_A_ ## i args (i,deflt),_END))
// private
#define SEARCH_A_0(k,v) BOOST_PP_IIF(BOOST_PP_EQUAL(0,k),(k,v) SKIP_A, SEARCH_B_0)
#define SEARCH_B_0(k,v) BOOST_PP_IIF(BOOST_PP_EQUAL(0,k),(k,v) SKIP_A, SEARCH_A_0)
#define SEARCH_A_1(k,v) BOOST_PP_IIF(BOOST_PP_EQUAL(1,k),(k,v) SKIP_A, SEARCH_B_1)
#define SEARCH_B_1(k,v) BOOST_PP_IIF(BOOST_PP_EQUAL(1,k),(k,v) SKIP_A, SEARCH_A_1)
#define SEARCH_A_2(k,v) BOOST_PP_IIF(BOOST_PP_EQUAL(2,k),(k,v) SKIP_A, SEARCH_B_2)
#define SEARCH_B_2(k,v) BOOST_PP_IIF(BOOST_PP_EQUAL(2,k),(k,v) SKIP_A, SEARCH_A_2)
// ...
#define SKIP_A(k,v) SKIP_B
#define SKIP_B(k,v) SKIP_A
#define SKIP_A_END
#define SKIP_B_END
// ---- macro with named parameters example
// public
#define MY_FIRST_ARG(value) (0,value)
#define MY_SECOND_ARG(value) (1,value)
#define MY_THIRD_ARG(value) (2,value)
#define MY_MACRO(args) \
MY_MACRO_IMPL( ARG(0,args,1st_default), ARG(1,args,2nd_default), ARG(2,args,3rd_default) )
// private
#define MY_MACRO_IMPL(arg1,arg2,arg3) \
arg1 arg2 arg3
// note: parameter names can be reused among macros
// ---- client code
MY_MACRO(
MY_FIRST_ARG(yaba)
MY_THIRD_ARG(doo)
MY_SECOND_ARG(daba)
)
// expands to: yaba daba doo
MY_MACRO(
MY_SECOND_ARG(bubu)
)
// expands to: 1st_default bubu 3rd_default.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk