]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/example/karma/auto_facilities.cpp
1 // Copyright (c) 2001-2010 Hartmut Kaiser
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 // The main purpose of this example is to show the uniform and easy way of
7 // output formatting for different container types.
9 // The 'auto_' primitive used below is very similar to the 'stream' primitive
10 // demonstrated in the example 'basic_facilities.cpp' as it allows to generate
11 // output from a multitude of data types. The main difference is that it is
12 // mapped to the correct Karma generator instead of using any available
13 // operator<<() for the contained data type. Additionally this means, that
14 // the format descriptions used below will be usable for any contained type as
15 // long as this type has a defined mapping to a Karma generator.
17 // use a larger value for the alignment field width (default is 10)
18 #define BOOST_KARMA_DEFAULT_FIELD_LENGTH 25
20 #include <boost/config/warning_disable.hpp>
30 #include <boost/array.hpp>
31 #include <boost/fusion/include/std_pair.hpp>
32 #include <boost/fusion/include/array.hpp>
33 #include <boost/range/iterator_range.hpp>
35 #include <boost/spirit/include/karma.hpp>
37 using namespace boost::spirit
;
38 using namespace boost::spirit::ascii
;
40 ///////////////////////////////////////////////////////////////////////////////
41 namespace boost
{ namespace spirit
{ namespace traits
43 // We add a specialization for the create_generator customization point
44 // defining a custom output format for the value type of the std::map used
45 // below (std::pair<int const, std::string>). Generally, any specialization
46 // for create_generator is expected to return the proto expression to be
47 // used to generate output for the type the customization point has been
50 // We need to utilize proto::deep_copy as the expression contains a literal
51 // (the ':') which normally gets embedded in the proto expression by
52 // reference only. The deep copy converts the proto tree to hold this by
53 // value. The deep copy operation can be left out for simpler proto
54 // expressions (not containing references to temporaries). Alternatively
55 // you could use the proto::make_expr() facility to build the required
58 struct create_generator
<std::pair
<int const, std::string
> >
60 typedef proto::result_of::deep_copy
<
61 BOOST_TYPEOF(int_
<< ':' << string
)
66 return proto::deep_copy(int_
<< ':' << string
);
71 ///////////////////////////////////////////////////////////////////////////////
72 // Output the given containers in list format
73 // Note: the format description does not depend on the type of the sequence
74 // nor does it depend on the type of the elements contained in the
76 ///////////////////////////////////////////////////////////////////////////////
77 template <typename Container
>
78 void output_container(std::ostream
& os
, Container
const& c
)
80 // output the container as a sequence without separators
83 auto_
, // format description
85 ) << std::endl
<< std::endl
;
89 *auto_
, // format description
91 ) << std::endl
<< std::endl
;
93 // output the container as a space separated sequence
95 karma::format_delimited(
96 auto_
, // format description
99 ) << std::endl
<< std::endl
;
102 karma::format_delimited(
103 *auto_
, // format description
106 ) << std::endl
<< std::endl
;
109 karma::format_delimited(
110 '[' << *auto_
<< ']', // format description
113 ) << std::endl
<< std::endl
;
115 // output the container as a comma separated list
118 auto_
% ", ", // format description
120 ) << std::endl
<< std::endl
;
124 '[' << (auto_
% ", ") << ']', // format description
126 ) << std::endl
<< std::endl
;
130 '[' << -(auto_
% ", ") << ']', // format description
132 ) << std::endl
<< std::endl
;
136 '[' << (+auto_
| "empty") << ']', // format description
138 ) << std::endl
<< std::endl
;
140 // output the container as a comma separated list of items enclosed in '()'
143 ('(' << auto_
<< ')') % ", ", // format description
145 ) << std::endl
<< std::endl
;
150 ('(' << auto_
<< ')') % ", "
151 ) << ']', // format description
153 ) << std::endl
<< std::endl
;
155 // output the container as a HTML list
157 karma::format_delimited(
159 *verbatim
["<li>" << auto_
<< "</li>"]
160 << "</ol>", // format description
165 // output the container as right aligned column
167 karma::format_delimited(
169 "|" << right_align
[auto_
] << "|"
170 ], // format description
180 ///////////////////////////////////////////////////////////////////////////
182 int i
[4] = { 3, 6, 9, 12 };
184 std::cout
<< "-------------------------------------------------------------"
186 std::cout
<< "int i[]" << std::endl
;
187 output_container(std::cout
, boost::make_iterator_range(i
, i
+4));
189 ///////////////////////////////////////////////////////////////////////////
191 std::vector
<int> v (5);
192 std::generate(v
.begin(), v
.end(), std::rand
); // randomly fill the vector
194 std::cout
<< "-------------------------------------------------------------"
196 std::cout
<< "std::vector<int>" << std::endl
;
197 output_container(std::cout
, v
);
199 ///////////////////////////////////////////////////////////////////////////
206 std::cout
<< "-------------------------------------------------------------"
208 std::cout
<< "std::list<char>" << std::endl
;
209 output_container(std::cout
, l
);
211 ///////////////////////////////////////////////////////////////////////////
213 std::string
str("Hello world!");
215 std::cout
<< "-------------------------------------------------------------"
217 std::cout
<< "std::string" << std::endl
;
218 output_container(std::cout
, str
);
220 ///////////////////////////////////////////////////////////////////////////
222 boost::array
<long, 5> arr
;
223 std::generate(arr
.begin(), arr
.end(), std::rand
); // randomly fill the array
225 std::cout
<< "-------------------------------------------------------------"
227 std::cout
<< "boost::array<long, 5>" << std::endl
;
228 output_container(std::cout
, arr
);
230 ///////////////////////////////////////////////////////////////////////////
231 // map of int --> string mappings
232 std::map
<int, std::string
> mappings
;
233 mappings
.insert(std::make_pair(0, "zero"));
234 mappings
.insert(std::make_pair(1, "one"));
235 mappings
.insert(std::make_pair(2, "two"));
237 std::cout
<< "-------------------------------------------------------------"
239 std::cout
<< "std::map<int, std::string>" << std::endl
;
240 output_container(std::cout
, mappings
);