Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:34:43

0001 /*=============================================================================
0002     Copyright (c) 1999-2003 Jeremiah Willcock
0003     Copyright (c) 1999-2003 Jaakko Jarvi
0004     Copyright (c) 2001-2011 Joel de Guzman
0005 
0006     Distributed under the Boost Software License, Version 1.0. (See accompanying 
0007     file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0008 ==============================================================================*/
0009 #if !defined(FUSION_MANIP_05052005_1200)
0010 #define FUSION_MANIP_05052005_1200
0011 
0012 #include <boost/fusion/support/config.hpp>
0013 #include <boost/config.hpp>
0014 #include <string>
0015 #include <vector>
0016 #include <cctype>
0017 
0018 // Tuple I/O manipulators
0019 
0020 #define FUSION_GET_CHAR_TYPE(T) typename T::char_type
0021 #define FUSION_GET_TRAITS_TYPE(T) typename T::traits_type
0022 
0023 #define FUSION_STRING_OF_STREAM(Stream)                                         \
0024     std::basic_string<                                                          \
0025         FUSION_GET_CHAR_TYPE(Stream)                                            \
0026       , FUSION_GET_TRAITS_TYPE(Stream)                                          \
0027     >
0028 
0029 //$$$ these should be part of the public API$$$
0030 //$$$ rename tuple_open, tuple_close and tuple_delimiter to 
0031 //    open, close and delimeter and add these synonyms to the
0032 //    TR1 tuple module.
0033 
0034 namespace boost { namespace fusion
0035 {
0036     namespace detail
0037     {
0038         template <typename Tag>
0039         int get_xalloc_index(int xalloc())
0040         {
0041             // each Tag will have a unique index
0042             static int const index = xalloc();
0043             return index;
0044         }
0045 
0046         template <typename Stream, typename Tag, typename T>
0047         struct stream_data
0048         {
0049             struct arena
0050             {
0051                 ~arena()
0052                 {
0053                     for (
0054                         typename std::vector<T*>::iterator i = data.begin()
0055                       ; i != data.end()
0056                       ; ++i)
0057                     {
0058                         delete *i;
0059                     }
0060                 }
0061 
0062                 std::vector<T*> data;
0063             };
0064 
0065             static void attach(Stream& stream, T const& data)
0066             {
0067                 static arena ar; // our arena
0068                 ar.data.push_back(new T(data));
0069                 stream.pword(get_xalloc_index<Tag>(stream.xalloc)) = ar.data.back();
0070             }
0071 
0072             static T const* get(Stream& stream)
0073             {
0074                 return (T const*)stream.pword(get_xalloc_index<Tag>(stream.xalloc));
0075             }
0076         };
0077 
0078 #ifdef _MSC_VER
0079 #  pragma warning(push)
0080 #  pragma warning(disable: 4512) // assignment operator could not be generated.
0081 #endif
0082         template <typename Tag, typename Stream>
0083         class string_ios_manip
0084         {
0085         public:
0086 
0087             typedef FUSION_STRING_OF_STREAM(Stream) string_type;
0088 
0089             typedef stream_data<Stream, Tag, string_type> stream_data_t;
0090 
0091             string_ios_manip(Stream& str_)
0092                 : stream(str_)
0093             {}
0094 
0095             void
0096             set(string_type const& s)
0097             {
0098                 stream_data_t::attach(stream, s);
0099             }
0100 
0101             void
0102             print(char const* default_) const
0103             {
0104                 // print a delimiter
0105                 string_type const* p = stream_data_t::get(stream);
0106                 if (p)
0107                     stream << *p;
0108                 else
0109                     stream << default_;
0110             }
0111 
0112             void
0113             read(char const* default_) const
0114             {
0115                 // read a delimiter
0116                 string_type const* p = stream_data_t::get(stream);
0117 
0118                 if (p)
0119                 {
0120                     typedef typename string_type::const_iterator iterator;
0121                     for (iterator i = p->begin(); i != p->end(); ++i)
0122                         check_delim(*i);
0123                 }
0124                 else
0125                 {
0126                     while (*default_)
0127                         check_delim(*default_++);
0128                 }
0129             }
0130 
0131         private:
0132 
0133             template <typename Char>
0134             void
0135             check_delim(Char c) const
0136             {
0137                 using namespace std;
0138                 if (!isspace(c))
0139                 {
0140                     if (stream.get() != c)
0141                     {
0142                         stream.unget();
0143                         stream.setstate(Stream::failbit);
0144                     }
0145                 }
0146             }
0147 
0148             Stream& stream;
0149         };
0150 #ifdef _MSC_VER
0151 #  pragma warning(pop)
0152 #endif
0153 
0154     } // detail
0155 
0156 
0157 #if defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
0158 
0159 #define STD_TUPLE_DEFINE_MANIPULATOR_FUNCTIONS(name)                            \
0160     template <typename Char, typename Traits>                                   \
0161     inline detail::name##_type<Char, Traits>                                    \
0162     name(const std::basic_string<Char, Traits>& s)                              \
0163     {                                                                           \
0164         return detail::name##_type<Char, Traits>(s);                            \
0165     }                                                                           \
0166                                                                                 \
0167     inline detail::name##_type<char>                                            \
0168     name(char const* s)                                                         \
0169     {                                                                           \
0170         return detail::name##_type<char>(std::basic_string<char>(s));           \
0171     }                                                                           \
0172                                                                                 \
0173     inline detail::name##_type<wchar_t>                                         \
0174     name(wchar_t const* s)                                                      \
0175     {                                                                           \
0176         return detail::name##_type<wchar_t>(std::basic_string<wchar_t>(s));     \
0177     }                                                                           \
0178                                                                                 \
0179     inline detail::name##_type<char>                                            \
0180     name(char c)                                                                \
0181     {                                                                           \
0182         return detail::name##_type<char>(std::basic_string<char>(1, c));        \
0183     }                                                                           \
0184                                                                                 \
0185     inline detail::name##_type<wchar_t>                                         \
0186     name(wchar_t c)                                                             \
0187     {                                                                           \
0188         return detail::name##_type<wchar_t>(std::basic_string<wchar_t>(1, c));  \
0189     }
0190 
0191 #else // defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
0192 
0193 #define STD_TUPLE_DEFINE_MANIPULATOR_FUNCTIONS(name)                            \
0194     template <typename Char, typename Traits>                                   \
0195     inline detail::name##_type<Char, Traits>                                    \
0196     name(const std::basic_string<Char, Traits>& s)                              \
0197     {                                                                           \
0198         return detail::name##_type<Char, Traits>(s);                            \
0199     }                                                                           \
0200                                                                                 \
0201     template <typename Char>                                                    \
0202     inline detail::name##_type<Char>                                            \
0203     name(Char s[])                                                              \
0204     {                                                                           \
0205         return detail::name##_type<Char>(std::basic_string<Char>(s));           \
0206     }                                                                           \
0207                                                                                 \
0208     template <typename Char>                                                    \
0209     inline detail::name##_type<Char>                                            \
0210     name(Char const s[])                                                        \
0211     {                                                                           \
0212         return detail::name##_type<Char>(std::basic_string<Char>(s));           \
0213     }                                                                           \
0214                                                                                 \
0215     template <typename Char>                                                    \
0216     inline detail::name##_type<Char>                                            \
0217     name(Char c)                                                                \
0218     {                                                                           \
0219         return detail::name##_type<Char>(std::basic_string<Char>(1, c));        \
0220     }
0221 
0222 #endif
0223 
0224 #define STD_TUPLE_DEFINE_MANIPULATOR(name)                                      \
0225     namespace detail                                                            \
0226     {                                                                           \
0227         struct name##_tag;                                                      \
0228                                                                                 \
0229         template <typename Char, typename Traits = std::char_traits<Char> >     \
0230         struct name##_type                                                      \
0231         {                                                                       \
0232             typedef std::basic_string<Char, Traits> string_type;                \
0233             string_type data;                                                   \
0234             name##_type(const string_type& d): data(d) {}                       \
0235         };                                                                      \
0236                                                                                 \
0237         template <typename Stream, typename Char, typename Traits>              \
0238         Stream& operator>>(Stream& s, const name##_type<Char,Traits>& m)        \
0239         {                                                                       \
0240             string_ios_manip<name##_tag, Stream> manip(s);                      \
0241             manip.set(m.data);                                                  \
0242             return s;                                                           \
0243         }                                                                       \
0244                                                                                 \
0245         template <typename Stream, typename Char, typename Traits>              \
0246         Stream& operator<<(Stream& s, const name##_type<Char,Traits>& m)        \
0247         {                                                                       \
0248             string_ios_manip<name##_tag, Stream> manip(s);                      \
0249             manip.set(m.data);                                                  \
0250             return s;                                                           \
0251         }                                                                       \
0252     }                                                                           \
0253 
0254 
0255     STD_TUPLE_DEFINE_MANIPULATOR(tuple_open)
0256     STD_TUPLE_DEFINE_MANIPULATOR(tuple_close)
0257     STD_TUPLE_DEFINE_MANIPULATOR(tuple_delimiter)
0258 
0259     STD_TUPLE_DEFINE_MANIPULATOR_FUNCTIONS(tuple_open)
0260     STD_TUPLE_DEFINE_MANIPULATOR_FUNCTIONS(tuple_close)
0261     STD_TUPLE_DEFINE_MANIPULATOR_FUNCTIONS(tuple_delimiter)
0262 
0263 #undef STD_TUPLE_DEFINE_MANIPULATOR
0264 #undef STD_TUPLE_DEFINE_MANIPULATOR_FUNCTIONS
0265 #undef FUSION_STRING_OF_STREAM
0266 #undef FUSION_GET_CHAR_TYPE
0267 #undef FUSION_GET_TRAITS_TYPE
0268 
0269 }}
0270 
0271 #endif