]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/example/karma/generate_code.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / spirit / example / karma / generate_code.cpp
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 ///////////////////////////////////////////////////////////////////////////////
7 //
8 // Several small snippets generating different C++ code constructs
9 //
10 // [ HK October 08, 2009 ] Spirit V2.2
11 //
12 ///////////////////////////////////////////////////////////////////////////////
13
14 #include <boost/config/warning_disable.hpp>
15 #include <boost/spirit/include/karma.hpp>
16 #include <boost/spirit/include/phoenix.hpp>
17
18 #include <iostream>
19 #include <string>
20 #include <complex>
21
22 namespace client
23 {
24 namespace karma = boost::spirit::karma;
25 namespace phoenix = boost::phoenix;
26
27 // create for instance: int name[5] = { 1, 2, 3, 4, 5 };
28 template <typename Iterator>
29 struct int_array : karma::grammar<Iterator, std::vector<int>()>
30 {
31 int_array(char const* name) : int_array::base_type(start)
32 {
33 using karma::int_;
34 using karma::uint_;
35 using karma::eol;
36 using karma::lit;
37 using karma::_val;
38 using karma::_r1;
39
40 start = array_def(phoenix::size(_val)) << " = " << initializer
41 << ';' << eol;
42 array_def = "int " << lit(name) << "[" << uint_(_r1) << "]";
43 initializer = "{ " << -(int_ % ", ") << " }";
44 }
45
46 karma::rule<Iterator, void(unsigned)> array_def;
47 karma::rule<Iterator, std::vector<int>()> initializer;
48 karma::rule<Iterator, std::vector<int>()> start;
49 };
50
51 typedef std::back_insert_iterator<std::string> iterator_type;
52 bool generate_array(char const* name, std::vector<int> const& v)
53 {
54 std::string generated;
55 iterator_type sink(generated);
56 int_array<iterator_type> g(name);
57 if (karma::generate(sink, g, v))
58 {
59 std::cout << generated;
60 return true;
61 }
62 return false;
63 }
64 }
65
66 ///////////////////////////////////////////////////////////////////////////////
67 // Main program
68 ///////////////////////////////////////////////////////////////////////////////
69 int main()
70 {
71 // generate an array of integers with initializers
72 std::vector<int> v;
73 v.push_back(1);
74 v.push_back(2);
75 v.push_back(3);
76 v.push_back(4);
77 client::generate_array("array1", v);
78
79 return 0;
80 }