Adobe Source Libraries 2.0.0
A collection of C++ libraries.
Loading...
Searching...
No Matches
manip.hpp
Go to the documentation of this file.
1/*
2 Copyright 2013 Adobe
3 Distributed under the Boost Software License, Version 1.0.
4 (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5*/
6/**************************************************************************************************/
7
8#ifdef ADOBE_STD_SERIALIZATION
9
10/**************************************************************************************************/
11
12#ifndef ADOBE_MANIP_HPP
13#define ADOBE_MANIP_HPP
14
15/**************************************************************************************************/
16
17#include <adobe/config.hpp>
18
19#include <iostream>
20
21/**************************************************************************************************/
22
23namespace adobe {
24
25/**************************************************************************************************/
26
31
32class manipulator_base {
33public:
34 manipulator_base() : error_m(std::ios_base::goodbit) {}
35
36protected:
37 template <typename StreamType>
38 std::ios_base::iostate handle_error(StreamType& strm) const {
39 std::ios_base::iostate err(error_m);
40
41 try {
42 throw;
43 }
44
45 catch (std::bad_alloc&) {
46 set_bad();
47
48 std::ios_base::iostate exception_mask(strm.exceptions());
49
50 if (exception_mask & std::ios_base::failbit &&
51 !(exception_mask & std::ios_base::badbit))
52 strm.setstate(err);
53 else if (exception_mask & std::ios_base::badbit) {
54 try {
55 strm.setstate(err);
56 } catch (std::ios_base::failure&) {
57 }
58 throw;
59 }
60 } catch (...) {
61 set_fail();
62
63 std::ios_base::iostate exception_mask(strm.exceptions());
64
65 if ((exception_mask & std::ios_base::badbit) && (err & std::ios_base::badbit))
66 strm.setstate(err);
67 else if (exception_mask & std::ios_base::failbit) {
68 try {
69 strm.setstate(err);
70 } catch (std::ios_base::failure&) {
71 }
72 throw;
73 }
74 }
75
76 return err;
77 }
78
79 void set_fail() const { error_m |= std::ios_base::failbit; }
80 void set_bad() const { error_m |= std::ios_base::badbit; }
81
82 mutable std::ios_base::iostate error_m;
83};
84
85/**************************************************************************************************/
86
87template <typename ArgumentType, class charT, class traits>
88class basic_omanipulator : public manipulator_base {
89public:
90 typedef ArgumentType argument_type;
91 typedef std::basic_ostream<charT, traits> stream_type;
92 typedef stream_type& (*manip_func)(stream_type&, const ArgumentType&);
93
94 basic_omanipulator(manip_func pf, const argument_type& arg) : pf_m(pf), arg_m(arg) {}
95
96 void do_manip(stream_type& strm) const {
97 if (error_m != std::ios_base::goodbit)
98 strm.setstate(error_m);
99 else {
100 std::ios_base::iostate err(error_m);
101 try {
102 (*pf_m)(strm, arg_m);
103 } catch (...) {
104 err = handle_error(strm);
105 }
106
107 if (err)
108 strm.setstate(err);
109 }
110 }
111
112private:
113 manip_func pf_m;
114
115protected:
116 argument_type arg_m;
117};
118
119/**************************************************************************************************/
120
121template <typename ArgumentType1, typename ArgumentType2, class charT, class traits>
122class basic_omanipulator2 : public manipulator_base {
123public:
124 typedef ArgumentType1 argument_type_1;
125 typedef ArgumentType2 argument_type_2;
126 typedef std::basic_ostream<charT, traits> stream_type;
127 typedef stream_type& (*manip_func)(stream_type&, const ArgumentType1&, const ArgumentType2&);
128
129 basic_omanipulator2(manip_func pf, const ArgumentType1& arg1, const ArgumentType2& arg2)
130 : pf_m(pf), arg1_m(arg1), arg2_m(arg2) {}
131
132 void do_manip(stream_type& strm) const {
133 if (error_m != std::ios_base::goodbit)
134 strm.setstate(error_m);
135 else {
136 std::ios_base::iostate err(error_m);
137 try {
138 (*pf_m)(strm, arg1_m, arg2_m);
139 } catch (...) {
140 err = handle_error(strm);
141 }
142
143 if (err)
144 strm.setstate(err);
145 }
146 }
147
148private:
149 manip_func pf_m;
150
151protected:
152 argument_type_1 arg1_m;
153 argument_type_2 arg2_m;
154};
155
156/**************************************************************************************************/
157
158template <class ArgumentType, class charT, class traits>
159std::basic_ostream<charT, traits>&
160operator<<(std::basic_ostream<charT, traits>& os,
161 const adobe::basic_omanipulator<ArgumentType, charT, traits>& manip) {
162 if (os.good())
163 manip.do_manip(os);
164
165 return os;
166}
167
168/**************************************************************************************************/
169
170template <class ArgumentType1, class ArgumentType2, class charT, class traits>
171std::basic_ostream<charT, traits>&
172operator<<(std::basic_ostream<charT, traits>& os,
173 const adobe::basic_omanipulator2<ArgumentType1, ArgumentType2, charT, traits>& manip) {
174 if (os.good())
175 manip.do_manip(os);
176
177 return os;
178}
179
180/**************************************************************************************************/
181
182template <class charT, class traits>
183class basic_bounded_width : public basic_omanipulator<unsigned int, charT, traits> {
184 typedef basic_omanipulator<unsigned int, charT, traits> inherited_t;
185
186public:
187 typedef typename inherited_t::stream_type stream_type;
188 typedef typename inherited_t::argument_type argument_type;
189
190 basic_bounded_width(argument_type min, argument_type max)
191 : basic_omanipulator<argument_type, charT, traits>(basic_bounded_width::fct, min),
192 min_m(min), max_m(max) {}
193
194 inherited_t& operator()(argument_type i) {
195 inherited_t::arg_m = std::min(max_m, std::max(i, min_m));
196 return *this;
197 }
198
199private:
200 static stream_type& fct(stream_type& strm, const argument_type& i) {
201 strm.width(i);
202 return strm;
203 }
204
205 argument_type min_m;
206 argument_type max_m;
207};
208
209typedef basic_bounded_width<char, std::char_traits<char>> bounded_width;
210typedef basic_bounded_width<wchar_t, std::char_traits<wchar_t>> wbounded_width;
211
213
214/**************************************************************************************************/
215
216} // namespace adobe
217
218/**************************************************************************************************/
219
220#endif
221
222/**************************************************************************************************/
223
224#endif
225
226/**************************************************************************************************/
const T & max(const T &a, const T &b, R r)
minmax implementation
Definition minmax.hpp:113
const T & min(const T &a, const T &b, R r)
minmax implementation
Definition minmax.hpp:69
std::ostream & operator<<(std::ostream &s, const extents_t &x)