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