formatted.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // Boost.Range library
  2. //
  3. // Copyright Neil Groves 2014.
  4. // Use, modification and distribution is subject to the Boost Software
  5. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // For more information, see http://www.boost.org/libs/range/
  9. //
  10. #ifndef BOOST_RANGE_ADAPTOR_FORMATTED_HPP_INCLUDED
  11. #define BOOST_RANGE_ADAPTOR_FORMATTED_HPP_INCLUDED
  12. #include <boost/config.hpp>
  13. #include <boost/range/concepts.hpp>
  14. #include <boost/range/begin.hpp>
  15. #include <boost/range/end.hpp>
  16. #include <boost/range/iterator.hpp>
  17. #include <boost/range/iterator_range_core.hpp>
  18. #include <boost/mpl/if.hpp>
  19. #include <boost/type_traits/is_array.hpp>
  20. #include <boost/type_traits/remove_extent.hpp>
  21. #include <ostream>
  22. namespace boost
  23. {
  24. namespace range_detail
  25. {
  26. template<typename Sep, typename Prefix, typename Postfix>
  27. struct formatted_holder
  28. {
  29. typedef typename boost::mpl::if_<
  30. boost::is_array<Sep>,
  31. const typename boost::remove_extent<Sep>::type*,
  32. Sep
  33. >::type separator_t;
  34. typedef typename boost::mpl::if_<
  35. boost::is_array<Prefix>,
  36. const typename boost::remove_extent<Prefix>::type*,
  37. Prefix
  38. >::type prefix_t;
  39. typedef typename boost::mpl::if_<
  40. boost::is_array<Postfix>,
  41. const typename boost::remove_extent<Postfix>::type*,
  42. Postfix
  43. >::type postfix_t;
  44. formatted_holder(
  45. const separator_t& sep,
  46. const prefix_t& prefix,
  47. const postfix_t& postfix)
  48. : m_sep(sep)
  49. , m_prefix(prefix)
  50. , m_postfix(postfix)
  51. {
  52. }
  53. separator_t m_sep;
  54. prefix_t m_prefix;
  55. postfix_t m_postfix;
  56. };
  57. template<typename Iter, typename Sep, typename Prefix, typename Postfix>
  58. class formatted_range
  59. : public boost::iterator_range<Iter>
  60. {
  61. typedef formatted_holder<Sep,Prefix,Postfix> holder_t;
  62. public:
  63. formatted_range(Iter first, Iter last, const holder_t& holder)
  64. : boost::iterator_range<Iter>(first, last)
  65. , m_holder(holder)
  66. {
  67. }
  68. template<typename OStream>
  69. void write(OStream& out) const
  70. {
  71. Iter it(this->begin());
  72. out << m_holder.m_prefix;
  73. if (it != this->end())
  74. {
  75. out << *it;
  76. for (++it; it != this->end(); ++it)
  77. {
  78. out << m_holder.m_sep << *it;
  79. }
  80. }
  81. out << m_holder.m_postfix;
  82. }
  83. private:
  84. holder_t m_holder;
  85. };
  86. template<
  87. typename SinglePassRange,
  88. typename Sep,
  89. typename Prefix,
  90. typename Postfix
  91. >
  92. inline range_detail::formatted_range<
  93. typename range_iterator<const SinglePassRange>::type, Sep, Prefix, Postfix
  94. >
  95. operator|(
  96. const SinglePassRange& rng,
  97. const range_detail::formatted_holder<Sep,Prefix,Postfix>& holder
  98. )
  99. {
  100. typedef typename range_iterator<const SinglePassRange>::type iterator;
  101. return range_detail::formatted_range<iterator, Sep, Prefix, Postfix>(
  102. boost::begin(rng), boost::end(rng), holder);
  103. }
  104. template<typename Char, typename Traits, typename Iter, typename Sep,
  105. typename Prefix, typename Postfix>
  106. std::basic_ostream<Char, Traits>&
  107. operator<<(
  108. std::basic_ostream<Char, Traits>& out,
  109. const formatted_range<Iter, Sep, Prefix, Postfix>& writer)
  110. {
  111. writer.write(out);
  112. return out;
  113. }
  114. } // namespace range_detail
  115. namespace adaptors
  116. {
  117. template<typename Sep, typename Prefix, typename Postfix>
  118. range_detail::formatted_holder<Sep, Prefix, Postfix>
  119. formatted(const Sep& sep, const Prefix& prefix, const Postfix& postfix)
  120. {
  121. return range_detail::formatted_holder<Sep,Prefix,Postfix>(
  122. sep, prefix, postfix);
  123. }
  124. template<typename Sep, typename Prefix>
  125. range_detail::formatted_holder<Sep, Prefix, char>
  126. formatted(const Sep& sep, const Prefix& prefix)
  127. {
  128. return range_detail::formatted_holder<Sep, Prefix, char>(sep, prefix, '}');
  129. }
  130. template<typename Sep>
  131. range_detail::formatted_holder<Sep, char, char>
  132. formatted(const Sep& sep)
  133. {
  134. return range_detail::formatted_holder<Sep, char, char>(sep, '{', '}');
  135. }
  136. inline range_detail::formatted_holder<char, char, char>
  137. formatted()
  138. {
  139. return range_detail::formatted_holder<char, char, char>(',', '{', '}');
  140. }
  141. using range_detail::formatted_range;
  142. template<typename SinglePassRange, typename Sep, typename Prefix,
  143. typename Postfix>
  144. inline boost::range_detail::formatted_range<
  145. typename boost::range_iterator<const SinglePassRange>::type,
  146. Sep, Prefix, Postfix
  147. >
  148. format(
  149. const SinglePassRange& rng,
  150. const Sep& sep,
  151. const Prefix& prefix,
  152. const Postfix& postfix
  153. )
  154. {
  155. typedef typename boost::range_iterator<const SinglePassRange>::type
  156. iterator_t;
  157. typedef boost::range_detail::formatted_range<
  158. iterator_t, Sep, Prefix, Postfix> result_t;
  159. typedef boost::range_detail::formatted_holder<Sep, Prefix, Postfix>
  160. holder_t;
  161. return result_t(boost::begin(rng), boost::end(rng),
  162. holder_t(sep, prefix, postfix));
  163. }
  164. template<typename SinglePassRange, typename Sep, typename Prefix>
  165. inline boost::range_detail::formatted_range<
  166. typename boost::range_iterator<const SinglePassRange>::type,
  167. Sep, Prefix, char
  168. >
  169. format(
  170. const SinglePassRange& rng,
  171. const Sep& sep,
  172. const Prefix& prefix)
  173. {
  174. return adaptors::format<SinglePassRange, Sep, Prefix, char>(rng, sep, prefix, '}');
  175. }
  176. template<typename SinglePassRange, typename Sep>
  177. inline boost::range_detail::formatted_range<
  178. typename boost::range_iterator<const SinglePassRange>::type,
  179. Sep, char, char
  180. >
  181. format(const SinglePassRange& rng, const Sep& sep)
  182. {
  183. return adaptors::format<SinglePassRange, Sep, char, char>(rng, sep, '{', '}');
  184. }
  185. template<typename SinglePassRange>
  186. inline boost::range_detail::formatted_range<
  187. typename boost::range_iterator<const SinglePassRange>::type,
  188. char, char, char
  189. >
  190. format(const SinglePassRange& rng)
  191. {
  192. return adaptors::format<SinglePassRange, char, char, char>(rng, ',', '{', '}');
  193. }
  194. } // namespace adaptors
  195. namespace range
  196. {
  197. using boost::range_detail::formatted_range;
  198. } // namespace range
  199. } // namespace boost
  200. #endif // include guard