cons.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  3. Copyright (c) 2005 Eric Niebler
  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. #if !defined(FUSION_CONS_07172005_0843)
  8. #define FUSION_CONS_07172005_0843
  9. #include <boost/fusion/container/list/cons_fwd.hpp>
  10. #include <boost/fusion/support/detail/access.hpp>
  11. #include <boost/fusion/sequence/intrinsic/begin.hpp>
  12. #include <boost/fusion/sequence/intrinsic/end.hpp>
  13. #include <boost/fusion/iterator/next.hpp>
  14. #include <boost/fusion/iterator/deref.hpp>
  15. #include <boost/fusion/container/list/cons_iterator.hpp>
  16. #include <boost/fusion/container/list/detail/begin_impl.hpp>
  17. #include <boost/fusion/container/list/detail/end_impl.hpp>
  18. #include <boost/fusion/container/list/detail/at_impl.hpp>
  19. #include <boost/fusion/container/list/detail/value_at_impl.hpp>
  20. #include <boost/fusion/container/list/detail/empty_impl.hpp>
  21. #include <boost/type_traits/is_convertible.hpp>
  22. #include <boost/utility/enable_if.hpp>
  23. #include <boost/fusion/support/sequence_base.hpp>
  24. #include <boost/mpl/int.hpp>
  25. #include <boost/mpl/bool.hpp>
  26. #include <boost/mpl/or.hpp>
  27. namespace boost { namespace fusion
  28. {
  29. struct void_;
  30. struct cons_tag;
  31. struct forward_traversal_tag;
  32. struct fusion_sequence_tag;
  33. struct nil_ : sequence_base<nil_>
  34. {
  35. typedef mpl::int_<0> size;
  36. typedef cons_tag fusion_tag;
  37. typedef fusion_sequence_tag tag; // this gets picked up by MPL
  38. typedef mpl::false_ is_view;
  39. typedef forward_traversal_tag category;
  40. typedef void_ car_type;
  41. typedef void_ cdr_type;
  42. nil_() {}
  43. template <typename Iterator>
  44. nil_(Iterator const& /*iter*/, mpl::true_ /*this_is_an_iterator*/)
  45. {}
  46. template <typename Iterator>
  47. void assign_from_iter(Iterator const& /*iter*/)
  48. {
  49. }
  50. };
  51. template <typename Car, typename Cdr /*= nil_*/>
  52. struct cons : sequence_base<cons<Car, Cdr> >
  53. {
  54. typedef mpl::int_<Cdr::size::value+1> size;
  55. typedef cons_tag fusion_tag;
  56. typedef fusion_sequence_tag tag; // this gets picked up by MPL
  57. typedef mpl::false_ is_view;
  58. typedef forward_traversal_tag category;
  59. typedef Car car_type;
  60. typedef Cdr cdr_type;
  61. cons()
  62. : car(), cdr() {}
  63. explicit cons(typename detail::call_param<Car>::type in_car)
  64. : car(in_car), cdr() {}
  65. cons(
  66. typename detail::call_param<Car>::type in_car
  67. , typename detail::call_param<Cdr>::type in_cdr)
  68. : car(in_car), cdr(in_cdr) {}
  69. template <typename Car2, typename Cdr2>
  70. cons(cons<Car2, Cdr2> const& rhs)
  71. : car(rhs.car), cdr(rhs.cdr) {}
  72. cons(cons const& rhs)
  73. : car(rhs.car), cdr(rhs.cdr) {}
  74. template <typename Sequence>
  75. cons(
  76. Sequence const& seq
  77. , typename boost::disable_if<
  78. mpl::or_<
  79. is_convertible<Sequence, cons> // use copy ctor instead
  80. , is_convertible<Sequence, Car> // use copy to car instead
  81. >
  82. >::type* /*dummy*/ = 0
  83. )
  84. : car(*fusion::begin(seq))
  85. , cdr(fusion::next(fusion::begin(seq)), mpl::true_()) {}
  86. template <typename Iterator>
  87. cons(Iterator const& iter, mpl::true_ /*this_is_an_iterator*/)
  88. : car(*iter)
  89. , cdr(fusion::next(iter), mpl::true_()) {}
  90. template <typename Car2, typename Cdr2>
  91. cons& operator=(cons<Car2, Cdr2> const& rhs)
  92. {
  93. car = rhs.car;
  94. cdr = rhs.cdr;
  95. return *this;
  96. }
  97. cons& operator=(cons const& rhs)
  98. {
  99. car = rhs.car;
  100. cdr = rhs.cdr;
  101. return *this;
  102. }
  103. template <typename Sequence>
  104. typename boost::disable_if<is_convertible<Sequence, Car>, cons&>::type
  105. operator=(Sequence const& seq)
  106. {
  107. typedef typename result_of::begin<Sequence const>::type Iterator;
  108. Iterator iter = fusion::begin(seq);
  109. this->assign_from_iter(iter);
  110. return *this;
  111. }
  112. template <typename Iterator>
  113. void assign_from_iter(Iterator const& iter)
  114. {
  115. car = *iter;
  116. cdr.assign_from_iter(fusion::next(iter));
  117. }
  118. car_type car;
  119. cdr_type cdr;
  120. };
  121. }}
  122. #endif