8#ifdef ADOBE_STD_SERIALIZATION
12#ifndef ADOBE_IOMANIP_HPP
13#define ADOBE_IOMANIP_HPP
29#include <boost/next_prior.hpp>
43namespace implementation {
64class format_element_t {
66 explicit format_element_t(name_t tag = name_t(),
const std::string& ident = std::string())
67 : ident_m(ident), num_out_m(0), tag_m(tag), value_m(0) {}
69 format_element_t(name_t tag,
const serializable_t& value)
70 : num_out_m(0), tag_m(tag), value_m(&value) {}
72 name_t tag()
const {
return tag_m; }
74 const serializable_t& value()
const {
76 throw std::runtime_error(
"invalid value");
82 std::size_t num_out_m;
86 const serializable_t* value_m;
94 typedef std::ostream stream_type;
95 typedef format_element_t stack_value_type;
96 typedef std::list<stack_value_type> stack_type;
98 virtual ~format_base() {}
100 virtual void begin_format(stream_type& os) { push_stack(os); }
101 virtual void end_format(stream_type& os) { pop_stack(os); }
103 virtual void begin_bag(stream_type& os,
const std::string&) { push_stack(os); }
104 virtual void end_bag(stream_type& os) { pop_stack(os); }
106 virtual void begin_sequence(stream_type& os) { push_stack(os); }
107 virtual void end_sequence(stream_type& os) { pop_stack(os); }
109 virtual void begin_alternate(stream_type& os) { push_stack(os); }
110 virtual void end_alternate(stream_type& os) { pop_stack(os); }
112 virtual void begin_atom(stream_type& os,
const serializable_t&) { push_stack(os); }
113 virtual void end_atom(stream_type& os) { pop_stack(os); }
115 format_base() : depth_m(0) {}
117 virtual std::size_t depth() {
return depth_m; }
119 virtual std::size_t stack_depth() {
return stack_m.size(); }
122 void push_stack(stream_type& os,
const stack_value_type& element = format_element_t()) {
123 stack_m.push_front(element);
124 stack_event(os,
true);
127 void pop_stack(stream_type& os) {
128 assert(stack_m.empty() ==
false);
129 stack_event(os,
false);
133 const stack_value_type& stack_top()
const {
return stack_n(0); }
135 stack_value_type& stack_top() {
return stack_n(0); }
137 const stack_value_type& stack_n(std::size_t n)
const {
138 if (n >= stack_m.size()) {
139 std::stringstream buf;
140 buf <<
"stack_n: n(" <<
static_cast<unsigned int>(n) <<
") > size("
141 <<
static_cast<unsigned int>(stack_m.size()) <<
").";
142 throw std::range_error(buf.str());
145 return *boost::next(stack_m.begin(), n);
148 stack_value_type& stack_n(std::size_t n) {
149 if (n >= stack_m.size()) {
150 std::stringstream buf;
151 buf <<
"stack_n: n(" <<
static_cast<unsigned int>(n) <<
") > size("
152 <<
static_cast<unsigned int>(stack_m.size()) <<
").";
153 throw std::range_error(buf.str());
156 return *boost::next(stack_m.begin(), n);
159 void up() { ++depth_m; }
162 std::size_t temp = (std::max)(std::size_t(0), --depth_m);
167 virtual void stack_event(stream_type& os,
bool is_push) = 0;
175format_base::stream_type& begin_format(format_base::stream_type& os);
176format_base::stream_type& end_format(format_base::stream_type& os);
178format_base::stream_type& end_bag(format_base::stream_type& os);
180format_base::stream_type& begin_sequence(format_base::stream_type& os);
181format_base::stream_type& end_sequence(format_base::stream_type& os);
183format_base::stream_type& begin_alternate(format_base::stream_type& os);
184format_base::stream_type& end_alternate(format_base::stream_type& os);
186format_base::stream_type& end_atom(format_base::stream_type& os);
191class indents :
public basic_omanipulator<std::size_t, char, std::char_traits<char>> {
192 typedef basic_omanipulator<std::size_t, char, std::char_traits<char>> inherited_t;
195 typedef inherited_t::stream_type stream_type;
196 typedef inherited_t::argument_type argument_type;
198 indents(argument_type num) : inherited_t(indents::fct, num) {}
200 inherited_t& operator()(argument_type i) {
206 static stream_type& fct(stream_type& os,
const argument_type& i) {
217class begin_bag :
public basic_omanipulator<std::string, char, std::char_traits<char>> {
218 typedef basic_omanipulator<std::string, char, std::char_traits<char>> inherited_t;
221 typedef inherited_t::stream_type stream_type;
222 typedef inherited_t::argument_type argument_type;
224 begin_bag(
const argument_type& index) : inherited_t(begin_bag::fct, index) {}
226 inherited_t& operator()(argument_type ) {
return *
this; }
229 static stream_type& fct(stream_type& os,
const argument_type& i) {
230 format_base* format(get_formatter(os));
233 format->begin_bag(os, i);
243class begin_atom :
public basic_omanipulator<const serializable_t, char, std::char_traits<char>> {
244 typedef basic_omanipulator<const serializable_t, char, std::char_traits<char>> inherited_t;
247 typedef inherited_t::stream_type stream_type;
248 typedef inherited_t::argument_type argument_type;
250 explicit begin_atom(
const T& x) : inherited_t(begin_atom::fct, serializable_t(x)) {}
252 inherited_t& operator()(argument_type ) {
return *
this; }
255 static stream_type& fct(stream_type& os,
const argument_type& atom) {
256 format_base* format(get_formatter(os));
259 format->begin_atom(os, atom);
270template <
typename T,
class charT,
class traits>
271std::basic_ostream<charT, traits>&
operator<<(std::basic_ostream<charT, traits>& os,
272 const begin_atom<T>& manip) {
282template <
typename NewFormat>
283void callback(std::ios_base::event ev, std::ios_base& strm,
int idx) {
284 if (ev == std::ios_base::erase_event) {
286 delete static_cast<NewFormat*
>(strm.pword(idx));
291 }
else if (ev == std::ios_base::copyfmt_event) {
292 NewFormat* old(
static_cast<NewFormat*
>(strm.pword(idx)));
296 strm.pword(idx) =
new NewFormat(*old);
297 }
catch (std::bad_alloc&) {
306template <
typename OldFormat,
typename NewFormat>
307void replace_pword(std::ios_base& iob,
int idx) {
308 iob.register_callback(callback<NewFormat>, idx);
310 NewFormat* new_format(
new NewFormat());
311 OldFormat* old_format(
static_cast<OldFormat*
>(iob.pword(idx)));
312 iob.pword(idx) = new_format;
319template <
typename OldFormat,
typename NewFormat,
typename T>
320void replace_pword(std::ios_base& iob,
int idx,
const T& x) {
321 iob.register_callback(callback<NewFormat>, idx);
323 NewFormat* new_format(
new NewFormat(x));
324 OldFormat* old_format(
static_cast<OldFormat*
>(iob.pword(idx)));
325 iob.pword(idx) = new_format;
333std::ostream& fmt(std::ostream& os,
const T& t) {
334 os << begin_atom<T>(t) << end_atom;
343class basic_format :
public basic_omanipulator<T, char, std::char_traits<char>> {
344 typedef basic_omanipulator<T, char, std::char_traits<char>> inherited_t;
347 typedef typename inherited_t::stream_type stream_type;
348 typedef typename inherited_t::argument_type argument_type;
350 basic_format(
const argument_type& t) : inherited_t(fmt<T>, t) {}
352 inherited_t& operator()(
const argument_type& ) {
return *
this; }
359inline basic_format<T> format(
const T& t) {
360 return basic_format<T>(t);
boost::range_difference< InputRange >::type count(InputRange &range, T &value)
count implementation
std::ostream & operator<<(std::ostream &s, const extents_t &x)
Utility wrapper to construct name_t with strings of static storage duration.