dyn_perfect_fwd.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* Copyright 2006-2008 Joaquin M Lopez Munoz.
  2. * Distributed under the Boost Software License, Version 1.0.
  3. * (See accompanying file LICENSE_1_0.txt or copy at
  4. * http://www.boost.org/LICENSE_1_0.txt)
  5. *
  6. * See http://www.boost.org/libs/flyweight for library home page.
  7. */
  8. /* no include guards */
  9. #include <boost/preprocessor/arithmetic/add.hpp>
  10. #include <boost/preprocessor/cat.hpp>
  11. #include <boost/preprocessor/repetition/enum.hpp>
  12. #include <boost/preprocessor/repetition/enum_params.hpp>
  13. #include <boost/preprocessor/repetition/repeat.hpp>
  14. #include <boost/preprocessor/repetition/repeat_from_to.hpp>
  15. #include <boost/preprocessor/seq/elem.hpp>
  16. #include <boost/preprocessor/seq/for_each_product.hpp>
  17. #include <boost/preprocessor/seq/size.hpp>
  18. #define BOOST_FLYWEIGHT_CONST(b) BOOST_PP_CAT(BOOST_FLYWEIGHT_CONST,b)
  19. #define BOOST_FLYWEIGHT_CONST0
  20. #define BOOST_FLYWEIGHT_CONST1 const
  21. /* if mask[n]==0 --> Tn& tn
  22. * if mask[n]==1 --> const Tn& tn
  23. */
  24. #define BOOST_FLYWEIGHT_PERFECT_FWD_ARG(z,n,mask) \
  25. BOOST_FLYWEIGHT_CONST(BOOST_PP_SEQ_ELEM(n,mask)) \
  26. BOOST_PP_CAT(T,n)& BOOST_PP_CAT(t,n)
  27. /* overload accepting size(mask) args, where the template args are
  28. * marked const or not according to the given mask (a seq of 0 or 1)
  29. */
  30. #define BOOST_FLYWEIGHT_PERFECT_FWD(r,mask) \
  31. template<BOOST_PP_ENUM_PARAMS(BOOST_PP_SEQ_SIZE(mask),typename T)> \
  32. BOOST_FLYWEIGHT_PERFECT_FWD_NAME( \
  33. BOOST_PP_ENUM( \
  34. BOOST_PP_SEQ_SIZE(mask),BOOST_FLYWEIGHT_PERFECT_FWD_ARG,mask)) \
  35. BOOST_FLYWEIGHT_PERFECT_FWD_BODY(BOOST_PP_SEQ_SIZE(mask))
  36. #define BOOST_FLYWEIGHT_01(z,n,_) ((0)(1))
  37. /* Perfect forwarding overloads accepting 1 to n args */
  38. #define BOOST_FLYWEIGHT_PERFECT_FWDS_N(z,n,_) \
  39. BOOST_PP_SEQ_FOR_EACH_PRODUCT( \
  40. BOOST_FLYWEIGHT_PERFECT_FWD, \
  41. BOOST_PP_REPEAT(n,BOOST_FLYWEIGHT_01,~))
  42. #define BOOST_FLYWEIGHT_PERFECT_FWD_OVERLOADS \
  43. BOOST_PP_REPEAT_FROM_TO( \
  44. 1,BOOST_PP_ADD(BOOST_FLYWEIGHT_LIMIT_PERFECT_FWD_ARGS,1), \
  45. BOOST_FLYWEIGHT_PERFECT_FWDS_N,~)
  46. /* generate the overloads */
  47. BOOST_FLYWEIGHT_PERFECT_FWD_OVERLOADS
  48. /* clean up */
  49. #undef BOOST_FLYWEIGHT_PERFECT_FWD_OVERLOADS
  50. #undef BOOST_FLYWEIGHT_01
  51. #undef BOOST_FLYWEIGHT_PERFECT_FWD
  52. #undef BOOST_FLYWEIGHT_PERFECT_FWD_ARG
  53. #undef BOOST_FLYWEIGHT_CONST1
  54. #undef BOOST_FLYWEIGHT_CONST0
  55. #undef BOOST_FLYWEIGHT_CONST
  56. /* user supplied argument macros */
  57. #undef BOOST_FLYWEIGHT_PERFECT_FWD_BODY
  58. #undef BOOST_FLYWEIGHT_PERFECT_FWD_NAME