range.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright Andrey Semashev 2020.
  3. * Distributed under the Boost Software License, Version 1.0.
  4. * (See accompanying file LICENSE_1_0.txt or copy at
  5. * https://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. /*!
  8. * \file utility/manipulators/range.hpp
  9. * \author Andrey Semashev
  10. * \date 11.05.2020
  11. *
  12. * The header contains implementation of a stream manipulator for inserting a range of elements, optionally separated with a delimiter.
  13. */
  14. #ifndef BOOST_LOG_UTILITY_MANIPULATORS_RANGE_HPP_INCLUDED_
  15. #define BOOST_LOG_UTILITY_MANIPULATORS_RANGE_HPP_INCLUDED_
  16. #include <cstddef>
  17. #include <boost/range/begin.hpp>
  18. #include <boost/range/end.hpp>
  19. #include <boost/range/const_iterator.hpp>
  20. #include <boost/core/enable_if.hpp>
  21. #include <boost/type_traits/is_scalar.hpp>
  22. #include <boost/type_traits/conditional.hpp>
  23. #include <boost/log/detail/config.hpp>
  24. #include <boost/log/detail/is_ostream.hpp>
  25. #include <boost/log/detail/header.hpp>
  26. #ifdef BOOST_HAS_PRAGMA_ONCE
  27. #pragma once
  28. #endif
  29. namespace boost {
  30. BOOST_LOG_OPEN_NAMESPACE
  31. /*!
  32. * Stream manipulator for inserting a range of elements, optionally separated with a delimiter.
  33. */
  34. template< typename RangeT, typename DelimiterT >
  35. class range_manipulator
  36. {
  37. private:
  38. typedef typename conditional<
  39. is_scalar< DelimiterT >::value,
  40. DelimiterT,
  41. DelimiterT const&
  42. >::type stored_delimiter_type;
  43. private:
  44. RangeT const& m_range;
  45. stored_delimiter_type m_delimiter;
  46. public:
  47. //! Initializing constructor
  48. range_manipulator(RangeT const& range, stored_delimiter_type delimiter) BOOST_NOEXCEPT :
  49. m_range(range),
  50. m_delimiter(delimiter)
  51. {
  52. }
  53. //! The method outputs elements of the range separated with delimiter
  54. template< typename StreamT >
  55. void output(StreamT& stream) const
  56. {
  57. typedef typename boost::range_const_iterator< RangeT >::type const_iterator;
  58. const_iterator it = boost::begin(m_range);
  59. const const_iterator end = boost::end(m_range);
  60. if (BOOST_LIKELY(it != end))
  61. {
  62. stream << *it;
  63. for (++it; it != end; ++it)
  64. {
  65. stream << m_delimiter;
  66. stream << *it;
  67. }
  68. }
  69. }
  70. };
  71. /*!
  72. * Stream manipulator for inserting a range of elements. Specialization for when there is no delimiter.
  73. */
  74. template< typename RangeT >
  75. class range_manipulator< RangeT, void >
  76. {
  77. private:
  78. RangeT const& m_range;
  79. public:
  80. //! Initializing constructor
  81. explicit range_manipulator(RangeT const& range) BOOST_NOEXCEPT :
  82. m_range(range)
  83. {
  84. }
  85. //! The method outputs elements of the range
  86. template< typename StreamT >
  87. void output(StreamT& stream) const
  88. {
  89. typedef typename boost::range_const_iterator< RangeT >::type const_iterator;
  90. const_iterator it = boost::begin(m_range);
  91. const const_iterator end = boost::end(m_range);
  92. for (; it != end; ++it)
  93. {
  94. stream << *it;
  95. }
  96. }
  97. };
  98. /*!
  99. * Stream output operator for \c range_manipulator. Outputs every element of the range, separated with a delimiter, if one was specified on manipulator construction.
  100. */
  101. template< typename StreamT, typename RangeT, typename DelimiterT >
  102. inline typename boost::enable_if_c< log::aux::is_ostream< StreamT >::value, StreamT& >::type operator<< (StreamT& strm, range_manipulator< RangeT, DelimiterT > const& manip)
  103. {
  104. if (BOOST_LIKELY(strm.good()))
  105. manip.output(strm);
  106. return strm;
  107. }
  108. /*!
  109. * Range manipulator generator function.
  110. *
  111. * \param range Range of elements to output. The range must support begin and end iterators, and its elements must support stream output.
  112. * \param delimiter Delimiter to separate elements in the output. Optional. If not specified, elements are output without separation.
  113. * \returns Manipulator to be inserted into the stream.
  114. *
  115. * \note Both \a range and \a delimiter objects must outlive the created manipulator object.
  116. */
  117. template< typename RangeT, typename DelimiterT >
  118. inline typename boost::enable_if_c<
  119. is_scalar< DelimiterT >::value,
  120. range_manipulator< RangeT, DelimiterT >
  121. >::type range_manip(RangeT const& range, DelimiterT delimiter) BOOST_NOEXCEPT
  122. {
  123. return range_manipulator< RangeT, DelimiterT >(range, delimiter);
  124. }
  125. /*!
  126. * Range manipulator generator function.
  127. *
  128. * \param range Range of elements to output. The range must support begin and end iterators, and its elements must support stream output.
  129. * \param delimiter Delimiter to separate elements in the output. Optional. If not specified, elements are output without separation.
  130. * \returns Manipulator to be inserted into the stream.
  131. *
  132. * \note Both \a range and \a delimiter objects must outlive the created manipulator object.
  133. */
  134. template< typename RangeT, typename DelimiterT >
  135. inline typename boost::disable_if_c<
  136. is_scalar< DelimiterT >::value,
  137. range_manipulator< RangeT, DelimiterT >
  138. >::type range_manip(RangeT const& range, DelimiterT const& delimiter) BOOST_NOEXCEPT
  139. {
  140. return range_manipulator< RangeT, DelimiterT >(range, delimiter);
  141. }
  142. /*!
  143. * Range manipulator generator function.
  144. *
  145. * \param range Range of elements to output. The range must support begin and end iterators, and its elements must support stream output.
  146. * \param delimiter Delimiter to separate elements in the output. Optional. If not specified, elements are output without separation.
  147. * \returns Manipulator to be inserted into the stream.
  148. *
  149. * \note Both \a range and \a delimiter objects must outlive the created manipulator object.
  150. */
  151. template< typename RangeT, typename DelimiterElementT, std::size_t N >
  152. inline range_manipulator< RangeT, DelimiterElementT* > range_manip(RangeT const& range, DelimiterElementT (&delimiter)[N]) BOOST_NOEXCEPT
  153. {
  154. return range_manipulator< RangeT, DelimiterElementT* >(range, delimiter);
  155. }
  156. /*!
  157. * Range manipulator generator function.
  158. *
  159. * \param range Range of elements to output. The range must support begin and end iterators, and its elements must support stream output.
  160. * \returns Manipulator to be inserted into the stream.
  161. *
  162. * \note \a delimiter object must outlive the created manipulator object.
  163. */
  164. template< typename RangeT >
  165. inline range_manipulator< RangeT, void > range_manip(RangeT const& range) BOOST_NOEXCEPT
  166. {
  167. return range_manipulator< RangeT, void >(range);
  168. }
  169. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  170. } // namespace boost
  171. #include <boost/log/detail/footer.hpp>
  172. #endif // BOOST_LOG_UTILITY_MANIPULATORS_RANGE_HPP_INCLUDED_