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