1 /*=============================================================================
2 Copyright (c) 2002-2010 Hartmut Kaiser
3 Copyright (c) 2002-2010 Joel de Guzman
4
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 ///////////////////////////////////////////////////////////////////////////////
9 //
10 // This sample demonstrates a generator formatting and printing a matrix
11 // of integers taken from a simple vector of vectors. The size and the
12 // contents of the printed matrix is generated randomly.
13 //
14 ///////////////////////////////////////////////////////////////////////////////
15
16 #include <boost/config/warning_disable.hpp>
17 #include <boost/spirit/include/karma.hpp>
18
19 #include <iostream>
20 #include <string>
21 #include <vector>
22 #include <cstdlib>
23 #include <ctime>
24
25 namespace karma = boost::spirit::karma;
26
27 namespace client
28 {
29 ///////////////////////////////////////////////////////////////////////////
30 // Our matrix generator
31 ///////////////////////////////////////////////////////////////////////////
32 //[tutorial_karma_nummatrix_grammar
33 template <typename OutputIterator>
34 struct matrix_grammar
35 : karma::grammar<OutputIterator, std::vector<std::vector<int> >()>
36 {
matrix_grammarclient::matrix_grammar37 matrix_grammar()
38 : matrix_grammar::base_type(matrix)
39 {
40 using karma::int_;
41 using karma::right_align;
42 using karma::eol;
43
44 element = right_align(10)[int_];
45 row = '|' << *element << '|';
46 matrix = row % eol;
47 }
48
49 karma::rule<OutputIterator, std::vector<std::vector<int> >()> matrix;
50 karma::rule<OutputIterator, std::vector<int>()> row;
51 karma::rule<OutputIterator, int()> element;
52 };
53 //]
54
55 //[tutorial_karma_nummatrix
56 template <typename OutputIterator>
generate_matrix(OutputIterator & sink,std::vector<std::vector<int>> const & v)57 bool generate_matrix(OutputIterator& sink
58 , std::vector<std::vector<int> > const& v)
59 {
60 matrix_grammar<OutputIterator> matrix;
61 return karma::generate(
62 sink, // destination: output iterator
63 matrix, // the generator
64 v // the data to output
65 );
66 }
67 //]
68 }
69
70 ////////////////////////////////////////////////////////////////////////////
71 // Main program
72 ////////////////////////////////////////////////////////////////////////////
73 int
main()74 main()
75 {
76 std::cout << "/////////////////////////////////////////////////////////\n\n";
77 std::cout << "\tPrinting integers in a matrix using Spirit...\n\n";
78 std::cout << "/////////////////////////////////////////////////////////\n\n";
79
80 // here we put the data to generate
81 std::vector<std::vector<int> > v;
82
83 // now, generate the size and the contents for the matrix
84 std::srand((unsigned int)std::time(NULL));
85 std::size_t rows = std::rand() / (RAND_MAX / 10);
86 std::size_t columns = std::rand() / (RAND_MAX / 10);
87
88 v.resize(rows);
89 for (std::size_t row = 0; row < rows; ++row)
90 {
91 v[row].resize(columns);
92 std::generate(v[row].begin(), v[row].end(), std::rand);
93 }
94
95 // ok, we got the matrix, now print it out
96 std::string generated;
97 std::back_insert_iterator<std::string> sink(generated);
98 if (!client::generate_matrix(sink, v))
99 {
100 std::cout << "-------------------------\n";
101 std::cout << "Generating failed\n";
102 std::cout << "-------------------------\n";
103 }
104 else
105 {
106 std::cout << "-------------------------\n";
107 std::cout << "Generated:\n" << generated << "\n";
108 std::cout << "-------------------------\n";
109 }
110
111 return 0;
112 }
113
114
115