stlab.adobe.com Adobe Systems Incorporated
manip.hpp
Go to the documentation of this file.
1 /*
2  Copyright 2005-2007 Adobe Systems Incorporated
3  Distributed under the MIT License (see accompanying file LICENSE_1_0_0.txt
4  or a copy at http://stlab.adobe.com/licenses.html)
5 */
6 
7 /*************************************************************************************************/
8 
9 #ifdef ADOBE_STD_SERIALIZATION
10 
11 /*************************************************************************************************/
12 
13 #ifndef ADOBE_MANIP_HPP
14 #define ADOBE_MANIP_HPP
15 
16 /*************************************************************************************************/
17 
18 #include <adobe/config.hpp>
19 
20 #include <iostream>
21 
22 /*************************************************************************************************/
23 
24 namespace adobe {
25 
26 
27 /*************************************************************************************************/
28 
35 {
36 public:
38  error_m(std::ios_base::goodbit)
39  { }
40 
41 protected:
42 
43  template <typename StreamType>
44  std::ios_base::iostate handle_error(StreamType& strm) const
45  {
46  std::ios_base::iostate err(error_m);
47 
48  try { throw; }
49 
50  catch (std::bad_alloc&)
51  {
52  set_bad();
53 
54  std::ios_base::iostate exception_mask(strm.exceptions());
55 
56  if (exception_mask & std::ios_base::failbit && !(exception_mask & std::ios_base::badbit))
57  strm.setstate(err);
58  else if (exception_mask & std::ios_base::badbit)
59  {
60  try { strm.setstate(err); }
61  catch (std::ios_base::failure&) { }
62  throw;
63  }
64  }
65  catch (...)
66  {
67  set_fail();
68 
69  std::ios_base::iostate exception_mask(strm.exceptions());
70 
71  if ((exception_mask & std::ios_base::badbit) && (err & std::ios_base::badbit))
72  strm.setstate(err);
73  else if (exception_mask & std::ios_base::failbit)
74  {
75  try { strm.setstate(err); }
76  catch (std::ios_base::failure&) { }
77  throw;
78  }
79  }
80 
81  return err;
82  }
83 
84  void set_fail() const { error_m |= std::ios_base::failbit; }
85  void set_bad() const { error_m |= std::ios_base::badbit; }
86 
87  mutable std::ios_base::iostate error_m;
88 };
89 
90 /*************************************************************************************************/
91 
92 template <typename ArgumentType, class charT, class traits>
94 {
95 public:
96  typedef ArgumentType argument_type;
97  typedef std::basic_ostream<charT, traits> stream_type;
98  typedef stream_type& (*manip_func)(stream_type&, const ArgumentType&);
99 
101  pf_m(pf), arg_m(arg)
102  { }
103 
104  void do_manip(stream_type& strm) const
105  {
106  if (error_m != std::ios_base::goodbit)
107  strm.setstate(error_m);
108  else
109  {
110  std::ios_base::iostate err(error_m);
111  try
112  {
113  (*pf_m)(strm, arg_m);
114  }
115  catch (...)
116  {
117  err = handle_error(strm);
118  }
119 
120  if (err) strm.setstate(err);
121  }
122  }
123 
124 private:
125  manip_func pf_m;
126 
127 protected:
129 };
130 
131 /*************************************************************************************************/
132 
133 template <typename ArgumentType1, typename ArgumentType2, class charT, class traits>
135 {
136 public:
137  typedef ArgumentType1 argument_type_1;
138  typedef ArgumentType2 argument_type_2;
139  typedef std::basic_ostream<charT, traits> stream_type;
140  typedef stream_type& (*manip_func)(stream_type&, const ArgumentType1&, const ArgumentType2&);
141 
142  basic_omanipulator2(manip_func pf, const ArgumentType1& arg1, const ArgumentType2& arg2) :
143  pf_m(pf), arg1_m(arg1) , arg2_m(arg2)
144  { }
145 
146  void do_manip(stream_type& strm) const
147  {
148  if (error_m != std::ios_base::goodbit)
149  strm.setstate(error_m);
150  else
151  {
152  std::ios_base::iostate err(error_m);
153  try
154  {
155  (*pf_m)(strm, arg1_m, arg2_m);
156  }
157  catch (...)
158  {
159  err = handle_error(strm);
160  }
161 
162  if (err) strm.setstate(err);
163  }
164  }
165 
166 private:
167  manip_func pf_m;
168 
169 protected:
172 };
173 
174 /*************************************************************************************************/
175 
176 template <class ArgumentType, class charT, class traits>
177 std::basic_ostream<charT, traits>& operator << (std::basic_ostream<charT, traits>& os,
179  {
180  if (os.good())
181  manip.do_manip(os);
182 
183  return os;
184  }
185 
186 /*************************************************************************************************/
187 
188 template <class ArgumentType1, class ArgumentType2, class charT, class traits>
189 std::basic_ostream<charT, traits>& operator << (std::basic_ostream<charT, traits>& os,
191  {
192  if (os.good())
193  manip.do_manip(os);
194 
195  return os;
196  }
197 
198 /*************************************************************************************************/
199 
200 template <class charT, class traits>
201 class basic_bounded_width : public basic_omanipulator<unsigned int, charT, traits>
202 {
204 
205 public:
208 
210  basic_omanipulator<argument_type, charT, traits>(basic_bounded_width::fct, min),
211  min_m(min), max_m(max)
212  { }
213 
215  {
216  inherited_t::arg_m = std::min(max_m, std::max(i, min_m));
217  return *this;
218  }
219 
220 private:
221  static stream_type& fct(stream_type& strm, const argument_type& i)
222  {
223  strm.width(i);
224  return strm;
225  }
226 
227  argument_type min_m;
228  argument_type max_m;
229 };
230 
233 
235 
236 /*************************************************************************************************/
237 
238 } // namespace adobe
239 
240 /*************************************************************************************************/
241 
242 #endif
243 
244 /*************************************************************************************************/
245 
246 #endif
247 
248 /*************************************************************************************************/

Copyright © 2006-2007 Adobe Systems Incorporated.

Use of this website signifies your agreement to the Terms of Use and Online Privacy Policy.

Search powered by Google