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