]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /*============================================================================= |
2 | Copyright (c) 2001-2011 Hartmut Kaiser | |
3 | http://spirit.sourceforge.net/ | |
4 | ||
5 | Distributed under the Boost Software License, Version 1.0. (See accompanying | |
6 | file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
7 | =============================================================================*/ | |
7c673cae FG |
8 | |
9 | //[customize_karma_embedded_container_includes | |
10 | #include <boost/spirit/include/karma.hpp> | |
11 | #include <iostream> | |
12 | #include <vector> | |
13 | //] | |
14 | ||
15 | /////////////////////////////////////////////////////////////////////////////// | |
16 | //[customize_karma_embedded_container_data | |
17 | namespace client | |
18 | { | |
19 | struct embedded_container | |
20 | { | |
21 | // expose the iterator of the embedded vector as our iterator | |
22 | typedef std::vector<int>::const_iterator iterator; | |
23 | ||
24 | // expose the type of the held data elements as our type | |
25 | typedef std::vector<int>::value_type type; | |
26 | ||
27 | // this is the vector holding the actual elements we need to generate | |
28 | // output from | |
29 | std::vector<int> data; | |
30 | }; | |
31 | } | |
32 | //] | |
33 | ||
34 | //[customize_karma_embedded_container_traits | |
35 | // All specializations of attribute customization points have to be placed into | |
36 | // the namespace boost::spirit::traits. | |
37 | // | |
38 | // Note that all templates below are specialized using the 'const' type. | |
39 | // This is necessary as all attributes in Karma are 'const'. | |
40 | namespace boost { namespace spirit { namespace traits | |
41 | { | |
42 | // The specialization of the template 'is_container<>' will tell the | |
43 | // library to treat the type 'client::embedded_container' as a | |
44 | // container holding the items to generate output from. | |
45 | template <> | |
46 | struct is_container<client::embedded_container const> | |
47 | : mpl::true_ | |
48 | {}; | |
49 | ||
50 | // The specialization of the template 'container_iterator<>' will be | |
51 | // invoked by the library to evaluate the iterator type to be used | |
52 | // for iterating the data elements in the container. We simply return | |
53 | // the type of the iterator exposed by the embedded 'std::vector<int>'. | |
54 | template <> | |
55 | struct container_iterator<client::embedded_container const> | |
56 | { | |
57 | typedef client::embedded_container::iterator type; | |
58 | }; | |
59 | ||
60 | // The specialization of the templates 'begin_container<>' and | |
61 | // 'end_container<>' below will be used by the library to get the iterators | |
62 | // pointing to the begin and the end of the data to generate output from. | |
63 | // These specializations simply return the 'begin' and 'end' iterators as | |
64 | // exposed by the embedded 'std::vector<int>'. | |
65 | // | |
66 | // The passed argument refers to the attribute instance passed to the list | |
67 | // generator. | |
68 | template <> | |
69 | struct begin_container<client::embedded_container const> | |
70 | { | |
71 | static client::embedded_container::iterator | |
72 | call(client::embedded_container const& d) | |
73 | { | |
74 | return d.data.begin(); | |
75 | } | |
76 | }; | |
77 | ||
78 | template <> | |
79 | struct end_container<client::embedded_container const> | |
80 | { | |
81 | static client::embedded_container::iterator | |
82 | call(client::embedded_container const& d) | |
83 | { | |
84 | return d.data.end(); | |
85 | } | |
86 | }; | |
87 | }}} | |
88 | //] | |
89 | ||
90 | /////////////////////////////////////////////////////////////////////////////// | |
91 | namespace karma = boost::spirit::karma; | |
92 | ||
93 | int main() | |
94 | { | |
95 | //[customize_karma_embedded_container | |
96 | client::embedded_container d1; // create some test data | |
97 | d1.data.push_back(1); | |
98 | d1.data.push_back(2); | |
99 | d1.data.push_back(3); | |
100 | ||
101 | // use the instance of an 'client::embedded_container' instead of a | |
102 | // STL vector | |
103 | std::cout << karma::format(karma::int_ % ", ", d1) << std::endl; // prints: '1, 2, 3' | |
104 | //] | |
105 | return 0; | |
106 | } | |
107 |