make_cons.hpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  3. http://spirit.sourceforge.net/
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. =============================================================================*/
  7. #ifndef BOOST_SPIRIT_MAKE_CONS_OCTOBER_16_2008_1252PM
  8. #define BOOST_SPIRIT_MAKE_CONS_OCTOBER_16_2008_1252PM
  9. #if defined(_MSC_VER)
  10. #pragma once
  11. #endif
  12. #include <boost/mpl/eval_if.hpp>
  13. #include <boost/fusion/include/cons.hpp>
  14. #include <boost/type_traits/remove_const.hpp>
  15. #include <boost/type_traits/is_abstract.hpp>
  16. #include <boost/type_traits/is_function.hpp>
  17. #include <boost/type_traits/add_reference.hpp>
  18. #include <boost/utility/enable_if.hpp>
  19. namespace boost { namespace spirit { namespace detail
  20. {
  21. template <typename T>
  22. struct as_meta_element
  23. : mpl::eval_if_c<is_abstract<T>::value || is_function<T>::value
  24. , add_reference<T>, remove_const<T> >
  25. {};
  26. template <typename T>
  27. struct as_meta_element<T&> : as_meta_element<T> // always store by value
  28. {};
  29. template <typename T, int N>
  30. struct as_meta_element<T[N]>
  31. {
  32. typedef const T(&type)[N];
  33. };
  34. namespace result_of
  35. {
  36. template <typename Car, typename Cdr = fusion::nil_>
  37. struct make_cons
  38. {
  39. typedef typename as_meta_element<Car>::type car_type; typedef typename fusion::cons<car_type, Cdr> type;
  40. };
  41. }
  42. template <typename Car, typename Cdr>
  43. fusion::cons<typename as_meta_element<Car>::type, Cdr>
  44. make_cons(Car const& car, Cdr const& cdr)
  45. {
  46. typedef typename as_meta_element<Car>::type car_type;
  47. typedef typename fusion::cons<car_type, Cdr> result;
  48. return result(car, cdr);
  49. }
  50. template <typename Car>
  51. fusion::cons<typename as_meta_element<Car>::type>
  52. make_cons(Car const& car)
  53. {
  54. typedef typename as_meta_element<Car>::type car_type;
  55. typedef typename fusion::cons<car_type> result;
  56. return result(car);
  57. }
  58. #if defined(__GNUC__) && (__GNUC__ == 4) && (__GNUC_MINOR__ == 0)
  59. // workaround for gcc-4.0 bug where illegal function types
  60. // can be formed (const is added to function type)
  61. // description: http://lists.boost.org/Archives/boost/2009/04/150743.php
  62. template <typename Car>
  63. fusion::cons<typename as_meta_element<Car>::type>
  64. make_cons(Car& car, typename enable_if<is_function<Car> >::type* = 0)
  65. {
  66. typedef typename as_meta_element<Car>::type car_type;
  67. typedef typename fusion::cons<car_type> result;
  68. return result(car);
  69. }
  70. #endif
  71. }}}
  72. #endif