make_cons.hpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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/spirit/include/phoenix_limits.hpp> // needs to be included before proto
  13. #include <boost/proto/proto.hpp>
  14. #include <boost/mpl/eval_if.hpp>
  15. #include <boost/fusion/include/cons.hpp>
  16. #include <boost/type_traits/remove_const.hpp>
  17. #include <boost/type_traits/is_abstract.hpp>
  18. #include <boost/type_traits/is_function.hpp>
  19. #include <boost/type_traits/add_reference.hpp>
  20. #include <boost/utility/enable_if.hpp>
  21. namespace boost { namespace spirit { namespace detail
  22. {
  23. template <typename T>
  24. struct as_meta_element
  25. : mpl::eval_if_c<is_abstract<T>::value || is_function<T>::value
  26. , add_reference<T>, remove_const<T> >
  27. {};
  28. template <typename T>
  29. struct as_meta_element<T&> : as_meta_element<T> // always store by value
  30. {};
  31. template <typename T, int N>
  32. struct as_meta_element<T[N]>
  33. {
  34. typedef const T(&type)[N];
  35. };
  36. namespace result_of
  37. {
  38. template <typename Car, typename Cdr = fusion::nil_>
  39. struct make_cons
  40. {
  41. typedef typename as_meta_element<Car>::type car_type; typedef typename fusion::cons<car_type, Cdr> type;
  42. };
  43. }
  44. template <typename Car, typename Cdr>
  45. fusion::cons<typename as_meta_element<Car>::type, Cdr>
  46. make_cons(Car const& car, Cdr const& cdr)
  47. {
  48. typedef typename as_meta_element<Car>::type car_type;
  49. typedef typename fusion::cons<car_type, Cdr> result;
  50. return result(car, cdr);
  51. }
  52. template <typename Car>
  53. fusion::cons<typename as_meta_element<Car>::type>
  54. make_cons(Car const& car)
  55. {
  56. typedef typename as_meta_element<Car>::type car_type;
  57. typedef typename fusion::cons<car_type> result;
  58. return result(car);
  59. }
  60. #if defined(__GNUC__) && (__GNUC__ == 4) && (__GNUC_MINOR__ == 0)
  61. // workaround for gcc-4.0 bug where illegal function types
  62. // can be formed (const is added to function type)
  63. // description: http://lists.boost.org/Archives/boost/2009/04/150743.php
  64. template <typename Car>
  65. fusion::cons<typename as_meta_element<Car>::type>
  66. make_cons(Car& car, typename enable_if<is_function<Car> >::type* = 0)
  67. {
  68. typedef typename as_meta_element<Car>::type car_type;
  69. typedef typename fusion::cons<car_type> result;
  70. return result(car);
  71. }
  72. #endif
  73. }}}
  74. #endif