stlab.adobe.com Adobe Systems Incorporated
iomanip.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_IOMANIP_HPP
14 #define ADOBE_IOMANIP_HPP
15 
16 /*************************************************************************************************/
17 
18 #include <adobe/config.hpp>
19 
20 #include <cassert>
21 #include <iosfwd>
22 #include <string>
23 #include <sstream>
24 #include <list>
25 #include <stdexcept>
26 #include <functional>
27 
28 #include <adobe/any_regular.hpp>
29 #include <adobe/iomanip_fwd.hpp>
30 #include <adobe/manip.hpp>
31 #include <adobe/name.hpp>
32 
33 /*************************************************************************************************/
34 
35 namespace adobe {
36 
37 /*************************************************************************************************/
38 
39 namespace implementation {
40 
41 /*************************************************************************************************/
42 
43 } // namespace implementation
44 
45 /*************************************************************************************************/
46 
47 static const aggregate_name_t bag_name_g = { "bag" };
48 static const aggregate_name_t seq_name_g = { "seq" };
49 static const aggregate_name_t alt_name_g = { "alt" };
50 static const aggregate_name_t atom_name_g = { "atom" };
51 
52 /*************************************************************************************************/
53 
59 class format_element_t
61 {
62 public:
63  explicit format_element_t(name_t tag = name_t(),
64  const std::string& ident = std::string()) :
65  ident_m(ident),
66  num_out_m(0),
67  tag_m(tag),
68  value_m(0)
69  { }
70 
71  format_element_t(name_t tag, const any_regular_t& value) :
72  num_out_m(0),
73  tag_m(tag),
74  value_m(&value)
75  { }
76 
77  name_t tag() const
78  { return tag_m; }
79 
80  const any_regular_t& value() const
81  {
82  if (!value_m)
83  throw std::runtime_error("invalid value");
84 
85  return *value_m;
86  }
87 
88  std::string ident_m;
89  std::size_t num_out_m;
90 
91 private:
92  name_t tag_m;
93  const any_regular_t* value_m;
94 };
95 
96 /*************************************************************************************************/
97 
100 {
101 public:
102  typedef std::ostream stream_type;
104  typedef std::list<stack_value_type> stack_type;
105 
106  virtual ~format_base()
107  { }
108 
109  virtual void begin_format(stream_type& os) { push_stack(os); }
110  virtual void end_format(stream_type& os) { pop_stack(os); }
111 
112  virtual void begin_bag(stream_type& os, const std::string&) { push_stack(os); }
113  virtual void end_bag(stream_type& os) { pop_stack(os); }
114 
115  virtual void begin_sequence(stream_type& os) { push_stack(os); }
116  virtual void end_sequence(stream_type& os) { pop_stack(os); }
117 
118  virtual void begin_alternate(stream_type& os) { push_stack(os); }
119  virtual void end_alternate(stream_type& os) { pop_stack(os); }
120 
121  virtual void begin_atom(stream_type& os, const any_regular_t&) { push_stack(os); }
122  virtual void end_atom(stream_type& os) { pop_stack(os); }
123 
125  depth_m(0)
126  { }
127 
128  virtual std::size_t depth()
129  { return depth_m; }
130 
131  virtual std::size_t stack_depth()
132  { return stack_m.size(); }
133 
134 protected:
135 
136  void push_stack(stream_type& os,
137  const stack_value_type& element = format_element_t())
138  {
139  stack_m.push_front(element);
140  stack_event(os, true);
141  }
142 
143  void pop_stack(stream_type& os)
144  {
145  assert(stack_m.empty() == false);
146  stack_event(os, false);
147  stack_m.pop_front();
148  }
149 
150  const stack_value_type& stack_top() const
151  { return stack_n(0); }
152 
153  stack_value_type& stack_top()
154  { return stack_n(0); }
155 
156  const stack_value_type& stack_n(std::size_t n) const
157  {
158  if (n > stack_m.size())
159  {
160  std::stringstream buf;
161  buf << "stack_n: n(" << static_cast<unsigned int>(n) << ") > size(" << static_cast<unsigned int>(stack_m.size()) << ").";
162  throw std::range_error(buf.str());
163  }
164 
165  return *boost::next(stack_m.begin(), n);
166  }
167 
168  stack_value_type& stack_n(std::size_t n)
169  {
170  if (n > stack_m.size())
171  {
172  std::stringstream buf;
173  buf << "stack_n: n(" << static_cast<unsigned int>(n) << ") > size(" << static_cast<unsigned int>(stack_m.size()) << ").";
174  throw std::range_error(buf.str());
175  }
176 
177  return *boost::next(stack_m.begin(), n);
178  }
179 
180  void up()
181  { ++depth_m; }
182 
183  void down()
184  { std::size_t temp = (std::max)(std::size_t(0), --depth_m);
185  depth_m = temp; // use temp to suppress gcc-4.5.0 warning/error
186  }
187 
188 private:
189  virtual void stack_event(stream_type& os, bool is_push) = 0;
190 
191  std::size_t depth_m; // note: Visual "depth", NOT the depth of the stack
192  stack_type stack_m;
193 };
194 
195 /*************************************************************************************************/
196 
199 
201 
204 
207 
209 
210 /*************************************************************************************************/
211 
213 class indents : public basic_omanipulator<std::size_t, char, std::char_traits<char> >
214 {
216 
217 public:
218  typedef inherited_t::stream_type stream_type;
220 
221  indents(argument_type num) :
222  inherited_t(indents::fct, num)
223  { }
224 
225  inherited_t& operator() (argument_type i)
226  { arg_m = i; return *this; }
227 
228 private:
229  static stream_type& fct(stream_type& os, const argument_type& i)
230  {
231  for (argument_type count(0); count < i; ++count)
232  os.put('\t');
233 
234  return os;
235  }
236 };
237 
238 /*************************************************************************************************/
239 
241 class begin_bag : public basic_omanipulator<std::string, char, std::char_traits<char> >
242 {
244 
245 public:
246  typedef inherited_t::stream_type stream_type;
248 
249  begin_bag(const argument_type& index) :
250  inherited_t(begin_bag::fct, index)
251  { }
252 
253  inherited_t& operator() (argument_type /*i*/)
254  { return *this; }
255 
256 private:
257  static stream_type& fct(stream_type& os, const argument_type& i)
258  {
260  if (format) format->begin_bag(os, i);
261  return os;
262  }
263 };
264 
265 /*************************************************************************************************/
266 
268 template <typename T>
269 class begin_atom : public basic_omanipulator<const any_regular_t, char, std::char_traits<char> >
270 {
272 
273 public:
274  typedef inherited_t::stream_type stream_type;
276 
277  explicit begin_atom(const T& x) :
278  inherited_t(begin_atom::fct, any_regular_t(x))
279  { }
280 
281  inherited_t& operator() (argument_type /*i*/)
282  { return *this; }
283 
284 private:
285  static stream_type& fct(stream_type& os, const argument_type& atom)
286  {
288 
289  if (format == 0)
290  return os << atom.cast<typename promote<T>::type>();
291 
292  format->begin_atom(os, atom);
293 
294  return os;
295  }
296 };
297 
298 /*************************************************************************************************/
299 
301 template <typename T, class charT, class traits>
302 std::basic_ostream<charT, traits>& operator << (std::basic_ostream<charT, traits>& os,
303  const begin_atom<T>& manip)
304 {
305  if (os.good())
306  manip.do_manip(os);
307 
308  return os;
309 }
310 
311 /*************************************************************************************************/
312 
314 template <typename NewFormat>
315 void callback(std::ios_base::event ev, std::ios_base& strm, int idx)
316  {
317  if (ev == std::ios_base::erase_event)
318  {
319  try
320  {
321  delete static_cast<NewFormat*>(strm.pword(idx));
322 
323  strm.pword(idx) = 0;
324  }
325  catch (...)
326  { }
327  }
328  else if (ev == std::ios_base::copyfmt_event)
329  {
330  NewFormat* old(static_cast<NewFormat*>(strm.pword(idx)));
331 
332  if (old != 0)
333  {
334  try
335  {
336  strm.pword(idx) = new NewFormat(*old);
337  }
338  catch (std::bad_alloc&)
339  { }
340  }
341  }
342  }
343 
344 /*************************************************************************************************/
345 
347 template <typename OldFormat, typename NewFormat>
348 void replace_pword(std::ios_base& iob, int idx)
349  {
350  iob.register_callback(callback<NewFormat>, idx);
351 
352  NewFormat* new_format(new NewFormat());
353  OldFormat* old_format(static_cast<OldFormat*>(iob.pword(idx)));
354  iob.pword(idx) = new_format;
355  delete old_format;
356  }
357 
358 /*************************************************************************************************/
359 
361 template <typename OldFormat, typename NewFormat, typename T>
362 void replace_pword(std::ios_base& iob, int idx, const T& x)
363  {
364  iob.register_callback(callback<NewFormat>, idx);
365 
366  NewFormat* new_format(new NewFormat(x));
367  OldFormat* old_format(static_cast<OldFormat*>(iob.pword(idx)));
368  iob.pword(idx) = new_format;
369  delete old_format;
370  }
371 
372 /*************************************************************************************************/
373 
375 template <class T>
376 std::ostream& fmt(std::ostream& os, const T& t)
377 {
378  os << begin_atom<T>(t) << end_atom;
379 
380  return os;
381 }
382 
383 /*************************************************************************************************/
384 
386 template <class T>
387 class basic_format : public basic_omanipulator<T, char, std::char_traits<char> >
388 {
390 
391 public:
392  typedef typename inherited_t::stream_type stream_type;
394 
395  basic_format(const argument_type& t) :
396  inherited_t(fmt<T>, t)
397  { }
398 
399  inherited_t& operator() (const argument_type& /*i*/)
400  { return *this; }
401 };
402 
403 /*************************************************************************************************/
404 
406 template <class T>
407 inline basic_format<T> format(const T& t)
408 {
409  return basic_format<T>(t);
410 }
411 
412 /*************************************************************************************************/
413 
414 } // namespace adobe
415 
416 /*************************************************************************************************/
417 
418 #endif
419 
420 /*************************************************************************************************/
421 
422 #endif
423 
424 /*************************************************************************************************/
const stack_value_type & stack_top() const
Definition: iomanip.hpp:150
inherited_t::stream_type stream_type
Definition: iomanip.hpp:392
std::list< stack_value_type > stack_type
Definition: iomanip.hpp:104
inherited_t::argument_type argument_type
Definition: iomanip.hpp:393
virtual std::size_t stack_depth()
Definition: iomanip.hpp:131
std::ostream stream_type
Definition: iomanip.hpp:102
begin_bag(const argument_type &index)
Definition: iomanip.hpp:249
void pop_stack(stream_type &os)
Definition: iomanip.hpp:143
inherited_t::argument_type argument_type
Definition: iomanip.hpp:247
format_base::stream_type & begin_alternate(format_base::stream_type &os)
basic_format< T > format(const T &t)
Definition: iomanip.hpp:407
stack_value_type & stack_top()
Definition: iomanip.hpp:153
stack_value_type & stack_n(std::size_t n)
Definition: iomanip.hpp:168
begin_atom(const T &x)
Definition: iomanip.hpp:277
virtual void end_format(stream_type &os)
Definition: iomanip.hpp:110
void push_stack(stream_type &os, const stack_value_type &element=format_element_t())
Definition: iomanip.hpp:136
inherited_t::stream_type stream_type
Definition: iomanip.hpp:274
void do_manip(stream_type &strm) const
Definition: manip.hpp:104
void callback(std::ios_base::event ev, std::ios_base &strm, int idx)
Definition: iomanip.hpp:315
const stack_value_type & stack_n(std::size_t n) const
Definition: iomanip.hpp:156
virtual std::size_t depth()
Definition: iomanip.hpp:128
inherited_t::argument_type argument_type
Definition: iomanip.hpp:275
virtual void begin_bag(stream_type &os, const std::string &)
Definition: iomanip.hpp:112
format_base::stream_type & end_alternate(format_base::stream_type &os)
static const aggregate_name_t seq_name_g
Definition: iomanip.hpp:48
virtual void end_bag(stream_type &os)
Definition: iomanip.hpp:113
format_base::stream_type & end_sequence(format_base::stream_type &os)
format_base::stream_type & end_bag(format_base::stream_type &os)
format_base * get_formatter(std::ostream &os)
format_base::stream_type & end_atom(format_base::stream_type &os)
const T &() max(const T &a, const T &b)
minmax implementation
Definition: minmax.hpp:86
format_base::stream_type & begin_sequence(format_base::stream_type &os)
virtual ~format_base()
Definition: iomanip.hpp:106
void replace_pword(std::ios_base &iob, int idx)
Definition: iomanip.hpp:348
static const aggregate_name_t alt_name_g
Definition: iomanip.hpp:49
boost::range_difference< InputRange >::type count(InputRange &range, T &value)
count implementation
Definition: count.hpp:41
virtual void end_atom(stream_type &os)
Definition: iomanip.hpp:122
indents(argument_type num)
Definition: iomanip.hpp:221
virtual void begin_atom(stream_type &os, const any_regular_t &)
Definition: iomanip.hpp:121
inherited_t::stream_type stream_type
Definition: iomanip.hpp:218
name_t tag() const
Definition: iomanip.hpp:77
basic_format(const argument_type &t)
Definition: iomanip.hpp:395
static const aggregate_name_t atom_name_g
Definition: iomanip.hpp:50
format_base::stream_type & begin_format(format_base::stream_type &os)
virtual void end_sequence(stream_type &os)
Definition: iomanip.hpp:116
virtual void begin_alternate(stream_type &os)
Definition: iomanip.hpp:118
virtual void begin_format(stream_type &os)
Definition: iomanip.hpp:109
inherited_t::argument_type argument_type
Definition: iomanip.hpp:219
format_element_t stack_value_type
Definition: iomanip.hpp:103
virtual void begin_sequence(stream_type &os)
Definition: iomanip.hpp:115
std::size_t num_out_m
Definition: iomanip.hpp:89
const any_regular_t & value() const
Definition: iomanip.hpp:80
virtual void end_alternate(stream_type &os)
Definition: iomanip.hpp:119
format_element_t(name_t tag=name_t(), const std::string &ident=std::string())
Definition: iomanip.hpp:63
std::ostream & fmt(std::ostream &os, const T &t)
Definition: iomanip.hpp:376
A runtime polymorphic type similar to boost::any which can hold any type which models Regular...
format_base::stream_type & end_format(format_base::stream_type &os)
static const aggregate_name_t bag_name_g
Definition: iomanip.hpp:47
format_element_t(name_t tag, const any_regular_t &value)
Definition: iomanip.hpp:71
inherited_t::stream_type stream_type
Definition: iomanip.hpp:246
std::string ident_m
Definition: iomanip.hpp:88

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