]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/example/karma/basic_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 // 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
26 #include <boost/array.hpp>
27 #include <boost/date_time/gregorian/gregorian.hpp>
28 #include <boost/fusion/include/std_pair.hpp>
29 #include <boost/range/iterator_range.hpp>
31 ///////////////////////////////////////////////////////////////////////////////
32 // This streaming operator is needed to generate the output from the map below
33 // Yes, it's heresy, but this operator has to live in namespace std to be
34 // picked up by the compiler.
37 operator<<(std::ostream
& os
, std::pair
<int const, std::string
> v
)
39 os
<< v
.first
<< ": " << v
.second
;
44 #include <boost/spirit/include/karma.hpp>
45 #include <boost/spirit/include/karma_format.hpp>
47 using namespace boost::spirit
;
48 using namespace boost::spirit::ascii
;
50 ///////////////////////////////////////////////////////////////////////////////
51 // Output the given containers in list format
52 // Note: the format description does not depend on the type of the sequence
53 // nor does it depend on the type of the elements contained in the
55 ///////////////////////////////////////////////////////////////////////////////
56 template <typename Container
>
57 void output_container(std::ostream
& os
, Container
const& c
)
59 // output the container as a space separated sequence
62 *stream
, // format description
64 ) << std::endl
<< std::endl
;
66 // output the container as a space separated sequence
68 karma::format_delimited(
69 *stream
, // format description
72 ) << std::endl
<< std::endl
;
75 karma::format_delimited(
76 '[' << *stream
<< ']', // format description
79 ) << std::endl
<< std::endl
;
81 // output the container as a comma separated list
84 stream
% ", ", // format description
86 ) << std::endl
<< std::endl
;
90 '[' << (stream
% ", ") << ']', // format description
92 ) << std::endl
<< std::endl
;
96 '[' << -(stream
% ", ") << ']', // format description
98 ) << std::endl
<< std::endl
;
102 '[' << (+stream
| "empty") << ']', // format description
104 ) << std::endl
<< std::endl
;
106 // output the container as a comma separated list of items enclosed in '()'
109 ('(' << stream
<< ')') % ", ", // format description
111 ) << std::endl
<< std::endl
;
116 ('(' << stream
<< ')') % ", "
117 ) << ']', // format description
119 ) << std::endl
<< std::endl
;
121 // output the container as a HTML list
123 karma::format_delimited(
125 *verbatim
["<li>" << stream
<< "</li>"]
126 << "</ol>", // format description
131 // output the container as right aligned column
133 karma::format_delimited(
135 "|" << right_align
[stream
] << "|"
136 ], // format description
146 ///////////////////////////////////////////////////////////////////////////
148 int i
[4] = { 3, 6, 9, 12 };
150 std::cout
<< "-------------------------------------------------------------"
152 std::cout
<< "int i[]" << std::endl
;
153 output_container(std::cout
, boost::make_iterator_range(i
, i
+4));
155 ///////////////////////////////////////////////////////////////////////////
157 std::vector
<int> v (5);
158 std::generate(v
.begin(), v
.end(), std::rand
); // randomly fill the vector
160 std::cout
<< "-------------------------------------------------------------"
162 std::cout
<< "std::vector<int>" << std::endl
;
163 output_container(std::cout
, v
);
165 ///////////////////////////////////////////////////////////////////////////
172 std::cout
<< "-------------------------------------------------------------"
174 std::cout
<< "std::list<char>" << std::endl
;
175 output_container(std::cout
, l
);
177 ///////////////////////////////////////////////////////////////////////////
179 std::string
str("Hello world!");
181 std::cout
<< "-------------------------------------------------------------"
183 std::cout
<< "std::string" << std::endl
;
184 output_container(std::cout
, str
);
186 ///////////////////////////////////////////////////////////////////////////
188 boost::array
<long, 5> arr
;
189 std::generate(arr
.begin(), arr
.end(), std::rand
); // randomly fill the array
191 std::cout
<< "-------------------------------------------------------------"
193 std::cout
<< "boost::array<long, 5>" << std::endl
;
194 output_container(std::cout
, arr
);
196 ///////////////////////////////////////////////////////////////////////////
197 // vector of boost::date objects
198 // Note: any registered facets get used!
199 using namespace boost::gregorian
;
200 std::vector
<date
> dates
;
201 dates
.push_back(date(2005, Jun
, 25));
202 dates
.push_back(date(2006, Jan
, 13));
203 dates
.push_back(date(2007, May
, 03));
205 date_facet
* facet(new date_facet("%A %B %d, %Y"));
206 std::cout
.imbue(std::locale(std::cout
.getloc(), facet
));
208 std::cout
<< "-------------------------------------------------------------"
210 std::cout
<< "std::vector<boost::date>" << std::endl
;
211 output_container(std::cout
, dates
);
213 ///////////////////////////////////////////////////////////////////////////
214 // map of int --> string mappings
215 std::map
<int, std::string
> mappings
;
216 mappings
.insert(std::make_pair(0, "zero"));
217 mappings
.insert(std::make_pair(1, "one"));
218 mappings
.insert(std::make_pair(2, "two"));
220 std::cout
<< "-------------------------------------------------------------"
222 std::cout
<< "std::map<int, std::string>" << std::endl
;
223 output_container(std::cout
, mappings
);