comparison DEPENDENCIES/generic/include/boost/chrono/io/duration_io.hpp @ 16:2665513ce2d3

Add boost headers
author Chris Cannam
date Tue, 05 Aug 2014 11:11:38 +0100
parents
children c530137014c0
comparison
equal deleted inserted replaced
15:663ca0da4350 16:2665513ce2d3
1 // (C) Copyright Howard Hinnant
2 // (C) Copyright 2011 Vicente J. Botet Escriba
3 // Use, modification and distribution are subject to the Boost Software License,
4 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt).
6 //
7 // This code was adapted by Vicente from Howard Hinnant's experimental work
8 // on chrono i/o to Boost
9
10 #ifndef BOOST_CHRONO_IO_DURATION_IO_HPP
11 #define BOOST_CHRONO_IO_DURATION_IO_HPP
12
13 #include <boost/chrono/duration.hpp>
14 #include <boost/ratio/ratio_io.hpp>
15 #include <boost/chrono/io/duration_style.hpp>
16 #include <boost/chrono/io/ios_base_state.hpp>
17 #include <boost/chrono/io/duration_put.hpp>
18 #include <boost/chrono/io/duration_get.hpp>
19 #include <boost/chrono/io/utility/manip_base.hpp>
20 #include <boost/detail/no_exceptions_support.hpp>
21 #include <locale>
22 #include <iostream>
23
24 namespace boost
25 {
26 namespace chrono
27 {
28
29 /**
30 * duration parameterized manipulator.
31 */
32
33 class duration_fmt: public manip<duration_fmt>
34 {
35 duration_style style_;
36 public:
37
38 /**
39 * explicit manipulator constructor from a @c duration_style
40 */
41 explicit duration_fmt(duration_style style)BOOST_NOEXCEPT
42 : style_(style)
43 {}
44
45 /**
46 * Change the duration_style ios state;
47 */
48 void operator()(std::ios_base &ios) const
49
50 {
51 set_duration_style(ios, style_);
52 }
53 };
54
55 /**
56 * duration_style i/o saver.
57 *
58 * See Boost.IO i/o state savers for a motivating compression.
59 */
60 struct duration_style_io_saver
61 {
62
63 //! the type of the state to restore
64 typedef std::ios_base state_type;
65 //! the type of aspect to save
66 typedef duration_style aspect_type;
67
68 /**
69 * Explicit construction from an i/o stream.
70 *
71 * Store a reference to the i/o stream and the value of the associated @c duration_style.
72 */
73 explicit duration_style_io_saver(state_type &s) :
74 s_save_(s)
75 {
76 a_save_ = get_duration_style(s_save_);
77 }
78
79 /**
80 * Construction from an i/o stream and a @c duration_style to restore.
81 *
82 * Stores a reference to the i/o stream and the value @c duration_style to restore given as parameter.
83 */
84 duration_style_io_saver(state_type &s, aspect_type new_value) :
85 s_save_(s), a_save_(new_value)
86 {
87 }
88
89 /**
90 * Destructor.
91 *
92 * Restores the i/o stream with the duration_style to be restored.
93 */
94 ~duration_style_io_saver()
95 {
96 this->restore();
97 }
98
99 /**
100 * Restores the i/o stream with the duration_style to be restored.
101 */
102 void restore()
103 {
104 set_duration_style(s_save_, a_save_);
105 }
106
107 private:
108 duration_style_io_saver& operator=(duration_style_io_saver const& rhs) ;
109
110 state_type& s_save_;
111 aspect_type a_save_;
112 };
113
114 /**
115 * duration stream inserter
116 * @param os the output stream
117 * @param d to value to insert
118 * @return @c os
119 */
120 template <class CharT, class Traits, class Rep, class Period>
121 std::basic_ostream<CharT, Traits>&
122 operator<<(std::basic_ostream<CharT, Traits>& os, const duration<Rep, Period>& d)
123 {
124 bool failed = false;
125 BOOST_TRY
126 {
127 std::ios_base::iostate err = std::ios_base::goodbit;
128 BOOST_TRY
129 {
130 typename std::basic_ostream<CharT, Traits>::sentry opfx(os);
131 if (bool(opfx))
132 {
133 if (!std::has_facet<duration_put<CharT> >(os.getloc()))
134 {
135 if (duration_put<CharT> ().put(os, os, os.fill(), d) .failed())
136 {
137 err = std::ios_base::badbit;
138 }
139 }
140 else if (std::use_facet<duration_put<CharT> >(os.getloc()) .put(os, os, os.fill(), d) .failed())
141 {
142 err = std::ios_base::badbit;
143 }
144 os.width(0);
145 }
146 }
147 BOOST_CATCH(...)
148 {
149 bool flag = false;
150 BOOST_TRY
151 {
152 os.setstate(std::ios_base::failbit);
153 }
154 BOOST_CATCH (std::ios_base::failure )
155 {
156 flag = true;
157 }
158 BOOST_CATCH_END
159 if (flag) throw;
160 }
161 BOOST_CATCH_END
162 if (err) os.setstate(err);
163 return os;
164 }
165 BOOST_CATCH(...)
166 {
167 failed = true;
168 }
169 BOOST_CATCH_END
170 if (failed) os.setstate(std::ios_base::failbit | std::ios_base::badbit);
171 return os;
172 }
173
174 /**
175 *
176 * @param is the input stream
177 * @param d the duration
178 * @return @c is
179 */
180 template <class CharT, class Traits, class Rep, class Period>
181 std::basic_istream<CharT, Traits>&
182 operator>>(std::basic_istream<CharT, Traits>& is, duration<Rep, Period>& d)
183 {
184 std::ios_base::iostate err = std::ios_base::goodbit;
185
186 BOOST_TRY
187 {
188 typename std::basic_istream<CharT, Traits>::sentry ipfx(is);
189 if (bool(ipfx))
190 {
191 if (!std::has_facet<duration_get<CharT> >(is.getloc()))
192 {
193 duration_get<CharT> ().get(is, std::istreambuf_iterator<CharT, Traits>(), is, err, d);
194 }
195 else
196 {
197 std::use_facet<duration_get<CharT> >(is.getloc()) .get(is, std::istreambuf_iterator<CharT, Traits>(), is,
198 err, d);
199 }
200 }
201 }
202 BOOST_CATCH (...)
203 {
204 bool flag = false;
205 BOOST_TRY
206 {
207 is.setstate(std::ios_base::failbit);
208 }
209 BOOST_CATCH (std::ios_base::failure )
210 {
211 flag = true;
212 }
213 BOOST_CATCH_END
214 if (flag) { BOOST_RETHROW }
215 }
216 BOOST_CATCH_END
217 if (err) is.setstate(err);
218 return is;
219 }
220
221 } // chrono
222
223 }
224
225 #endif // header