print_sequence.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // Boost.TypeErasure library
  2. //
  3. // Copyright 2011 Steven Watanabe
  4. //
  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)
  8. //
  9. // $Id$
  10. //[print_sequence
  11. /*`
  12. (For the source of this example see
  13. [@boost:/libs/type_erasure/example/print_sequence.cpp print_sequence.cpp])
  14. This example defines a class hierarchy that allows a sequence
  15. to be formatted in several different ways. We'd like to be
  16. able to handle any sequence and any stream type, since the
  17. range formatting is independent of the formatting of
  18. individual elements. Thus, our interface needs to look
  19. something like this:
  20. ``
  21. class abstract_printer {
  22. public:
  23. template<class CharT, class Traits, class Range>
  24. virtual void print(std::basic_ostream<CharT, Traits>& os, const Range& r) const = 0;
  25. };
  26. ``
  27. Unfortunately, this is illegal because a virtual function
  28. cannot be a template. However, we can define a
  29. class with much the same behavior using Boost.TypeErasure.
  30. */
  31. #include <boost/type_erasure/any.hpp>
  32. #include <boost/type_erasure/iterator.hpp>
  33. #include <boost/type_erasure/operators.hpp>
  34. #include <boost/type_erasure/tuple.hpp>
  35. #include <boost/type_erasure/same_type.hpp>
  36. #include <boost/range/begin.hpp>
  37. #include <boost/range/end.hpp>
  38. #include <boost/range/iterator.hpp>
  39. #include <iostream>
  40. #include <iomanip>
  41. #include <vector>
  42. using namespace boost::type_erasure;
  43. struct _t : placeholder {};
  44. struct _iter : placeholder {};
  45. struct _os : placeholder {};
  46. template<class T, class U = _self>
  47. struct base_and_derived
  48. {
  49. static T& apply(U& arg) { return arg; }
  50. };
  51. namespace boost {
  52. namespace type_erasure {
  53. template<class T, class U, class Base>
  54. struct concept_interface<base_and_derived<T, U>, Base, U> : Base
  55. {
  56. operator typename rebind_any<Base, const T&>::type() const
  57. {
  58. return call(base_and_derived<T, U>(), const_cast<concept_interface&>(*this));
  59. }
  60. operator typename rebind_any<Base, T&>::type()
  61. {
  62. return call(base_and_derived<T, U>(), *this);
  63. }
  64. };
  65. }
  66. }
  67. // abstract_printer - An abstract base class for formatting sequences.
  68. class abstract_printer {
  69. public:
  70. // print - write a sequence to a std::ostream in a manner
  71. // specific to the derived class.
  72. //
  73. // Requires: Range must be a Forward Range whose elements can be
  74. // printed to os.
  75. template<class CharT, class Traits, class Range>
  76. void print(std::basic_ostream<CharT, Traits>& os, const Range& r) const {
  77. // Capture the arguments
  78. typename boost::range_iterator<const Range>::type
  79. first(boost::begin(r)),
  80. last(boost::end(r));
  81. tuple<requirements, _os&, _iter, _iter> args(os, first, last);
  82. // and forward to the real implementation
  83. do_print(get<0>(args), get<1>(args), get<2>(args));
  84. }
  85. virtual ~abstract_printer() {}
  86. protected:
  87. // define the concept requirements of the arguments of
  88. // print and typedef the any types.
  89. typedef boost::mpl::vector<
  90. base_and_derived<std::ios_base, _os>,
  91. ostreamable<_os, _t>,
  92. ostreamable<_os, const char*>,
  93. forward_iterator<_iter, const _t&>,
  94. same_type<_t, forward_iterator<_iter, const _t&>::value_type>
  95. > requirements;
  96. typedef boost::type_erasure::any<requirements, _os&> ostream_type;
  97. typedef boost::type_erasure::any<requirements, _iter> iterator_type;
  98. // do_print - This method must be implemented by derived classes
  99. virtual void do_print(
  100. ostream_type os, iterator_type first, iterator_type last) const = 0;
  101. };
  102. // separator_printer - writes the elements of a sequence
  103. // separated by a fixed string. For example, if
  104. // the separator is ", " separator_printer produces
  105. // a comma separated list.
  106. class separator_printer : public abstract_printer {
  107. public:
  108. explicit separator_printer(const std::string& sep) : separator(sep) {}
  109. protected:
  110. virtual void do_print(
  111. ostream_type os, iterator_type first, iterator_type last) const {
  112. if(first != last) {
  113. os << *first;
  114. ++first;
  115. for(; first != last; ++first) {
  116. os << separator.c_str() << *first;
  117. }
  118. }
  119. }
  120. private:
  121. std::string separator;
  122. };
  123. // column_separator_printer - like separator_printer, but
  124. // also inserts a line break after every n elements.
  125. class column_separator_printer : public abstract_printer {
  126. public:
  127. column_separator_printer(const std::string& sep, std::size_t num_columns)
  128. : separator(sep),
  129. cols(num_columns)
  130. {}
  131. protected:
  132. virtual void do_print(
  133. ostream_type os, iterator_type first, iterator_type last) const {
  134. std::size_t count = 0;
  135. for(; first != last; ++first) {
  136. os << *first;
  137. boost::type_erasure::any<requirements, _iter> temp = first;
  138. ++temp;
  139. if(temp != last) {
  140. os << separator.c_str();
  141. }
  142. if(++count % cols == 0) {
  143. os << "\n";
  144. }
  145. }
  146. }
  147. private:
  148. std::string separator;
  149. std::size_t cols;
  150. };
  151. // aligned_column_printer - formats a sequence in columns
  152. // reading down. For example, given the sequence
  153. // { 1, 2, 3, 4, 5 }, aligned_column_printer might print
  154. // 1 4
  155. // 2 5
  156. // 3
  157. class aligned_column_printer : public abstract_printer {
  158. public:
  159. aligned_column_printer(std::size_t column_width, std::size_t num_columns)
  160. : width(column_width),
  161. cols(num_columns)
  162. {}
  163. protected:
  164. virtual void do_print(
  165. ostream_type os, iterator_type first, iterator_type last) const
  166. {
  167. if(first == last) return;
  168. std::vector<iterator_type> column_iterators;
  169. // find the tops of the columns
  170. std::size_t count = 0;
  171. for(iterator_type iter = first; iter != last; ++iter) {
  172. ++count;
  173. }
  174. std::size_t rows = (count + cols - 1) / cols;
  175. count = 0;
  176. for(iterator_type iter = first; iter != last; ++iter) {
  177. if(count % rows == 0) {
  178. column_iterators.push_back(iter);
  179. }
  180. ++count;
  181. }
  182. iterator_type last_col = column_iterators.back();
  183. // print the full rows
  184. while(column_iterators.back() != last) {
  185. for(std::vector<iterator_type>::iterator
  186. iter = column_iterators.begin(),
  187. end = column_iterators.end(); iter != end; ++iter)
  188. {
  189. static_cast<std::ios_base&>(os).width(width);
  190. os << **iter;
  191. ++*iter;
  192. }
  193. os << "\n";
  194. }
  195. // print the rows that are missing the last column
  196. column_iterators.pop_back();
  197. if(!column_iterators.empty()) {
  198. while(column_iterators.back() != last_col) {
  199. for(std::vector<iterator_type>::iterator
  200. iter = column_iterators.begin(),
  201. end = column_iterators.end(); iter != end; ++iter)
  202. {
  203. static_cast<std::ios_base&>(os).width(width);
  204. os << **iter;
  205. ++*iter;
  206. }
  207. os << "\n";
  208. }
  209. }
  210. }
  211. private:
  212. std::size_t width;
  213. std::size_t cols;
  214. };
  215. int main() {
  216. int test[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  217. separator_printer p1(",");
  218. p1.print(std::cout, test);
  219. std::cout << std::endl;
  220. column_separator_printer p2(",", 4);
  221. p2.print(std::cout, test);
  222. std::cout << std::endl;
  223. aligned_column_printer p3(16, 4);
  224. p3.print(std::cout, test);
  225. }
  226. //]