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