basic_facilities.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // Copyright (c) 2001-2010 Hartmut Kaiser
  2. //
  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)
  5. // The main purpose of this example is to show the uniform and easy way of
  6. // output formatting for different container types.
  7. //
  8. // Since the 'stream' primitive used below uses the streaming operator defined
  9. // for the container value_type, you must make sure to have a corresponding
  10. // operator<<() available for this contained data type. OTOH this means, that
  11. // the format descriptions used below will be usable for any contained type as
  12. // long as this type has an associated streaming operator defined.
  13. // use a larger value for the alignment field width (default is 10)
  14. #define BOOST_KARMA_DEFAULT_FIELD_LENGTH 25
  15. #include <boost/config/warning_disable.hpp>
  16. #include <iostream>
  17. #include <string>
  18. #include <vector>
  19. #include <list>
  20. #include <map>
  21. #include <algorithm>
  22. #include <cstdlib>
  23. #include <boost/range.hpp>
  24. #include <boost/array.hpp>
  25. #include <boost/date_time/gregorian/gregorian.hpp>
  26. #include <boost/fusion/include/std_pair.hpp>
  27. ///////////////////////////////////////////////////////////////////////////////
  28. // This streaming operator is needed to generate the output from the map below
  29. // Yes, it's heresy, but this operator has to live in namespace std to be
  30. // picked up by the compiler.
  31. namespace std {
  32. inline std::ostream&
  33. operator<<(std::ostream& os, std::pair<int const, std::string> v)
  34. {
  35. os << v.first << ": " << v.second;
  36. return os;
  37. }
  38. }
  39. #include <boost/spirit/include/karma.hpp>
  40. #include <boost/spirit/include/karma_format.hpp>
  41. using namespace boost::spirit;
  42. using namespace boost::spirit::ascii;
  43. ///////////////////////////////////////////////////////////////////////////////
  44. // Output the given containers in list format
  45. // Note: the format description does not depend on the type of the sequence
  46. // nor does it depend on the type of the elements contained in the
  47. // sequence
  48. ///////////////////////////////////////////////////////////////////////////////
  49. template <typename Container>
  50. void output_container(std::ostream& os, Container const& c)
  51. {
  52. // output the container as a space separated sequence
  53. os <<
  54. karma::format(
  55. *stream, // format description
  56. c // data
  57. ) << std::endl << std::endl;
  58. // output the container as a space separated sequence
  59. os <<
  60. karma::format_delimited(
  61. *stream, // format description
  62. space, // delimiter
  63. c // data
  64. ) << std::endl << std::endl;
  65. os <<
  66. karma::format_delimited(
  67. '[' << *stream << ']', // format description
  68. space, // delimiter
  69. c // data
  70. ) << std::endl << std::endl;
  71. // output the container as a comma separated list
  72. os <<
  73. karma::format(
  74. stream % ", ", // format description
  75. c // data
  76. ) << std::endl << std::endl;
  77. os <<
  78. karma::format(
  79. '[' << (stream % ", ") << ']', // format description
  80. c // data
  81. ) << std::endl << std::endl;
  82. os <<
  83. karma::format(
  84. '[' << -(stream % ", ") << ']', // format description
  85. c // data
  86. ) << std::endl << std::endl;
  87. os <<
  88. karma::format(
  89. '[' << (+stream | "empty") << ']', // format description
  90. c // data
  91. ) << std::endl << std::endl;
  92. // output the container as a comma separated list of items enclosed in '()'
  93. os <<
  94. karma::format(
  95. ('(' << stream << ')') % ", ", // format description
  96. c // data
  97. ) << std::endl << std::endl;
  98. os <<
  99. karma::format(
  100. '[' << (
  101. ('(' << stream << ')') % ", "
  102. ) << ']', // format description
  103. c // data
  104. ) << std::endl << std::endl;
  105. // output the container as a HTML list
  106. os <<
  107. karma::format_delimited(
  108. "<ol>" <<
  109. *verbatim["<li>" << stream << "</li>"]
  110. << "</ol>", // format description
  111. '\n', // delimiter
  112. c // data
  113. ) << std::endl;
  114. // output the container as right aligned column
  115. os <<
  116. karma::format_delimited(
  117. *verbatim[
  118. "|" << right_align[stream] << "|"
  119. ], // format description
  120. '\n', // delimiter
  121. c // data
  122. ) << std::endl;
  123. os << std::endl;
  124. }
  125. int main()
  126. {
  127. ///////////////////////////////////////////////////////////////////////////
  128. // C-style array
  129. int i[4] = { 3, 6, 9, 12 };
  130. std::cout << "-------------------------------------------------------------"
  131. << std::endl;
  132. std::cout << "int i[]" << std::endl;
  133. output_container(std::cout, boost::make_iterator_range(i, i+4));
  134. ///////////////////////////////////////////////////////////////////////////
  135. // vector
  136. std::vector<int> v (5);
  137. std::generate(v.begin(), v.end(), std::rand); // randomly fill the vector
  138. std::cout << "-------------------------------------------------------------"
  139. << std::endl;
  140. std::cout << "std::vector<int>" << std::endl;
  141. output_container(std::cout, v);
  142. ///////////////////////////////////////////////////////////////////////////
  143. // list
  144. std::list<char> l;
  145. l.push_back('A');
  146. l.push_back('B');
  147. l.push_back('C');
  148. std::cout << "-------------------------------------------------------------"
  149. << std::endl;
  150. std::cout << "std::list<char>" << std::endl;
  151. output_container(std::cout, l);
  152. ///////////////////////////////////////////////////////////////////////////
  153. // strings
  154. std::string str("Hello world!");
  155. std::cout << "-------------------------------------------------------------"
  156. << std::endl;
  157. std::cout << "std::string" << std::endl;
  158. output_container(std::cout, str);
  159. ///////////////////////////////////////////////////////////////////////////
  160. // boost::array
  161. boost::array<long, 5> arr;
  162. std::generate(arr.begin(), arr.end(), std::rand); // randomly fill the array
  163. std::cout << "-------------------------------------------------------------"
  164. << std::endl;
  165. std::cout << "boost::array<long, 5>" << std::endl;
  166. output_container(std::cout, arr);
  167. ///////////////////////////////////////////////////////////////////////////
  168. // vector of boost::date objects
  169. // Note: any registered facets get used!
  170. using namespace boost::gregorian;
  171. std::vector<date> dates;
  172. dates.push_back(date(2005, Jun, 25));
  173. dates.push_back(date(2006, Jan, 13));
  174. dates.push_back(date(2007, May, 03));
  175. date_facet* facet(new date_facet("%A %B %d, %Y"));
  176. std::cout.imbue(std::locale(std::cout.getloc(), facet));
  177. std::cout << "-------------------------------------------------------------"
  178. << std::endl;
  179. std::cout << "std::vector<boost::date>" << std::endl;
  180. output_container(std::cout, dates);
  181. ///////////////////////////////////////////////////////////////////////////
  182. // map of int --> string mappings
  183. std::map<int, std::string> mappings;
  184. mappings.insert(std::make_pair(0, "zero"));
  185. mappings.insert(std::make_pair(1, "one"));
  186. mappings.insert(std::make_pair(2, "two"));
  187. std::cout << "-------------------------------------------------------------"
  188. << std::endl;
  189. std::cout << "std::map<int, std::string>" << std::endl;
  190. output_container(std::cout, mappings);
  191. return 0;
  192. }