Boost logo

Boost :

From: Daniel Wallin (dalwan01_at_[hidden])
Date: 2005-05-10 16:56:18


Jonathan Turkanis wrote:
[...]
> Yes, I'm interested. I'm pretty comfortable implementing the current feature set
> with above syntax. I'd certainly be interested in you ideas about extensibility,
> however.

Here's some PP-code that allows a pretty optimal syntax. Works with GCC
and probably nothing else, but maybe it's possible to port to other
preprocessors.

  #include <boost/preprocessor/seq.hpp>
  #include <boost/preprocessor/tuple.hpp>
  #include <boost/preprocessor/control/while.hpp>
  #include <boost/preprocessor/detail/is_unary.hpp>
  #include <boost/preprocessor/arithmetic/add.hpp>

  #define GET(x) x, ~
  #define GET_II(x) GET_III(x)
  #define GET_III(x, y) x
  #define MACRO_D(x, y) (BOOST_PP_SEQ_TAIL(x), \
    BOOST_PP_SEQ_PUSH_BACK(y, BOOST_PP_SEQ_HEAD(x)))
  #define MACRO(n, x) MACRO_D(BOOST_PP_TUPLE_ELEM(2, 0, x), \
    BOOST_PP_TUPLE_ELEM(2, 1, x))
  #define PRED(n, x) BOOST_PP_IS_UNARY(BOOST_PP_TUPLE_ELEM(2, 0, x))

  #define INTERFACE_IMPL_II(x) IS_CONST(x none)
  #define INTERFACE_IMPL(x) [ qualifiers: \
    INTERFACE_IMPL_II(BOOST_PP_TUPLE_ELEM(2, 0, x)) ] [ args: \
    BOOST_PP_TUPLE_ELEM(2, 1, x) ]
  #define INTERFACE_II(x) INTERFACE_IMPL(BOOST_PP_WHILE(PRED, MACRO, x))
  #define INTERFACE(x) INTERFACE_II((x, BOOST_PP_EMPTY()))

  #define EAT_CONST_const
  #define EAT_CONST_volatile
  #define EAT_CONST_none none

  #define IS_CONST_II_const 1, ~
  #define IS_CONST_II_volatile 2, ~
  #define IS_CONST_II_none 0, ~
  #define IS_CONST_III(x, y) x
  #define IS_CONST_II(x) IS_CONST_III(x)
  #define IS_CONST_DO(x) IS_CONST_II(IS_CONST_II_ ## x)
  #define IS_CONST_DO_I(x) IS_CONST_DO(x)
  #define IS_CONST(x) BOOST_PP_ADD(IS_CONST_DO(x), \
    IS_CONST_DO_I(EAT_CONST_ ## x))

  INTERFACE(
      (char&)(int) const volatile
  )

  INTERFACE(
      (char&)(int) const
  )

  INTERFACE(
      (char&)(int) volatile
  )

  INTERFACE(
      (char&)(int)
  )

preprocessing this with gcc results in:

  [ qualifiers: 3 ] [ args: (char&)(int) ]
  [ qualifiers: 1 ] [ args: (char&)(int) ]
  [ qualifiers: 2 ] [ args: (char&)(int) ]
  [ qualifiers: 0 ] [ args: (char&)(int) ]

-- 
Daniel Wallin

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk