File: basic_facilities.cpp

package info (click to toggle)
boost1.74 1.74.0-9
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 464,084 kB
  • sloc: cpp: 3,338,324; xml: 131,293; python: 33,088; ansic: 14,336; asm: 4,034; sh: 3,351; makefile: 1,193; perl: 1,036; yacc: 478; php: 212; ruby: 102; lisp: 24; sql: 13; csh: 6
file content (229 lines) | stat: -rw-r--r-- 8,444 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
//  Copyright (c) 2001-2010 Hartmut Kaiser
// 
//  Distributed under the Boost Software License, Version 1.0. (See accompanying 
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

//  The main purpose of this example is to show the uniform and easy way of
//  output formatting for different container types. 
//
//  Since the 'stream' primitive used below uses the streaming operator defined 
//  for the container value_type, you must make sure to have a corresponding
//  operator<<() available for this contained data type. OTOH this means, that
//  the format descriptions used below will be usable for any contained type as
//  long as this type has an associated streaming operator defined.

//  use a larger value for the alignment field width (default is 10)
#define BOOST_KARMA_DEFAULT_FIELD_LENGTH 25

#include <boost/config/warning_disable.hpp>

#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <algorithm>
#include <cstdlib> 

#include <boost/array.hpp>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/fusion/include/std_pair.hpp>
#include <boost/range/iterator_range.hpp>

///////////////////////////////////////////////////////////////////////////////
// This streaming operator is needed to generate the output from the map below
// Yes, it's heresy, but this operator has to live in namespace std to be 
// picked up by the compiler.
namespace std {
    inline std::ostream& 
    operator<<(std::ostream& os, std::pair<int const, std::string> v)
    {
        os << v.first << ": " << v.second;
        return os;
    }
}

#include <boost/spirit/include/karma.hpp>
#include <boost/spirit/include/karma_format.hpp>

using namespace boost::spirit;
using namespace boost::spirit::ascii;

///////////////////////////////////////////////////////////////////////////////
// Output the given containers in list format
// Note: the format description does not depend on the type of the sequence
//       nor does it depend on the type of the elements contained in the 
//       sequence
///////////////////////////////////////////////////////////////////////////////
template <typename Container>
void output_container(std::ostream& os, Container const& c)
{
    // output the container as a space separated sequence
    os << 
        karma::format(
            *stream,                              // format description
            c                                     // data
        ) << std::endl << std::endl;

    // output the container as a space separated sequence
    os << 
        karma::format_delimited(
            *stream,                              // format description
            space,                                // delimiter
            c                                     // data
        ) << std::endl << std::endl;

    os << 
        karma::format_delimited(
            '[' << *stream << ']',                // format description
            space,                                // delimiter
            c                                     // data
        ) << std::endl << std::endl;

    // output the container as a comma separated list
    os << 
        karma::format(
            stream % ", ",                        // format description
            c                                     // data
        ) << std::endl << std::endl;

    os << 
        karma::format(
            '[' << (stream % ", ") << ']',        // format description
            c                                     // data
        ) << std::endl << std::endl;

    os << 
        karma::format(
            '[' << -(stream % ", ") << ']',       // format description
            c                                     // data
        ) << std::endl << std::endl;

    os << 
        karma::format(
            '[' << (+stream | "empty") << ']',    // format description
            c                                     // data
        ) << std::endl << std::endl;

    // output the container as a comma separated list of items enclosed in '()'
    os << 
        karma::format(
            ('(' << stream << ')') % ", ",        // format description
            c                                     // data
        ) << std::endl << std::endl;

    os << 
        karma::format(
            '[' << (  
                ('(' << stream << ')') % ", "
             )  << ']',                           // format description
            c                                     // data
        ) << std::endl << std::endl;

    // output the container as a HTML list
    os << 
        karma::format_delimited(
            "<ol>" << 
                *verbatim["<li>" << stream << "</li>"]
            << "</ol>",                           // format description
            '\n',                                 // delimiter
            c                                     // data
        ) << std::endl;

    // output the container as right aligned column
    os << 
        karma::format_delimited(
           *verbatim[
                "|" << right_align[stream] << "|"
            ],                                    // format description
            '\n',                                 // delimiter
            c                                     // data
        ) << std::endl;

    os << std::endl;
}

int main()
{
    ///////////////////////////////////////////////////////////////////////////
    // C-style array
    int i[4] = { 3, 6, 9, 12 };
    
    std::cout << "-------------------------------------------------------------" 
              << std::endl;
    std::cout << "int i[]" << std::endl;
    output_container(std::cout, boost::make_iterator_range(i, i+4));

    ///////////////////////////////////////////////////////////////////////////
    // vector
    std::vector<int> v (5);
    std::generate(v.begin(), v.end(), std::rand); // randomly fill the vector

    std::cout << "-------------------------------------------------------------" 
              << std::endl;
    std::cout << "std::vector<int>" << std::endl;
    output_container(std::cout, v);

    ///////////////////////////////////////////////////////////////////////////
    // list
    std::list<char> l;
    l.push_back('A');
    l.push_back('B');
    l.push_back('C');

    std::cout << "-------------------------------------------------------------" 
              << std::endl;
    std::cout << "std::list<char>" << std::endl;
    output_container(std::cout, l);

    ///////////////////////////////////////////////////////////////////////////
    // strings
    std::string str("Hello world!");

    std::cout << "-------------------------------------------------------------" 
              << std::endl;
    std::cout << "std::string" << std::endl;
    output_container(std::cout, str);

    ///////////////////////////////////////////////////////////////////////////
    // boost::array
    boost::array<long, 5> arr;
    std::generate(arr.begin(), arr.end(), std::rand); // randomly fill the array

    std::cout << "-------------------------------------------------------------" 
              << std::endl;
    std::cout << "boost::array<long, 5>" << std::endl;
    output_container(std::cout, arr);

    ///////////////////////////////////////////////////////////////////////////
    //  vector of boost::date objects
    //  Note: any registered facets get used!
    using namespace boost::gregorian;
    std::vector<date> dates;
    dates.push_back(date(2005, Jun, 25));
    dates.push_back(date(2006, Jan, 13));
    dates.push_back(date(2007, May, 03));

    date_facet* facet(new date_facet("%A %B %d, %Y"));
    std::cout.imbue(std::locale(std::cout.getloc(), facet));

    std::cout << "-------------------------------------------------------------" 
              << std::endl;
    std::cout << "std::vector<boost::date>" << std::endl;
    output_container(std::cout, dates);

    ///////////////////////////////////////////////////////////////////////////
    //  map of int --> string mappings
    std::map<int, std::string> mappings;
    mappings.insert(std::make_pair(0, "zero"));
    mappings.insert(std::make_pair(1, "one"));
    mappings.insert(std::make_pair(2, "two"));

    std::cout << "-------------------------------------------------------------" 
              << std::endl;
    std::cout << "std::map<int, std::string>" << std::endl;
    output_container(std::cout, mappings);

    return 0;
}