1 /*=============================================================================
2 Copyright (c) 2001-2011 Joel de Guzman
3 Copyright (c) 2005 Eric Niebler
5 Distributed under the Boost Software License, Version 1.0. (See accompanying
6 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 ==============================================================================*/
8 #if !defined(FUSION_CONS_07172005_0843)
9 #define FUSION_CONS_07172005_0843
11 #include <boost/fusion/container/list/cons_fwd.hpp>
12 #include <boost/fusion/support/detail/access.hpp>
13 #include <boost/fusion/sequence/intrinsic/begin.hpp>
14 #include <boost/fusion/sequence/intrinsic/end.hpp>
15 #include <boost/fusion/iterator/next.hpp>
16 #include <boost/fusion/iterator/deref.hpp>
17 #include <boost/fusion/container/list/cons_iterator.hpp>
18 #include <boost/fusion/container/list/detail/begin_impl.hpp>
19 #include <boost/fusion/container/list/detail/end_impl.hpp>
20 #include <boost/fusion/container/list/detail/at_impl.hpp>
21 #include <boost/fusion/container/list/detail/value_at_impl.hpp>
22 #include <boost/fusion/container/list/detail/empty_impl.hpp>
23 #include <boost/type_traits/is_convertible.hpp>
24 #include <boost/utility/enable_if.hpp>
25 #include <boost/fusion/support/sequence_base.hpp>
26 #include <boost/mpl/int.hpp>
27 #include <boost/mpl/bool.hpp>
28 #include <boost/mpl/or.hpp>
30 namespace boost { namespace fusion
34 struct forward_traversal_tag;
35 struct fusion_sequence_tag;
37 struct nil_ : sequence_base<nil_>
39 typedef mpl::int_<0> size;
40 typedef cons_tag fusion_tag;
41 typedef fusion_sequence_tag tag; // this gets picked up by MPL
42 typedef mpl::false_ is_view;
43 typedef forward_traversal_tag category;
44 typedef void_ car_type;
45 typedef void_ cdr_type;
49 template <typename Iterator>
50 nil_(Iterator const& /*iter*/, mpl::true_ /*this_is_an_iterator*/)
53 template <typename Iterator>
54 void assign_from_iter(Iterator const& /*iter*/)
59 template <typename Car, typename Cdr /*= nil_*/>
60 struct cons : sequence_base<cons<Car, Cdr> >
62 typedef mpl::int_<Cdr::size::value+1> size;
63 typedef cons_tag fusion_tag;
64 typedef fusion_sequence_tag tag; // this gets picked up by MPL
65 typedef mpl::false_ is_view;
66 typedef forward_traversal_tag category;
73 explicit cons(typename detail::call_param<Car>::type in_car)
74 : car(in_car), cdr() {}
77 typename detail::call_param<Car>::type in_car
78 , typename detail::call_param<Cdr>::type in_cdr)
79 : car(in_car), cdr(in_cdr) {}
81 template <typename Car2, typename Cdr2>
82 cons(cons<Car2, Cdr2> const& rhs)
83 : car(rhs.car), cdr(rhs.cdr) {}
86 : car(rhs.car), cdr(rhs.cdr) {}
88 template <typename Sequence>
91 , typename boost::disable_if<
93 is_convertible<Sequence, cons> // use copy ctor instead
94 , is_convertible<Sequence, Car> // use copy to car instead
96 >::type* /*dummy*/ = 0
98 : car(*fusion::begin(seq))
99 , cdr(fusion::next(fusion::begin(seq)), mpl::true_()) {}
101 template <typename Iterator>
102 cons(Iterator const& iter, mpl::true_ /*this_is_an_iterator*/)
104 , cdr(fusion::next(iter), mpl::true_()) {}
106 template <typename Car2, typename Cdr2>
107 cons& operator=(cons<Car2, Cdr2> const& rhs)
114 cons& operator=(cons const& rhs)
121 template <typename Sequence>
122 typename boost::disable_if<is_convertible<Sequence, Car>, cons&>::type
123 operator=(Sequence const& seq)
125 typedef typename result_of::begin<Sequence const>::type Iterator;
126 Iterator iter = fusion::begin(seq);
127 this->assign_from_iter(iter);
131 template <typename Iterator>
132 void assign_from_iter(Iterator const& iter)
135 cdr.assign_from_iter(fusion::next(iter));