fix doc example typo
[boost.git] / boost / proto / deep_copy.hpp
blob4f36917b048858e27322d81ff3b50d2ff3e5a524
1 #ifndef BOOST_PP_IS_ITERATING
2 ///////////////////////////////////////////////////////////////////////////////
3 /// \file deep_copy.hpp
4 /// Replace all nodes stored by reference by nodes stored by value.
5 //
6 // Copyright 2008 Eric Niebler. Distributed under the Boost
7 // Software License, Version 1.0. (See accompanying file
8 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
10 #ifndef BOOST_PROTO_DEEP_COPY_HPP_EAN_11_21_2006
11 #define BOOST_PROTO_DEEP_COPY_HPP_EAN_11_21_2006
13 #include <boost/proto/detail/prefix.hpp>
14 #include <boost/preprocessor/cat.hpp>
15 #include <boost/preprocessor/repetition/enum.hpp>
16 #include <boost/preprocessor/iteration/iterate.hpp>
17 #include <boost/mpl/if.hpp>
18 #include <boost/type_traits/is_function.hpp>
19 #include <boost/type_traits/remove_reference.hpp>
20 #include <boost/proto/proto_fwd.hpp>
21 #include <boost/proto/expr.hpp>
22 #include <boost/proto/detail/suffix.hpp>
24 namespace boost { namespace proto
26 namespace detail
28 template<typename Expr, long Arity = Expr::proto_arity_c>
29 struct deep_copy_impl;
31 template<typename Expr>
32 struct deep_copy_impl<Expr, 0>
34 typedef BOOST_PROTO_UNCVREF(typename Expr::proto_child0) raw_terminal_type;
35 // can't store a function type in a terminal.
36 typedef
37 typename mpl::if_c<
38 is_function<raw_terminal_type>::value
39 , typename Expr::proto_child0
40 , raw_terminal_type
41 >::type
42 actual_terminal_type;
43 typedef typename terminal<actual_terminal_type>::type expr_type;
44 typedef typename Expr::proto_domain::template result<void(expr_type)>::type type;
46 template<typename Expr2>
47 static type call(Expr2 const &expr)
49 return typename Expr::proto_domain()(expr_type::make(expr.proto_base().child0));
54 namespace result_of
56 /// \brief A metafunction for calculating the return type
57 /// of \c proto::deep_copy().
58 ///
59 /// A metafunction for calculating the return type
60 /// of \c proto::deep_copy(). The type parameter \c Expr
61 /// should be the type of a Proto expression tree.
62 /// It should not be a reference type, nor should it
63 /// be cv-qualified.
64 template<typename Expr>
65 struct deep_copy
67 typedef
68 typename detail::deep_copy_impl<
69 BOOST_PROTO_UNCVREF(Expr)
70 >::type
71 type;
75 namespace functional
77 /// \brief A PolymorphicFunctionObject type for deep-copying
78 /// Proto expression trees.
79 ///
80 /// A PolymorphicFunctionObject type for deep-copying
81 /// Proto expression trees. When a tree is deep-copied,
82 /// all internal nodes and most terminals held by reference
83 /// are instead held by value.
84 ///
85 /// \attention Terminals of reference-to-function type are
86 /// left unchanged. Terminals of reference-to-array type are
87 /// stored by value, which can cause a large amount of data
88 /// to be passed by value and stored on the stack.
89 struct deep_copy
91 BOOST_PROTO_CALLABLE()
93 template<typename Sig>
94 struct result;
96 template<typename This, typename Expr>
97 struct result<This(Expr)>
99 typedef
100 typename result_of::deep_copy<Expr>::type
101 type;
104 /// \brief Deep-copies a Proto expression tree, turning all
105 /// nodes and terminals held by reference into ones held by
106 /// value.
107 template<typename Expr>
108 typename result_of::deep_copy<Expr>::type
109 operator()(Expr const &expr) const
111 return proto::detail::deep_copy_impl<Expr>::call(expr);
116 /// \brief A function for deep-copying
117 /// Proto expression trees.
119 /// A function for deep-copying
120 /// Proto expression trees. When a tree is deep-copied,
121 /// all internal nodes and most terminals held by reference
122 /// are instead held by value.
124 /// \attention Terminals of reference-to-array type and of
125 /// reference-to-function type are left unchanged.
127 /// \sa proto::functional::deep_copy.
128 template<typename Expr>
129 typename proto::result_of::deep_copy<Expr>::type
130 deep_copy(Expr const &expr)
132 return proto::detail::deep_copy_impl<Expr>::call(expr);
135 namespace detail
137 #define BOOST_PROTO_DEFINE_DEEP_COPY_TYPE(Z, N, DATA) \
138 typename deep_copy_impl< \
139 typename remove_reference< \
140 typename Expr::BOOST_PP_CAT(proto_child, N) \
141 >::type::proto_derived_expr \
142 >::type \
143 /**/
145 #define BOOST_PROTO_DEFINE_DEEP_COPY_FUN(Z, N, DATA) \
146 proto::deep_copy(expr.proto_base().BOOST_PP_CAT(child, N)) \
147 /**/
149 #define BOOST_PP_ITERATION_PARAMS_1 (3, (1, BOOST_PROTO_MAX_ARITY, <boost/proto/deep_copy.hpp>))
150 #include BOOST_PP_ITERATE()
152 #undef BOOST_PROTO_DEFINE_DEEP_COPY_FUN
153 #undef BOOST_PROTO_DEFINE_DEEP_COPY_TYPE
158 #endif // BOOST_PROTO_COMPILER_DEEP_COPY_HPP_EAN_11_21_2006
160 #else
162 #define N BOOST_PP_ITERATION()
164 template<typename Expr>
165 struct deep_copy_impl<Expr, N>
167 typedef
168 proto::expr<
169 typename Expr::proto_tag
170 , BOOST_PP_CAT(list, N)<
171 BOOST_PP_ENUM(N, BOOST_PROTO_DEFINE_DEEP_COPY_TYPE, ~)
174 expr_type;
176 typedef typename Expr::proto_domain::template result<void(expr_type)>::type type;
178 template<typename Expr2>
179 static type call(Expr2 const &expr)
181 expr_type that = {
182 BOOST_PP_ENUM(N, BOOST_PROTO_DEFINE_DEEP_COPY_FUN, ~)
185 return typename Expr::proto_domain()(that);
189 #undef N
191 #endif