dyn_perfect_fwd.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* Copyright 2006-2014 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. #ifndef BOOST_FLYWEIGHT_DETAIL_DYN_PERFECT_FWD_HPP
  9. #define BOOST_FLYWEIGHT_DETAIL_DYN_PERFECT_FWD_HPP
  10. #if defined(_MSC_VER)
  11. #pragma once
  12. #endif
  13. #include <boost/config.hpp>
  14. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  15. #include <boost/preprocessor/arithmetic/add.hpp>
  16. #include <boost/preprocessor/cat.hpp>
  17. #include <boost/preprocessor/repetition/enum.hpp>
  18. #include <boost/preprocessor/repetition/enum_params.hpp>
  19. #include <boost/preprocessor/repetition/repeat_from_to.hpp>
  20. #include <boost/preprocessor/seq/seq.hpp>
  21. #define BOOST_FLYWEIGHT_PERFECT_FWD_ARG(z,n,_) \
  22. BOOST_PP_CAT(T,n)&& BOOST_PP_CAT(t,n)
  23. #define BOOST_FLYWEIGHT_PERFECT_FWD_N_AUX(n,name,body) \
  24. template<BOOST_PP_ENUM_PARAMS(n,typename T)> \
  25. name(BOOST_PP_ENUM(n,BOOST_FLYWEIGHT_PERFECT_FWD_ARG,~)) \
  26. body((FORWARD)(n))
  27. #define BOOST_FLYWEIGHT_PERFECT_FWD_N(z,n,data) \
  28. BOOST_FLYWEIGHT_PERFECT_FWD_N_AUX( \
  29. n,BOOST_PP_SEQ_HEAD(data), \
  30. BOOST_PP_SEQ_HEAD(BOOST_PP_SEQ_TAIL(data)))
  31. #define BOOST_FLYWEIGHT_PERFECT_FWD_WITH_ARGS(name,body) \
  32. BOOST_PP_REPEAT_FROM_TO( \
  33. 1,BOOST_PP_ADD(BOOST_FLYWEIGHT_LIMIT_PERFECT_FWD_ARGS,1), \
  34. BOOST_FLYWEIGHT_PERFECT_FWD_N,(name)(body))
  35. #define BOOST_FLYWEIGHT_PERFECT_FWD(name,body) \
  36. name()body((ENUM)(0)) \
  37. BOOST_FLYWEIGHT_PERFECT_FWD_WITH_ARGS(name,body)
  38. #else
  39. /* no rvalue refs -> [const] Tn& overloads */
  40. #include <boost/preprocessor/arithmetic/add.hpp>
  41. #include <boost/preprocessor/cat.hpp>
  42. #include <boost/preprocessor/repetition/enum.hpp>
  43. #include <boost/preprocessor/repetition/enum_params.hpp>
  44. #include <boost/preprocessor/repetition/repeat.hpp>
  45. #include <boost/preprocessor/repetition/repeat_from_to.hpp>
  46. #include <boost/preprocessor/seq/elem.hpp>
  47. #include <boost/preprocessor/seq/for_each_product.hpp>
  48. #include <boost/preprocessor/seq/seq.hpp>
  49. #include <boost/preprocessor/seq/size.hpp>
  50. #define BOOST_FLYWEIGHT_CONST(b) BOOST_PP_CAT(BOOST_FLYWEIGHT_CONST,b)
  51. #define BOOST_FLYWEIGHT_CONST0
  52. #define BOOST_FLYWEIGHT_CONST1 const
  53. /* if mask[n]==0 --> Tn& tn
  54. * if mask[n]==1 --> const Tn& tn
  55. */
  56. #define BOOST_FLYWEIGHT_PERFECT_FWD_ARG(z,n,mask) \
  57. BOOST_FLYWEIGHT_CONST(BOOST_PP_SEQ_ELEM(n,mask)) \
  58. BOOST_PP_CAT(T,n)& BOOST_PP_CAT(t,n)
  59. /* overload accepting size(mask) args, where the template args are
  60. * marked const or not according to the given mask (a seq of 0 or 1)
  61. */
  62. #define BOOST_FLYWEIGHT_PERFECT_FWD_MASK_AUX(r,name,body,mask) \
  63. template<BOOST_PP_ENUM_PARAMS(BOOST_PP_SEQ_SIZE(mask),typename T)> \
  64. name( \
  65. BOOST_PP_ENUM( \
  66. BOOST_PP_SEQ_SIZE(mask),BOOST_FLYWEIGHT_PERFECT_FWD_ARG,mask)) \
  67. body((ENUM)(BOOST_PP_SEQ_SIZE(mask)))
  68. #define BOOST_FLYWEIGHT_PERFECT_FWD_MASK(r,data) \
  69. BOOST_FLYWEIGHT_PERFECT_FWD_MASK_AUX( \
  70. r, \
  71. BOOST_PP_SEQ_ELEM(0,BOOST_PP_SEQ_HEAD(data)), \
  72. BOOST_PP_SEQ_ELEM(1,BOOST_PP_SEQ_HEAD(data)), \
  73. BOOST_PP_SEQ_TAIL(data))
  74. #define BOOST_FLYWEIGHT_01(z,n,_) ((0)(1))
  75. /* Perfect forwarding overloads accepting 1 to n args */
  76. #define BOOST_FLYWEIGHT_PERFECT_FWD_N(z,n,data) \
  77. BOOST_PP_SEQ_FOR_EACH_PRODUCT( \
  78. BOOST_FLYWEIGHT_PERFECT_FWD_MASK, \
  79. ((data)) \
  80. BOOST_PP_REPEAT(n,BOOST_FLYWEIGHT_01,~))
  81. #define BOOST_FLYWEIGHT_PERFECT_FWD_WITH_ARGS(name,body) \
  82. BOOST_PP_REPEAT_FROM_TO( \
  83. 1,BOOST_PP_ADD(BOOST_FLYWEIGHT_LIMIT_PERFECT_FWD_ARGS,1), \
  84. BOOST_FLYWEIGHT_PERFECT_FWD_N,(name)(body))
  85. #define BOOST_FLYWEIGHT_PERFECT_FWD(name,body) \
  86. name()body((ENUM)(0)) \
  87. BOOST_FLYWEIGHT_PERFECT_FWD_WITH_ARGS(name,body)
  88. #endif
  89. #endif