base.hpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright (C) 2009-2012 Lorenzo Caminiti
  2. // Distributed under the Boost Software License, Version 1.0
  3. // (see accompanying file LICENSE_1_0.txt or a copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Home at http://www.boost.org/libs/functional/overloaded_function
  6. #if !BOOST_PP_IS_ITERATING
  7. # ifndef BOOST_FUNCTIONAL_OVERLOADED_FUNCTION_DETAIL_BASE_HPP_
  8. # define BOOST_FUNCTIONAL_OVERLOADED_FUNCTION_DETAIL_BASE_HPP_
  9. # include <boost/functional/overloaded_function/config.hpp>
  10. # include <boost/function.hpp>
  11. # include <boost/preprocessor/iteration/iterate.hpp>
  12. # include <boost/preprocessor/repetition/enum.hpp>
  13. # include <boost/preprocessor/cat.hpp>
  14. # include <boost/preprocessor/comma_if.hpp>
  15. #define BOOST_FUNCTIONAL_DETAIL_arg_type(z, n, unused) \
  16. BOOST_PP_CAT(A, n)
  17. #define BOOST_FUNCTIONAL_DETAIL_arg_name(z, n, unused) \
  18. BOOST_PP_CAT(a, n)
  19. #define BOOST_FUNCTIONAL_DETAIL_arg_tparam(z, n, unused) \
  20. typename BOOST_FUNCTIONAL_DETAIL_arg_type(z, n, unused)
  21. #define BOOST_FUNCTIONAL_DETAIL_arg(z, n, unused) \
  22. BOOST_FUNCTIONAL_DETAIL_arg_type(z, n, unused) \
  23. BOOST_FUNCTIONAL_DETAIL_arg_name(z, n, unused)
  24. #define BOOST_FUNCTIONAL_DETAIL_f \
  25. R (BOOST_PP_ENUM(BOOST_FUNCTIONAL_DETAIL_arity, \
  26. BOOST_FUNCTIONAL_DETAIL_arg_type, ~))
  27. // Do not use namespace ::detail because overloaded_function is already a class.
  28. namespace boost { namespace overloaded_function_detail {
  29. template<typename F>
  30. class base {}; // Empty template cannot be used directly (only its spec).
  31. # define BOOST_PP_ITERATION_PARAMS_1 \
  32. (3, (0, BOOST_FUNCTIONAL_OVERLOADED_FUNCTION_CONFIG_ARITY_MAX, \
  33. "boost/functional/overloaded_function/detail/base.hpp"))
  34. # include BOOST_PP_ITERATE() // Iterate over funciton arity.
  35. } } // namespace
  36. #undef BOOST_FUNCTIONAL_DETAIL_arg_type
  37. #undef BOOST_FUNCTIONAL_DETAIL_arg_name
  38. #undef BOOST_FUNCTIONAL_DETAIL_arg_tparam
  39. #undef BOOST_FUNCTIONAL_DETAIL_arg
  40. #undef BOOST_FUNCTIONAL_DETAIL_f
  41. # endif // #include guard
  42. #elif BOOST_PP_ITERATION_DEPTH() == 1
  43. # define BOOST_FUNCTIONAL_DETAIL_arity BOOST_PP_FRAME_ITERATION(1)
  44. template<
  45. typename R
  46. BOOST_PP_COMMA_IF(BOOST_FUNCTIONAL_DETAIL_arity)
  47. BOOST_PP_ENUM(BOOST_FUNCTIONAL_DETAIL_arity,
  48. BOOST_FUNCTIONAL_DETAIL_arg_tparam, ~)
  49. >
  50. class base< BOOST_FUNCTIONAL_DETAIL_f > {
  51. public:
  52. /* implicit */ inline base(
  53. // This requires specified type to be implicitly convertible to
  54. // a boost::function<> functor.
  55. boost::function< BOOST_FUNCTIONAL_DETAIL_f > const& f): f_(f)
  56. {}
  57. inline R operator()(BOOST_PP_ENUM(BOOST_FUNCTIONAL_DETAIL_arity,
  58. BOOST_FUNCTIONAL_DETAIL_arg, ~)) const {
  59. return f_(BOOST_PP_ENUM(BOOST_FUNCTIONAL_DETAIL_arity,
  60. BOOST_FUNCTIONAL_DETAIL_arg_name, ~));
  61. }
  62. private:
  63. boost::function< BOOST_FUNCTIONAL_DETAIL_f > const f_;
  64. };
  65. # undef BOOST_FUNCTIONAL_DETAIL_arity
  66. #endif // iteration