manip_base.hpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // boost/chrono/utility/manip_base.hpp ------------------------------------------------------------//
  2. // Copyright 2011 Vicente J. Botet Escriba
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. // See http://www.boost.org/libs/chrono for documentation.
  6. #ifndef BOOST_CHRONO_UTILITY_MANIP_BASE_PTR_HPP
  7. #define BOOST_CHRONO_UTILITY_MANIP_BASE_PTR_HPP
  8. #include <ios>
  9. /**
  10. *
  11. */
  12. namespace boost
  13. {
  14. namespace chrono
  15. {
  16. /**
  17. * manip is a manipulator mixin class following the CRTP.
  18. * @tparam Final the derived from manip and final type
  19. *
  20. * @Example
  21. * @code
  22. class mendl: public manip<mendl>
  23. {
  24. public:
  25. explicit mendl(size_t how_many) :
  26. count(how_many) {}
  27. template <typename out_stream>
  28. void operator()(out_stream &out) const
  29. {
  30. for (size_t line = 0; line < count; ++line)
  31. {
  32. out.put(out.widen('\n'));
  33. }
  34. out.flush();
  35. }
  36. private:
  37. size_t count;
  38. };
  39. * @codeend
  40. */
  41. template <typename Final>
  42. class manip
  43. {
  44. public:
  45. /**
  46. *
  47. * @param ios the io stream or ios_base.
  48. * @Effects calls to the manipulator final functor.
  49. */
  50. //template <typename out_stream>
  51. void operator()(std::ios_base &ios) const
  52. {
  53. (*static_cast<const Final *> (this))(ios);
  54. }
  55. };
  56. /**
  57. * @c manip stream inserter
  58. * @param out the io stream or ios_base.
  59. * @param op the manipulator instance.
  60. * @Effects if @c out is good calls to the manipulator functor @op.
  61. * @return @c out
  62. */
  63. template <typename out_stream, typename manip_type>
  64. out_stream &operator<<(out_stream &out, const manip<manip_type> &op)
  65. {
  66. if (out.good())
  67. op(out);
  68. return out;
  69. }
  70. /**
  71. * @c manip stream extractor
  72. * @param in the io stream or ios_base.
  73. * @param op the manipulator instance.
  74. * @Effects if @c in is good calls to the manipulator functor @op.
  75. * @return @c in
  76. */
  77. template <typename in_stream, typename manip_type>
  78. in_stream &operator>>(in_stream &in, const manip<manip_type> &op)
  79. {
  80. if (in.good())
  81. op(in);
  82. return in;
  83. }
  84. } // namespace chrono
  85. } // namespace boost
  86. #endif // header