]>
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/config/warning_disable.hpp>
15 #include <boost/spirit/include/karma.hpp>
16 #include <boost/spirit/include/phoenix.hpp>
24 namespace karma
= boost::spirit::karma
;
25 namespace phoenix
= boost::phoenix
;
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>()>
31 int_array(char const* name
) : int_array::base_type(start
)
40 start
= array_def(phoenix::size(_val
)) << " = " << initializer
42 array_def
= "int " << lit(name
) << "[" << uint_(_r1
) << "]";
43 initializer
= "{ " << -(int_
% ", ") << " }";
46 karma::rule
<Iterator
, void(unsigned)> array_def
;
47 karma::rule
<Iterator
, std::vector
<int>()> initializer
;
48 karma::rule
<Iterator
, std::vector
<int>()> start
;
51 typedef std::back_insert_iterator
<std::string
> iterator_type
;
52 bool generate_array(char const* name
, std::vector
<int> const& v
)
54 std::string generated
;
55 iterator_type
sink(generated
);
56 int_array
<iterator_type
> g(name
);
57 if (karma::generate(sink
, g
, v
))
59 std::cout
<< generated
;
66 ///////////////////////////////////////////////////////////////////////////////
68 ///////////////////////////////////////////////////////////////////////////////
71 // generate an array of integers with initializers
77 client::generate_array("array1", v
);