]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/example/karma/generate_code.cpp
1 // Copyright (c) 2001-2010 Hartmut Kaiser
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)
6 ///////////////////////////////////////////////////////////////////////////////
8 // Several small snippets generating different C++ code constructs
10 // [ HK October 08, 2009 ] Spirit V2.2
12 ///////////////////////////////////////////////////////////////////////////////
14 #include <boost/spirit/include/karma.hpp>
15 #include <boost/phoenix.hpp>
23 namespace karma
= boost::spirit::karma
;
24 namespace phoenix
= boost::phoenix
;
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>()>
30 int_array(char const* name
) : int_array::base_type(start
)
39 start
= array_def(phoenix::size(_val
)) << " = " << initializer
41 array_def
= "int " << lit(name
) << "[" << uint_(_r1
) << "]";
42 initializer
= "{ " << -(int_
% ", ") << " }";
45 karma::rule
<Iterator
, void(unsigned)> array_def
;
46 karma::rule
<Iterator
, std::vector
<int>()> initializer
;
47 karma::rule
<Iterator
, std::vector
<int>()> start
;
50 typedef std::back_insert_iterator
<std::string
> iterator_type
;
51 bool generate_array(char const* name
, std::vector
<int> const& v
)
53 std::string generated
;
54 iterator_type
sink(generated
);
55 int_array
<iterator_type
> g(name
);
56 if (karma::generate(sink
, g
, v
))
58 std::cout
<< generated
;
65 ///////////////////////////////////////////////////////////////////////////////
67 ///////////////////////////////////////////////////////////////////////////////
70 // generate an array of integers with initializers
76 client::generate_array("array1", v
);