]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/proto/debug.hpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / boost / proto / debug.hpp
1 ///////////////////////////////////////////////////////////////////////////////
2 /// \file debug.hpp
3 /// Utilities for debugging Proto expression trees
4 //
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)
8
9 #ifndef BOOST_PROTO_DEBUG_HPP_EAN_12_31_2006
10 #define BOOST_PROTO_DEBUG_HPP_EAN_12_31_2006
11
12 #include <iostream>
13 #include <boost/preprocessor/stringize.hpp>
14 #include <boost/core/ref.hpp>
15 #include <boost/core/typeinfo.hpp>
16 #include <boost/mpl/assert.hpp>
17 #include <boost/proto/proto_fwd.hpp>
18 #include <boost/proto/traits.hpp>
19 #include <boost/proto/matches.hpp>
20 #include <boost/proto/fusion.hpp>
21 #include <boost/fusion/algorithm/iteration/for_each.hpp>
22
23 namespace boost { namespace proto
24 {
25 namespace tagns_ { namespace tag
26 {
27 #define BOOST_PROTO_DEFINE_TAG_INSERTION(Tag) \
28 /** \brief INTERNAL ONLY */ \
29 inline std::ostream &operator <<(std::ostream &sout, Tag const &) \
30 { \
31 return sout << BOOST_PP_STRINGIZE(Tag); \
32 } \
33 /**/
34
35 BOOST_PROTO_DEFINE_TAG_INSERTION(terminal)
36 BOOST_PROTO_DEFINE_TAG_INSERTION(unary_plus)
37 BOOST_PROTO_DEFINE_TAG_INSERTION(negate)
38 BOOST_PROTO_DEFINE_TAG_INSERTION(dereference)
39 BOOST_PROTO_DEFINE_TAG_INSERTION(complement)
40 BOOST_PROTO_DEFINE_TAG_INSERTION(address_of)
41 BOOST_PROTO_DEFINE_TAG_INSERTION(logical_not)
42 BOOST_PROTO_DEFINE_TAG_INSERTION(pre_inc)
43 BOOST_PROTO_DEFINE_TAG_INSERTION(pre_dec)
44 BOOST_PROTO_DEFINE_TAG_INSERTION(post_inc)
45 BOOST_PROTO_DEFINE_TAG_INSERTION(post_dec)
46 BOOST_PROTO_DEFINE_TAG_INSERTION(shift_left)
47 BOOST_PROTO_DEFINE_TAG_INSERTION(shift_right)
48 BOOST_PROTO_DEFINE_TAG_INSERTION(multiplies)
49 BOOST_PROTO_DEFINE_TAG_INSERTION(divides)
50 BOOST_PROTO_DEFINE_TAG_INSERTION(modulus)
51 BOOST_PROTO_DEFINE_TAG_INSERTION(plus)
52 BOOST_PROTO_DEFINE_TAG_INSERTION(minus)
53 BOOST_PROTO_DEFINE_TAG_INSERTION(less)
54 BOOST_PROTO_DEFINE_TAG_INSERTION(greater)
55 BOOST_PROTO_DEFINE_TAG_INSERTION(less_equal)
56 BOOST_PROTO_DEFINE_TAG_INSERTION(greater_equal)
57 BOOST_PROTO_DEFINE_TAG_INSERTION(equal_to)
58 BOOST_PROTO_DEFINE_TAG_INSERTION(not_equal_to)
59 BOOST_PROTO_DEFINE_TAG_INSERTION(logical_or)
60 BOOST_PROTO_DEFINE_TAG_INSERTION(logical_and)
61 BOOST_PROTO_DEFINE_TAG_INSERTION(bitwise_and)
62 BOOST_PROTO_DEFINE_TAG_INSERTION(bitwise_or)
63 BOOST_PROTO_DEFINE_TAG_INSERTION(bitwise_xor)
64 BOOST_PROTO_DEFINE_TAG_INSERTION(comma)
65 BOOST_PROTO_DEFINE_TAG_INSERTION(mem_ptr)
66 BOOST_PROTO_DEFINE_TAG_INSERTION(assign)
67 BOOST_PROTO_DEFINE_TAG_INSERTION(shift_left_assign)
68 BOOST_PROTO_DEFINE_TAG_INSERTION(shift_right_assign)
69 BOOST_PROTO_DEFINE_TAG_INSERTION(multiplies_assign)
70 BOOST_PROTO_DEFINE_TAG_INSERTION(divides_assign)
71 BOOST_PROTO_DEFINE_TAG_INSERTION(modulus_assign)
72 BOOST_PROTO_DEFINE_TAG_INSERTION(plus_assign)
73 BOOST_PROTO_DEFINE_TAG_INSERTION(minus_assign)
74 BOOST_PROTO_DEFINE_TAG_INSERTION(bitwise_and_assign)
75 BOOST_PROTO_DEFINE_TAG_INSERTION(bitwise_or_assign)
76 BOOST_PROTO_DEFINE_TAG_INSERTION(bitwise_xor_assign)
77 BOOST_PROTO_DEFINE_TAG_INSERTION(subscript)
78 BOOST_PROTO_DEFINE_TAG_INSERTION(member)
79 BOOST_PROTO_DEFINE_TAG_INSERTION(if_else_)
80 BOOST_PROTO_DEFINE_TAG_INSERTION(function)
81
82 #undef BOOST_PROTO_DEFINE_TAG_INSERTION
83 }}
84
85 namespace hidden_detail_
86 {
87 struct ostream_wrapper
88 {
89 ostream_wrapper(std::ostream &sout)
90 : sout_(sout)
91 {}
92
93 std::ostream &sout_;
94
95 private:
96 ostream_wrapper &operator =(ostream_wrapper const &);
97 };
98
99 struct named_any
100 {
101 template<typename T>
102 named_any(T const &)
103 : name_(BOOST_CORE_TYPEID(T).name())
104 {}
105
106 char const *name_;
107 };
108
109 inline std::ostream &operator <<(ostream_wrapper sout_wrap, named_any t)
110 {
111 return sout_wrap.sout_ << t.name_;
112 }
113 }
114
115 namespace detail
116 {
117 // copyable functor to pass by value to fusion::foreach
118 struct display_expr_impl;
119 struct display_expr_impl_functor
120 {
121 display_expr_impl_functor(display_expr_impl const& impl): impl_(impl)
122 {}
123
124 template<typename Expr>
125 void operator()(Expr const &expr) const
126 {
127 this->impl_(expr);
128 }
129
130 private:
131 display_expr_impl const& impl_;
132 };
133
134 struct display_expr_impl
135 {
136 explicit display_expr_impl(std::ostream &sout, int depth = 0)
137 : depth_(depth)
138 , first_(true)
139 , sout_(sout)
140 {}
141
142 template<typename Expr>
143 void operator()(Expr const &expr) const
144 {
145 this->impl(expr, mpl::long_<arity_of<Expr>::value>());
146 }
147
148 private:
149 display_expr_impl(display_expr_impl const &);
150 display_expr_impl &operator =(display_expr_impl const &);
151
152 template<typename Expr>
153 void impl(Expr const &expr, mpl::long_<0>) const
154 {
155 using namespace hidden_detail_;
156 typedef typename tag_of<Expr>::type tag;
157 this->sout_.width(this->depth_);
158 this->sout_ << (this->first_? "" : ", ");
159 this->sout_ << tag() << "(" << proto::value(expr) << ")\n";
160 this->first_ = false;
161 }
162
163 template<typename Expr, typename Arity>
164 void impl(Expr const &expr, Arity) const
165 {
166 using namespace hidden_detail_;
167 typedef typename tag_of<Expr>::type tag;
168 this->sout_.width(this->depth_);
169 this->sout_ << (this->first_? "" : ", ");
170 this->sout_ << tag() << "(\n";
171 display_expr_impl display(this->sout_, this->depth_ + 4);
172 fusion::for_each(expr, display_expr_impl_functor(display));
173 this->sout_.width(this->depth_);
174 this->sout_ << "" << ")\n";
175 this->first_ = false;
176 }
177
178 int depth_;
179 mutable bool first_;
180 std::ostream &sout_;
181 };
182 }
183
184 namespace functional
185 {
186 /// \brief Pretty-print a Proto expression tree.
187 ///
188 /// A PolymorphicFunctionObject which accepts a Proto expression
189 /// tree and pretty-prints it to an \c ostream for debugging
190 /// purposes.
191 struct display_expr
192 {
193 BOOST_PROTO_CALLABLE()
194
195 typedef void result_type;
196
197 /// \param sout The \c ostream to which the expression tree
198 /// will be written.
199 /// \param depth The starting indentation depth for this node.
200 /// Children nodes will be displayed at a starting
201 /// depth of <tt>depth+4</tt>.
202 explicit display_expr(std::ostream &sout = std::cout, int depth = 0)
203 : depth_(depth)
204 , sout_(sout)
205 {}
206
207 /// \brief Pretty-print the current node in a Proto expression
208 /// tree.
209 template<typename Expr>
210 void operator()(Expr const &expr) const
211 {
212 detail::display_expr_impl(this->sout_, this->depth_)(expr);
213 }
214
215 private:
216 int depth_;
217 reference_wrapper<std::ostream> sout_;
218 };
219 }
220
221 /// \brief Pretty-print a Proto expression tree.
222 ///
223 /// \note Equivalent to <tt>functional::display_expr(0, sout)(expr)</tt>
224 /// \param expr The Proto expression tree to pretty-print
225 /// \param sout The \c ostream to which the output should be
226 /// written. If not specified, defaults to
227 /// <tt>std::cout</tt>.
228 template<typename Expr>
229 void display_expr(Expr const &expr, std::ostream &sout)
230 {
231 functional::display_expr(sout, 0)(expr);
232 }
233
234 /// \overload
235 ///
236 template<typename Expr>
237 void display_expr(Expr const &expr)
238 {
239 functional::display_expr()(expr);
240 }
241
242 /// \brief Assert at compile time that a particular expression
243 /// matches the specified grammar.
244 ///
245 /// \note Equivalent to <tt>BOOST_MPL_ASSERT((proto::matches\<Expr, Grammar\>))</tt>
246 /// \param expr The Proto expression to check againts <tt>Grammar</tt>
247 template<typename Grammar, typename Expr>
248 void assert_matches(Expr const & /*expr*/)
249 {
250 BOOST_MPL_ASSERT((proto::matches<Expr, Grammar>));
251 }
252
253 /// \brief Assert at compile time that a particular expression
254 /// does not match the specified grammar.
255 ///
256 /// \note Equivalent to <tt>BOOST_MPL_ASSERT_NOT((proto::matches\<Expr, Grammar\>))</tt>
257 /// \param expr The Proto expression to check againts <tt>Grammar</tt>
258 template<typename Grammar, typename Expr>
259 void assert_matches_not(Expr const & /*expr*/)
260 {
261 BOOST_MPL_ASSERT_NOT((proto::matches<Expr, Grammar>));
262 }
263
264 /// \brief Assert at compile time that a particular expression
265 /// matches the specified grammar.
266 ///
267 /// \note Equivalent to <tt>proto::assert_matches\<Grammar\>(Expr)</tt>
268 /// \param Expr The Proto expression to check againts <tt>Grammar</tt>
269 /// \param Grammar The grammar used to validate Expr.
270 #define BOOST_PROTO_ASSERT_MATCHES(Expr, Grammar) \
271 (true ? (void)0 : boost::proto::assert_matches<Grammar>(Expr))
272
273 /// \brief Assert at compile time that a particular expression
274 /// does not match the specified grammar.
275 ///
276 /// \note Equivalent to <tt>proto::assert_matches_not\<Grammar\>(Expr)</tt>
277 /// \param Expr The Proto expression to check againts <tt>Grammar</tt>
278 /// \param Grammar The grammar used to validate Expr.
279 #define BOOST_PROTO_ASSERT_MATCHES_NOT(Expr, Grammar) \
280 (true ? (void)0 : boost::proto::assert_matches_not<Grammar>(Expr))
281
282 }}
283
284 #endif