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