]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/example/karma/basic_facilities.cpp
f0c481b45ae720f1fd647710e8bbe0de5c8a6e3e
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 // Since the 'stream' primitive used below uses the streaming operator defined
10 // for the container value_type, you must make sure to have a corresponding
11 // operator<<() available for this contained data type. OTOH this means, that
12 // the format descriptions used below will be usable for any contained type as
13 // long as this type has an associated streaming operator defined.
15 // use a larger value for the alignment field width (default is 10)
16 #define BOOST_KARMA_DEFAULT_FIELD_LENGTH 25
18 #include <boost/config/warning_disable.hpp>
28 #include <boost/array.hpp>
29 #include <boost/date_time/gregorian/gregorian.hpp>
30 #include <boost/fusion/include/std_pair.hpp>
31 #include <boost/range/iterator_range.hpp>
33 ///////////////////////////////////////////////////////////////////////////////
34 // This streaming operator is needed to generate the output from the map below
35 // Yes, it's heresy, but this operator has to live in namespace std to be
36 // picked up by the compiler.
39 operator<<(std::ostream
& os
, std::pair
<int const, std::string
> v
)
41 os
<< v
.first
<< ": " << v
.second
;
46 #include <boost/spirit/include/karma.hpp>
47 #include <boost/spirit/include/karma_format.hpp>
49 using namespace boost::spirit
;
50 using namespace boost::spirit::ascii
;
52 ///////////////////////////////////////////////////////////////////////////////
53 // Output the given containers in list format
54 // Note: the format description does not depend on the type of the sequence
55 // nor does it depend on the type of the elements contained in the
57 ///////////////////////////////////////////////////////////////////////////////
58 template <typename Container
>
59 void output_container(std::ostream
& os
, Container
const& c
)
61 // output the container as a space separated sequence
64 *stream
, // format description
66 ) << std::endl
<< std::endl
;
68 // output the container as a space separated sequence
70 karma::format_delimited(
71 *stream
, // format description
74 ) << std::endl
<< std::endl
;
77 karma::format_delimited(
78 '[' << *stream
<< ']', // format description
81 ) << std::endl
<< std::endl
;
83 // output the container as a comma separated list
86 stream
% ", ", // format description
88 ) << std::endl
<< std::endl
;
92 '[' << (stream
% ", ") << ']', // format description
94 ) << std::endl
<< std::endl
;
98 '[' << -(stream
% ", ") << ']', // format description
100 ) << std::endl
<< std::endl
;
104 '[' << (+stream
| "empty") << ']', // format description
106 ) << std::endl
<< std::endl
;
108 // output the container as a comma separated list of items enclosed in '()'
111 ('(' << stream
<< ')') % ", ", // format description
113 ) << std::endl
<< std::endl
;
118 ('(' << stream
<< ')') % ", "
119 ) << ']', // format description
121 ) << std::endl
<< std::endl
;
123 // output the container as a HTML list
125 karma::format_delimited(
127 *verbatim
["<li>" << stream
<< "</li>"]
128 << "</ol>", // format description
133 // output the container as right aligned column
135 karma::format_delimited(
137 "|" << right_align
[stream
] << "|"
138 ], // format description
148 ///////////////////////////////////////////////////////////////////////////
150 int i
[4] = { 3, 6, 9, 12 };
152 std::cout
<< "-------------------------------------------------------------"
154 std::cout
<< "int i[]" << std::endl
;
155 output_container(std::cout
, boost::make_iterator_range(i
, i
+4));
157 ///////////////////////////////////////////////////////////////////////////
159 std::vector
<int> v (5);
160 std::generate(v
.begin(), v
.end(), std::rand
); // randomly fill the vector
162 std::cout
<< "-------------------------------------------------------------"
164 std::cout
<< "std::vector<int>" << std::endl
;
165 output_container(std::cout
, v
);
167 ///////////////////////////////////////////////////////////////////////////
174 std::cout
<< "-------------------------------------------------------------"
176 std::cout
<< "std::list<char>" << std::endl
;
177 output_container(std::cout
, l
);
179 ///////////////////////////////////////////////////////////////////////////
181 std::string
str("Hello world!");
183 std::cout
<< "-------------------------------------------------------------"
185 std::cout
<< "std::string" << std::endl
;
186 output_container(std::cout
, str
);
188 ///////////////////////////////////////////////////////////////////////////
190 boost::array
<long, 5> arr
;
191 std::generate(arr
.begin(), arr
.end(), std::rand
); // randomly fill the array
193 std::cout
<< "-------------------------------------------------------------"
195 std::cout
<< "boost::array<long, 5>" << std::endl
;
196 output_container(std::cout
, arr
);
198 ///////////////////////////////////////////////////////////////////////////
199 // vector of boost::date objects
200 // Note: any registered facets get used!
201 using namespace boost::gregorian
;
202 std::vector
<date
> dates
;
203 dates
.push_back(date(2005, Jun
, 25));
204 dates
.push_back(date(2006, Jan
, 13));
205 dates
.push_back(date(2007, May
, 03));
207 date_facet
* facet(new date_facet("%A %B %d, %Y"));
208 std::cout
.imbue(std::locale(std::cout
.getloc(), facet
));
210 std::cout
<< "-------------------------------------------------------------"
212 std::cout
<< "std::vector<boost::date>" << std::endl
;
213 output_container(std::cout
, dates
);
215 ///////////////////////////////////////////////////////////////////////////
216 // map of int --> string mappings
217 std::map
<int, std::string
> mappings
;
218 mappings
.insert(std::make_pair(0, "zero"));
219 mappings
.insert(std::make_pair(1, "one"));
220 mappings
.insert(std::make_pair(2, "two"));
222 std::cout
<< "-------------------------------------------------------------"
224 std::cout
<< "std::map<int, std::string>" << std::endl
;
225 output_container(std::cout
, mappings
);