Chris@16
|
1 /*=============================================================================
|
Chris@16
|
2 Copyright (c) 1999-2003 Jeremiah Willcock
|
Chris@16
|
3 Copyright (c) 1999-2003 Jaakko Jarvi
|
Chris@16
|
4 Copyright (c) 2001-2011 Joel de Guzman
|
Chris@16
|
5
|
Chris@16
|
6 Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
7 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
8 ==============================================================================*/
|
Chris@16
|
9 #if !defined(FUSION_MANIP_05052005_1200)
|
Chris@16
|
10 #define FUSION_MANIP_05052005_1200
|
Chris@16
|
11
|
Chris@16
|
12 #include <boost/config.hpp>
|
Chris@16
|
13 #include <string>
|
Chris@16
|
14 #include <vector>
|
Chris@16
|
15 #include <cctype>
|
Chris@16
|
16
|
Chris@16
|
17 // Tuple I/O manipulators
|
Chris@16
|
18
|
Chris@16
|
19 #define FUSION_GET_CHAR_TYPE(T) typename T::char_type
|
Chris@16
|
20 #define FUSION_GET_TRAITS_TYPE(T) typename T::traits_type
|
Chris@16
|
21
|
Chris@16
|
22 #if defined (BOOST_NO_TEMPLATED_STREAMS)
|
Chris@16
|
23 #define FUSION_STRING_OF_STREAM(Stream) std::string
|
Chris@16
|
24 #else
|
Chris@16
|
25 #define FUSION_STRING_OF_STREAM(Stream) \
|
Chris@16
|
26 std::basic_string< \
|
Chris@16
|
27 FUSION_GET_CHAR_TYPE(Stream) \
|
Chris@16
|
28 , FUSION_GET_TRAITS_TYPE(Stream) \
|
Chris@16
|
29 >
|
Chris@16
|
30 #endif
|
Chris@16
|
31
|
Chris@16
|
32 //$$$ these should be part of the public API$$$
|
Chris@16
|
33 //$$$ rename tuple_open, tuple_close and tuple_delimiter to
|
Chris@16
|
34 // open, close and delimeter and add these synonyms to the
|
Chris@16
|
35 // TR1 tuple module.
|
Chris@16
|
36
|
Chris@16
|
37 namespace boost { namespace fusion
|
Chris@16
|
38 {
|
Chris@16
|
39 namespace detail
|
Chris@16
|
40 {
|
Chris@16
|
41 template <typename Tag>
|
Chris@16
|
42 int get_xalloc_index(Tag* = 0)
|
Chris@16
|
43 {
|
Chris@16
|
44 // each Tag will have a unique index
|
Chris@16
|
45 static int index = std::ios::xalloc();
|
Chris@16
|
46 return index;
|
Chris@16
|
47 }
|
Chris@16
|
48
|
Chris@16
|
49 template <typename Stream, typename Tag, typename T>
|
Chris@16
|
50 struct stream_data
|
Chris@16
|
51 {
|
Chris@16
|
52 struct arena
|
Chris@16
|
53 {
|
Chris@16
|
54 ~arena()
|
Chris@16
|
55 {
|
Chris@16
|
56 for (
|
Chris@16
|
57 typename std::vector<T*>::iterator i = data.begin()
|
Chris@16
|
58 ; i != data.end()
|
Chris@16
|
59 ; ++i)
|
Chris@16
|
60 {
|
Chris@16
|
61 delete *i;
|
Chris@16
|
62 }
|
Chris@16
|
63 }
|
Chris@16
|
64
|
Chris@16
|
65 std::vector<T*> data;
|
Chris@16
|
66 };
|
Chris@16
|
67
|
Chris@16
|
68 static void attach(Stream& stream, T const& data)
|
Chris@16
|
69 {
|
Chris@16
|
70 static arena ar; // our arena
|
Chris@16
|
71 ar.data.push_back(new T(data));
|
Chris@16
|
72 stream.pword(get_xalloc_index<Tag>()) = ar.data.back();
|
Chris@16
|
73 }
|
Chris@16
|
74
|
Chris@16
|
75 static T const* get(Stream& stream)
|
Chris@16
|
76 {
|
Chris@16
|
77 return (T const*)stream.pword(get_xalloc_index<Tag>());
|
Chris@16
|
78 }
|
Chris@16
|
79 };
|
Chris@16
|
80
|
Chris@16
|
81 template <typename Tag, typename Stream>
|
Chris@16
|
82 class string_ios_manip
|
Chris@16
|
83 {
|
Chris@16
|
84 public:
|
Chris@16
|
85
|
Chris@16
|
86 typedef FUSION_STRING_OF_STREAM(Stream) string_type;
|
Chris@16
|
87
|
Chris@16
|
88 typedef stream_data<Stream, Tag, string_type> stream_data_t;
|
Chris@16
|
89
|
Chris@16
|
90 string_ios_manip(Stream& str_)
|
Chris@16
|
91 : stream(str_)
|
Chris@16
|
92 {}
|
Chris@16
|
93
|
Chris@16
|
94 void
|
Chris@16
|
95 set(string_type const& s)
|
Chris@16
|
96 {
|
Chris@16
|
97 stream_data_t::attach(stream, s);
|
Chris@16
|
98 }
|
Chris@16
|
99
|
Chris@16
|
100 void
|
Chris@16
|
101 print(char const* default_) const
|
Chris@16
|
102 {
|
Chris@16
|
103 // print a delimiter
|
Chris@16
|
104 string_type const* p = stream_data_t::get(stream);
|
Chris@16
|
105 if (p)
|
Chris@16
|
106 stream << *p;
|
Chris@16
|
107 else
|
Chris@16
|
108 stream << default_;
|
Chris@16
|
109 }
|
Chris@16
|
110
|
Chris@16
|
111 void
|
Chris@16
|
112 read(char const* default_) const
|
Chris@16
|
113 {
|
Chris@16
|
114 // read a delimiter
|
Chris@16
|
115 string_type const* p = stream_data_t::get(stream);
|
Chris@16
|
116 using namespace std;
|
Chris@16
|
117 ws(stream);
|
Chris@16
|
118
|
Chris@16
|
119 if (p)
|
Chris@16
|
120 {
|
Chris@16
|
121 typedef typename string_type::const_iterator iterator;
|
Chris@16
|
122 for (iterator i = p->begin(); i != p->end(); ++i)
|
Chris@16
|
123 check_delim(*i);
|
Chris@16
|
124 }
|
Chris@16
|
125 else
|
Chris@16
|
126 {
|
Chris@16
|
127 while (*default_)
|
Chris@16
|
128 check_delim(*default_++);
|
Chris@16
|
129 }
|
Chris@16
|
130 }
|
Chris@16
|
131
|
Chris@16
|
132 private:
|
Chris@16
|
133
|
Chris@16
|
134 template <typename Char>
|
Chris@16
|
135 void
|
Chris@16
|
136 check_delim(Char c) const
|
Chris@16
|
137 {
|
Chris@16
|
138 if (!isspace(c))
|
Chris@16
|
139 {
|
Chris@16
|
140 if (stream.get() != c)
|
Chris@16
|
141 {
|
Chris@16
|
142 stream.unget();
|
Chris@16
|
143 stream.setstate(std::ios::failbit);
|
Chris@16
|
144 }
|
Chris@16
|
145 }
|
Chris@16
|
146 }
|
Chris@16
|
147
|
Chris@16
|
148 Stream& stream;
|
Chris@16
|
149
|
Chris@16
|
150 private:
|
Chris@16
|
151 // silence MSVC warning C4512: assignment operator could not be generated
|
Chris@16
|
152 string_ios_manip& operator= (string_ios_manip const&);
|
Chris@16
|
153 };
|
Chris@16
|
154
|
Chris@16
|
155 } // detail
|
Chris@16
|
156
|
Chris@16
|
157 #if defined (BOOST_NO_TEMPLATED_STREAMS)
|
Chris@16
|
158
|
Chris@16
|
159 #define STD_TUPLE_DEFINE_MANIPULATOR(name) \
|
Chris@16
|
160 namespace detail \
|
Chris@16
|
161 { \
|
Chris@16
|
162 struct name##_tag; \
|
Chris@16
|
163 \
|
Chris@16
|
164 struct name##_type \
|
Chris@16
|
165 { \
|
Chris@16
|
166 typedef std::string string_type; \
|
Chris@16
|
167 string_type data; \
|
Chris@16
|
168 name##_type(const string_type& d): data(d) {} \
|
Chris@16
|
169 }; \
|
Chris@16
|
170 \
|
Chris@16
|
171 template <typename Stream> \
|
Chris@16
|
172 Stream& operator>>(Stream& s, const name##_type& m) \
|
Chris@16
|
173 { \
|
Chris@16
|
174 string_ios_manip<name##_tag, Stream>(s).set(m.data); \
|
Chris@16
|
175 return s; \
|
Chris@16
|
176 } \
|
Chris@16
|
177 \
|
Chris@16
|
178 template <typename Stream> \
|
Chris@16
|
179 Stream& operator<<(Stream& s, const name##_type& m) \
|
Chris@16
|
180 { \
|
Chris@16
|
181 string_ios_manip<name##_tag, Stream>(s).set(m.data); \
|
Chris@16
|
182 return s; \
|
Chris@16
|
183 } \
|
Chris@16
|
184 }
|
Chris@16
|
185
|
Chris@16
|
186 #define STD_TUPLE_DEFINE_MANIPULATOR_FUNCTIONS(name) \
|
Chris@16
|
187 inline detail::name##_type \
|
Chris@16
|
188 name(const std::string& s) \
|
Chris@16
|
189 { \
|
Chris@16
|
190 return detail::name##_type(s); \
|
Chris@16
|
191 } \
|
Chris@16
|
192 \
|
Chris@16
|
193 inline detail::name##_type \
|
Chris@16
|
194 name(const char* s) \
|
Chris@16
|
195 { \
|
Chris@16
|
196 return detail::name##_type(std::string(s)); \
|
Chris@16
|
197 } \
|
Chris@16
|
198 \
|
Chris@16
|
199 inline detail::name##_type \
|
Chris@16
|
200 name(char c) \
|
Chris@16
|
201 { \
|
Chris@16
|
202 return detail::name##_type(std::string(1, c)); \
|
Chris@16
|
203 }
|
Chris@16
|
204
|
Chris@16
|
205 #else // defined(BOOST_NO_TEMPLATED_STREAMS)
|
Chris@16
|
206
|
Chris@16
|
207 #if defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
|
Chris@16
|
208
|
Chris@16
|
209 #define STD_TUPLE_DEFINE_MANIPULATOR_FUNCTIONS(name) \
|
Chris@16
|
210 template <typename Char, typename Traits> \
|
Chris@16
|
211 inline detail::name##_type<Char, Traits> \
|
Chris@16
|
212 name(const std::basic_string<Char, Traits>& s) \
|
Chris@16
|
213 { \
|
Chris@16
|
214 return detail::name##_type<Char, Traits>(s); \
|
Chris@16
|
215 } \
|
Chris@16
|
216 \
|
Chris@16
|
217 inline detail::name##_type<char> \
|
Chris@16
|
218 name(char const* s) \
|
Chris@16
|
219 { \
|
Chris@16
|
220 return detail::name##_type<char>(std::basic_string<char>(s)); \
|
Chris@16
|
221 } \
|
Chris@16
|
222 \
|
Chris@16
|
223 inline detail::name##_type<wchar_t> \
|
Chris@16
|
224 name(wchar_t const* s) \
|
Chris@16
|
225 { \
|
Chris@16
|
226 return detail::name##_type<wchar_t>(std::basic_string<wchar_t>(s)); \
|
Chris@16
|
227 } \
|
Chris@16
|
228 \
|
Chris@16
|
229 inline detail::name##_type<char> \
|
Chris@16
|
230 name(char c) \
|
Chris@16
|
231 { \
|
Chris@16
|
232 return detail::name##_type<char>(std::basic_string<char>(1, c)); \
|
Chris@16
|
233 } \
|
Chris@16
|
234 \
|
Chris@16
|
235 inline detail::name##_type<wchar_t> \
|
Chris@16
|
236 name(wchar_t c) \
|
Chris@16
|
237 { \
|
Chris@16
|
238 return detail::name##_type<wchar_t>(std::basic_string<wchar_t>(1, c)); \
|
Chris@16
|
239 }
|
Chris@16
|
240
|
Chris@16
|
241 #else // defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
|
Chris@16
|
242
|
Chris@16
|
243 #define STD_TUPLE_DEFINE_MANIPULATOR_FUNCTIONS(name) \
|
Chris@16
|
244 template <typename Char, typename Traits> \
|
Chris@16
|
245 inline detail::name##_type<Char, Traits> \
|
Chris@16
|
246 name(const std::basic_string<Char, Traits>& s) \
|
Chris@16
|
247 { \
|
Chris@16
|
248 return detail::name##_type<Char, Traits>(s); \
|
Chris@16
|
249 } \
|
Chris@16
|
250 \
|
Chris@16
|
251 template <typename Char> \
|
Chris@16
|
252 inline detail::name##_type<Char> \
|
Chris@16
|
253 name(Char s[]) \
|
Chris@16
|
254 { \
|
Chris@16
|
255 return detail::name##_type<Char>(std::basic_string<Char>(s)); \
|
Chris@16
|
256 } \
|
Chris@16
|
257 \
|
Chris@16
|
258 template <typename Char> \
|
Chris@16
|
259 inline detail::name##_type<Char> \
|
Chris@16
|
260 name(Char const s[]) \
|
Chris@16
|
261 { \
|
Chris@16
|
262 return detail::name##_type<Char>(std::basic_string<Char>(s)); \
|
Chris@16
|
263 } \
|
Chris@16
|
264 \
|
Chris@16
|
265 template <typename Char> \
|
Chris@16
|
266 inline detail::name##_type<Char> \
|
Chris@16
|
267 name(Char c) \
|
Chris@16
|
268 { \
|
Chris@16
|
269 return detail::name##_type<Char>(std::basic_string<Char>(1, c)); \
|
Chris@16
|
270 }
|
Chris@16
|
271
|
Chris@16
|
272 #endif
|
Chris@16
|
273
|
Chris@16
|
274 #define STD_TUPLE_DEFINE_MANIPULATOR(name) \
|
Chris@16
|
275 namespace detail \
|
Chris@16
|
276 { \
|
Chris@16
|
277 struct name##_tag; \
|
Chris@16
|
278 \
|
Chris@16
|
279 template <typename Char, typename Traits = std::char_traits<Char> > \
|
Chris@16
|
280 struct name##_type \
|
Chris@16
|
281 { \
|
Chris@16
|
282 typedef std::basic_string<Char, Traits> string_type; \
|
Chris@16
|
283 string_type data; \
|
Chris@16
|
284 name##_type(const string_type& d): data(d) {} \
|
Chris@16
|
285 }; \
|
Chris@16
|
286 \
|
Chris@16
|
287 template <typename Stream, typename Char, typename Traits> \
|
Chris@16
|
288 Stream& operator>>(Stream& s, const name##_type<Char,Traits>& m) \
|
Chris@16
|
289 { \
|
Chris@16
|
290 string_ios_manip<name##_tag, Stream>(s).set(m.data); \
|
Chris@16
|
291 return s; \
|
Chris@16
|
292 } \
|
Chris@16
|
293 \
|
Chris@16
|
294 template <typename Stream, typename Char, typename Traits> \
|
Chris@16
|
295 Stream& operator<<(Stream& s, const name##_type<Char,Traits>& m) \
|
Chris@16
|
296 { \
|
Chris@16
|
297 string_ios_manip<name##_tag, Stream>(s).set(m.data); \
|
Chris@16
|
298 return s; \
|
Chris@16
|
299 } \
|
Chris@16
|
300 } \
|
Chris@16
|
301
|
Chris@16
|
302 #endif // defined(BOOST_NO_TEMPLATED_STREAMS)
|
Chris@16
|
303
|
Chris@16
|
304 STD_TUPLE_DEFINE_MANIPULATOR(tuple_open)
|
Chris@16
|
305 STD_TUPLE_DEFINE_MANIPULATOR(tuple_close)
|
Chris@16
|
306 STD_TUPLE_DEFINE_MANIPULATOR(tuple_delimiter)
|
Chris@16
|
307
|
Chris@16
|
308 STD_TUPLE_DEFINE_MANIPULATOR_FUNCTIONS(tuple_open)
|
Chris@16
|
309 STD_TUPLE_DEFINE_MANIPULATOR_FUNCTIONS(tuple_close)
|
Chris@16
|
310 STD_TUPLE_DEFINE_MANIPULATOR_FUNCTIONS(tuple_delimiter)
|
Chris@16
|
311
|
Chris@16
|
312 #undef STD_TUPLE_DEFINE_MANIPULATOR
|
Chris@16
|
313 #undef STD_TUPLE_DEFINE_MANIPULATOR_FUNCTIONS
|
Chris@16
|
314 #undef FUSION_STRING_OF_STREAM
|
Chris@16
|
315 #undef FUSION_GET_CHAR_TYPE
|
Chris@16
|
316 #undef FUSION_GET_TRAITS_TYPE
|
Chris@16
|
317
|
Chris@16
|
318 }}
|
Chris@16
|
319
|
Chris@16
|
320 #endif
|