1 /*=============================================================================
2 Copyright (c) 2001-2014 Joel de Guzman
3 Copyright (c) 2001-2011 Hartmut Kaiser
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(BOOST_SPIRIT_X3_PRINT_ATTRIBUTE_JANUARY_20_2013_0814AM)
9 #define BOOST_SPIRIT_X3_PRINT_ATTRIBUTE_JANUARY_20_2013_0814AM
11 #include <boost/variant.hpp>
12 #include <boost/optional/optional.hpp>
13 #include <boost/fusion/include/is_sequence.hpp>
14 #include <boost/fusion/include/for_each.hpp>
15 #include <boost/spirit/home/x3/support/traits/attribute_category.hpp>
16 #include <boost/spirit/home/x3/support/traits/is_variant.hpp>
18 namespace boost { namespace spirit { namespace x3 { namespace traits
20 template <typename Out, typename T>
21 void print_attribute(Out& out, T const& val);
23 template <typename Out>
24 inline void print_attribute(Out&, unused_type) {}
26 ///////////////////////////////////////////////////////////////////////////
29 template <typename Out>
30 struct print_fusion_sequence
32 print_fusion_sequence(Out& out)
33 : out(out), is_first(true) {}
35 typedef void result_type;
38 void operator()(T const& val) const
44 x3::traits::print_attribute(out, val);
48 mutable bool is_first;
51 // print elements in a variant
52 template <typename Out>
53 struct print_visitor : static_visitor<>
55 print_visitor(Out& out) : out(out) {}
58 void operator()(T const& val) const
60 x3::traits::print_attribute(out, val);
67 template <typename Out, typename T, typename Enable = void>
68 struct print_attribute_debug
70 // for plain data types
71 template <typename T_>
72 static void call(Out& out, T_ const& val, unused_attribute)
77 // for plain data types
78 template <typename T_>
79 static void call(Out& out, T_ const& val, plain_attribute)
84 // for fusion data types
85 template <typename T_>
86 static void call(Out& out, T_ const& val, tuple_attribute)
89 fusion::for_each(val, detail::print_fusion_sequence<Out>(out));
94 template <typename T_>
95 static void call(Out& out, T_ const& val, container_attribute)
98 if (!traits::is_empty(val))
101 typename container_iterator<T_ const>::type iend = traits::end(val);
102 for (typename container_iterator<T_ const>::type i = traits::begin(val);
103 !traits::compare(i, iend); traits::next(i))
108 x3::traits::print_attribute(out, traits::deref(i));
115 template <typename T_>
116 static void call(Out& out, T_ const& val, variant_attribute)
118 apply_visitor(detail::print_visitor<Out>(out), val);
121 // for optional types
122 template <typename T_>
123 static void call(Out& out, T_ const& val, optional_attribute)
126 x3::traits::print_attribute(out, *val);
132 static void call(Out& out, T const& val)
134 call(out, val, typename attribute_category<T>::type());
138 ///////////////////////////////////////////////////////////////////////////
139 template <typename Out, typename T>
140 inline void print_attribute(Out& out, T const& val)
142 print_attribute_debug<Out, T>::call(out, val);