| 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 | |
| 6 | // The purpose of this example is to show how a simple custom generator |
| 7 | // directive can be written. We develop a custom generator allowing to wrap |
| 8 | // the generated output after each 5th column. |
| 9 | // |
| 10 | // For more information see: http://boost-spirit.com/home/?page_id=659 |
| 11 | |
| 12 | #include <boost/spirit/include/karma_generate_attr.hpp> |
| 13 | #include <boost/spirit/include/karma_char.hpp> |
| 14 | #include <boost/spirit/include/karma_operator.hpp> |
| 15 | #include <boost/spirit/include/karma_numeric.hpp> |
| 16 | |
| 17 | #include <iostream> |
| 18 | #include <string> |
| 19 | #include "simple_columns_directive.hpp" |
| 20 | |
| 21 | namespace karma = boost::spirit::karma; |
| 22 | |
| 23 | int main() |
| 24 | { |
| 25 | using custom_generator::columns; |
| 26 | |
| 27 | std::vector<int> v; |
| 28 | for (int i = 0; i < 17; ++i) |
| 29 | v.push_back(x: i); |
| 30 | |
| 31 | std::string generated; |
| 32 | std::back_insert_iterator<std::string> sink(generated); |
| 33 | |
| 34 | bool result = karma::generate_delimited( |
| 35 | sink, expr: columns[*karma::int_], delimiter: karma::space, attr: v); |
| 36 | if (result) |
| 37 | { |
| 38 | std::cout << "-------------------------------- \n"; |
| 39 | std::cout << "Generation succeeded\n"; |
| 40 | std::cout << "generated output: "<< "\n"<< generated << "\n"; |
| 41 | std::cout << "-------------------------------- \n"; |
| 42 | } |
| 43 | else |
| 44 | { |
| 45 | std::cout << "-------------------------------- \n"; |
| 46 | std::cout << "Generation failed\n"; |
| 47 | std::cout << "-------------------------------- \n"; |
| 48 | } |
| 49 | return 0; |
| 50 | } |
| 51 |
