]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/type_erasure/example/print_sequence.cpp
1 // Boost.TypeErasure library
3 // Copyright 2011 Steven Watanabe
5 // Distributed under the Boost Software License Version 1.0. (See
6 // accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
14 (For the source of this example see
15 [@boost:/libs/type_erasure/example/print_sequence.cpp print_sequence.cpp])
17 This example defines a class hierarchy that allows a sequence
18 to be formatted in several different ways. We'd like to be
19 able to handle any sequence and any stream type, since the
20 range formatting is independent of the formatting of
21 individual elements. Thus, our interface needs to look
25 class abstract_printer {
27 template<class CharT, class Traits, class Range>
28 virtual void print(std::basic_ostream<CharT, Traits>& os, const Range& r) const = 0;
32 Unfortunately, this is illegal because a virtual function
33 cannot be a template. However, we can define a
34 class with much the same behavior using Boost.TypeErasure.
37 #include <boost/type_erasure/any.hpp>
38 #include <boost/type_erasure/iterator.hpp>
39 #include <boost/type_erasure/operators.hpp>
40 #include <boost/type_erasure/tuple.hpp>
41 #include <boost/type_erasure/same_type.hpp>
42 #include <boost/range/begin.hpp>
43 #include <boost/range/end.hpp>
44 #include <boost/range/iterator.hpp>
49 using namespace boost::type_erasure
;
51 struct _t
: placeholder
{};
52 struct _iter
: placeholder
{};
53 struct _os
: placeholder
{};
55 template<class T
, class U
= _self
>
56 struct base_and_derived
58 static T
& apply(U
& arg
) { return arg
; }
62 namespace type_erasure
{
64 template<class T
, class U
, class Base
>
65 struct concept_interface
<base_and_derived
<T
, U
>, Base
, U
> : Base
67 operator typename rebind_any
<Base
, const T
&>::type() const
69 return call(base_and_derived
<T
, U
>(), const_cast<concept_interface
&>(*this));
71 operator typename rebind_any
<Base
, T
&>::type()
73 return call(base_and_derived
<T
, U
>(), *this);
80 // abstract_printer - An abstract base class for formatting sequences.
81 class abstract_printer
{
83 // print - write a sequence to a std::ostream in a manner
84 // specific to the derived class.
86 // Requires: Range must be a Forward Range whose elements can be
88 template<class CharT
, class Traits
, class Range
>
89 void print(std::basic_ostream
<CharT
, Traits
>& os
, const Range
& r
) const {
90 // Capture the arguments
91 typename
boost::range_iterator
<const Range
>::type
92 first(boost::begin(r
)),
94 tuple
<requirements
, _os
&, _iter
, _iter
> args(os
, first
, last
);
95 // and forward to the real implementation
96 do_print(get
<0>(args
), get
<1>(args
), get
<2>(args
));
98 virtual ~abstract_printer() {}
100 // define the concept requirements of the arguments of
101 // print and typedef the any types.
102 typedef boost::mpl::vector
<
103 base_and_derived
<std::ios_base
, _os
>,
104 ostreamable
<_os
, _t
>,
105 ostreamable
<_os
, const char*>,
106 forward_iterator
<_iter
, const _t
&>,
107 same_type
<_t
, forward_iterator
<_iter
, const _t
&>::value_type
>
109 typedef boost::type_erasure::any
<requirements
, _os
&> ostream_type
;
110 typedef boost::type_erasure::any
<requirements
, _iter
> iterator_type
;
111 // do_print - This method must be implemented by derived classes
112 virtual void do_print(
113 ostream_type os
, iterator_type first
, iterator_type last
) const = 0;
116 // separator_printer - writes the elements of a sequence
117 // separated by a fixed string. For example, if
118 // the separator is ", " separator_printer produces
119 // a comma separated list.
120 class separator_printer
: public abstract_printer
{
122 explicit separator_printer(const std::string
& sep
) : separator(sep
) {}
124 virtual void do_print(
125 ostream_type os
, iterator_type first
, iterator_type last
) const {
129 for(; first
!= last
; ++first
) {
130 os
<< separator
.c_str() << *first
;
135 std::string separator
;
138 // column_separator_printer - like separator_printer, but
139 // also inserts a line break after every n elements.
140 class column_separator_printer
: public abstract_printer
{
142 column_separator_printer(const std::string
& sep
, std::size_t num_columns
)
147 virtual void do_print(
148 ostream_type os
, iterator_type first
, iterator_type last
) const {
149 std::size_t count
= 0;
150 for(; first
!= last
; ++first
) {
152 boost::type_erasure::any
<requirements
, _iter
> temp
= first
;
155 os
<< separator
.c_str();
157 if(++count
% cols
== 0) {
163 std::string separator
;
167 // aligned_column_printer - formats a sequence in columns
168 // reading down. For example, given the sequence
169 // { 1, 2, 3, 4, 5 }, aligned_column_printer might print
173 class aligned_column_printer
: public abstract_printer
{
175 aligned_column_printer(std::size_t column_width
, std::size_t num_columns
)
176 : width(column_width
),
180 virtual void do_print(
181 ostream_type os
, iterator_type first
, iterator_type last
) const
183 if(first
== last
) return;
184 std::vector
<iterator_type
> column_iterators
;
186 // find the tops of the columns
187 std::size_t count
= 0;
188 for(iterator_type iter
= first
; iter
!= last
; ++iter
) {
191 std::size_t rows
= (count
+ cols
- 1) / cols
;
193 for(iterator_type iter
= first
; iter
!= last
; ++iter
) {
194 if(count
% rows
== 0) {
195 column_iterators
.push_back(iter
);
200 iterator_type last_col
= column_iterators
.back();
202 // print the full rows
203 while(column_iterators
.back() != last
) {
204 for(std::vector
<iterator_type
>::iterator
205 iter
= column_iterators
.begin(),
206 end
= column_iterators
.end(); iter
!= end
; ++iter
)
208 static_cast<std::ios_base
&>(os
).width(width
);
215 // print the rows that are missing the last column
216 column_iterators
.pop_back();
217 if(!column_iterators
.empty()) {
218 while(column_iterators
.back() != last_col
) {
219 for(std::vector
<iterator_type
>::iterator
220 iter
= column_iterators
.begin(),
221 end
= column_iterators
.end(); iter
!= end
; ++iter
)
223 static_cast<std::ios_base
&>(os
).width(width
);
237 int test
[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
238 separator_printer
p1(",");
239 p1
.print(std::cout
, test
);
240 std::cout
<< std::endl
;
241 column_separator_printer
p2(",", 4);
242 p2
.print(std::cout
, test
);
243 std::cout
<< std::endl
;
244 aligned_column_printer
p3(16, 4);
245 p3
.print(std::cout
, test
);