1 ///////////////////////////////////////////////////////////////////////////////
2 /// \file fold_tree.hpp
3 /// Contains definition of the fold_tree<> and reverse_fold_tree<> transforms.
5 // Copyright 2008 Eric Niebler. Distributed under the Boost
6 // Software License, Version 1.0. (See accompanying file
7 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 #ifndef BOOST_PROTO_TRANSFORM_FOLD_TREE_HPP_EAN_11_05_2007
10 #define BOOST_PROTO_TRANSFORM_FOLD_TREE_HPP_EAN_11_05_2007
12 #include <boost/type_traits/is_same.hpp>
13 #include <boost/proto/proto_fwd.hpp>
14 #include <boost/proto/traits.hpp>
15 #include <boost/proto/matches.hpp>
16 #include <boost/proto/transform/fold.hpp>
17 #include <boost/proto/transform/impl.hpp>
19 namespace boost { namespace proto
23 template<typename Tag>
26 template<typename Expr, typename State, typename Data, typename EnableIf = Tag>
29 typedef mpl::false_ result_type;
32 template<typename Expr, typename State, typename Data>
33 struct impl<Expr, State, Data, typename Expr::proto_tag>
35 typedef mpl::true_ result_type;
38 template<typename Expr, typename State, typename Data>
39 struct impl<Expr &, State, Data, typename Expr::proto_tag>
41 typedef mpl::true_ result_type;
45 template<typename Tag, typename Fun>
47 : if_<has_tag<Tag>, fold<_, _state, fold_tree_<Tag, Fun> >, Fun>
50 template<typename Tag, typename Fun>
51 struct reverse_fold_tree_
52 : if_<has_tag<Tag>, reverse_fold<_, _state, reverse_fold_tree_<Tag, Fun> >, Fun>
56 /// \brief A PrimitiveTransform that recursively applies the
57 /// <tt>fold\<\></tt> transform to sub-trees that all share a common
60 /// <tt>fold_tree\<\></tt> is useful for flattening trees into lists;
61 /// for example, you might use <tt>fold_tree\<\></tt> to flatten an
62 /// expression tree like <tt>a | b | c</tt> into a Fusion list like
63 /// <tt>cons(c, cons(b, cons(a)))</tt>.
65 /// <tt>fold_tree\<\></tt> is easily understood in terms of a
66 /// <tt>recurse_if_\<\></tt> helper, defined as follows:
69 /// template<typename Tag, typename Fun>
70 /// struct recurse_if_
72 /// // If the current node has type "Tag" ...
73 /// is_same<tag_of<_>, Tag>()
74 /// // ... recurse, otherwise ...
75 /// , fold<_, _state, recurse_if_<Tag, Fun> >
76 /// // ... apply the Fun transform.
82 /// With <tt>recurse_if_\<\></tt> as defined above,
83 /// <tt>fold_tree\<Sequence, State0, Fun\>()(e, s, d)</tt> is
85 /// <tt>fold<Sequence, State0, recurse_if_<Expr::proto_tag, Fun> >()(e, s, d).</tt>
86 /// It has the effect of folding a tree front-to-back, recursing into
87 /// child nodes that share a tag type with the parent node.
88 template<typename Sequence, typename State0, typename Fun>
90 : transform<fold_tree<Sequence, State0, Fun> >
92 template<typename Expr, typename State, typename Data>
97 , detail::fold_tree_<typename Expr::proto_tag, Fun>
98 >::template impl<Expr, State, Data>
101 template<typename Expr, typename State, typename Data>
102 struct impl<Expr &, State, Data>
106 , detail::fold_tree_<typename Expr::proto_tag, Fun>
107 >::template impl<Expr &, State, Data>
111 /// \brief A PrimitiveTransform that recursively applies the
112 /// <tt>reverse_fold\<\></tt> transform to sub-trees that all share
113 /// a common tag type.
115 /// <tt>reverse_fold_tree\<\></tt> is useful for flattening trees into
116 /// lists; for example, you might use <tt>reverse_fold_tree\<\></tt> to
117 /// flatten an expression tree like <tt>a | b | c</tt> into a Fusion list
118 /// like <tt>cons(a, cons(b, cons(c)))</tt>.
120 /// <tt>reverse_fold_tree\<\></tt> is easily understood in terms of a
121 /// <tt>recurse_if_\<\></tt> helper, defined as follows:
124 /// template<typename Tag, typename Fun>
125 /// struct recurse_if_
127 /// // If the current node has type "Tag" ...
128 /// is_same<tag_of<_>, Tag>()
129 /// // ... recurse, otherwise ...
130 /// , reverse_fold<_, _state, recurse_if_<Tag, Fun> >
131 /// // ... apply the Fun transform.
137 /// With <tt>recurse_if_\<\></tt> as defined above,
138 /// <tt>reverse_fold_tree\<Sequence, State0, Fun\>()(e, s, d)</tt> is
140 /// <tt>reverse_fold<Sequence, State0, recurse_if_<Expr::proto_tag, Fun> >()(e, s, d).</tt>
141 /// It has the effect of folding a tree back-to-front, recursing into
142 /// child nodes that share a tag type with the parent node.
143 template<typename Sequence, typename State0, typename Fun>
144 struct reverse_fold_tree
145 : transform<reverse_fold_tree<Sequence, State0, Fun> >
147 template<typename Expr, typename State, typename Data>
152 , detail::reverse_fold_tree_<typename Expr::proto_tag, Fun>
153 >::template impl<Expr, State, Data>
156 template<typename Expr, typename State, typename Data>
157 struct impl<Expr &, State, Data>
161 , detail::reverse_fold_tree_<typename Expr::proto_tag, Fun>
162 >::template impl<Expr &, State, Data>
168 template<typename Sequence, typename State0, typename Fun>
169 struct is_callable<fold_tree<Sequence, State0, Fun> >
175 template<typename Sequence, typename State0, typename Fun>
176 struct is_callable<reverse_fold_tree<Sequence, State0, Fun> >