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