1 /*=============================================================================
2 Copyright (c) 2001-2011 Joel de Guzman
3 Copyright (c) 2001-2011 Hartmut Kaiser
4 Copyright (c) 2010 Bryce Lelbach
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(BOOST_SPIRIT_STRING_TRAITS_OCTOBER_2008_1252PM)
10 #define BOOST_SPIRIT_STRING_TRAITS_OCTOBER_2008_1252PM
16 #include <boost/spirit/home/support/container.hpp>
18 #include <boost/mpl/bool.hpp>
19 #include <boost/mpl/identity.hpp>
20 #include <boost/mpl/if.hpp>
21 #include <boost/proto/proto_fwd.hpp> // for BOOST_PROTO_DISABLE_IF_IS_CONST
22 #include <boost/type_traits/is_const.hpp>
23 #if defined(__GNUC__) && (__GNUC__ < 4)
24 #include <boost/type_traits/add_const.hpp>
27 namespace boost { namespace spirit { namespace traits
29 ///////////////////////////////////////////////////////////////////////////
30 // Determine if T is a character type
31 ///////////////////////////////////////////////////////////////////////////
33 struct is_char : mpl::false_ {};
36 struct is_char<T const> : is_char<T> {};
39 struct is_char<char> : mpl::true_ {};
42 struct is_char<wchar_t> : mpl::true_ {};
44 ///////////////////////////////////////////////////////////////////////////
45 // Determine if T is a string
46 ///////////////////////////////////////////////////////////////////////////
48 struct is_string : mpl::false_ {};
51 struct is_string<T const> : is_string<T> {};
54 struct is_string<char const*> : mpl::true_ {};
57 struct is_string<wchar_t const*> : mpl::true_ {};
60 struct is_string<char*> : mpl::true_ {};
63 struct is_string<wchar_t*> : mpl::true_ {};
65 template <std::size_t N>
66 struct is_string<char[N]> : mpl::true_ {};
68 template <std::size_t N>
69 struct is_string<wchar_t[N]> : mpl::true_ {};
71 template <std::size_t N>
72 struct is_string<char const[N]> : mpl::true_ {};
74 template <std::size_t N>
75 struct is_string<wchar_t const[N]> : mpl::true_ {};
77 template <std::size_t N>
78 struct is_string<char(&)[N]> : mpl::true_ {};
80 template <std::size_t N>
81 struct is_string<wchar_t(&)[N]> : mpl::true_ {};
83 template <std::size_t N>
84 struct is_string<char const(&)[N]> : mpl::true_ {};
86 template <std::size_t N>
87 struct is_string<wchar_t const(&)[N]> : mpl::true_ {};
89 template <typename T, typename Traits, typename Allocator>
90 struct is_string<std::basic_string<T, Traits, Allocator> > : mpl::true_ {};
92 ///////////////////////////////////////////////////////////////////////////
93 // Get the underlying char type of a string
94 ///////////////////////////////////////////////////////////////////////////
99 struct char_type_of<T const> : char_type_of<T> {};
102 struct char_type_of<char> : mpl::identity<char> {};
105 struct char_type_of<wchar_t> : mpl::identity<wchar_t> {};
108 struct char_type_of<char const*> : mpl::identity<char const> {};
111 struct char_type_of<wchar_t const*> : mpl::identity<wchar_t const> {};
114 struct char_type_of<char*> : mpl::identity<char> {};
117 struct char_type_of<wchar_t*> : mpl::identity<wchar_t> {};
119 template <std::size_t N>
120 struct char_type_of<char[N]> : mpl::identity<char> {};
122 template <std::size_t N>
123 struct char_type_of<wchar_t[N]> : mpl::identity<wchar_t> {};
125 template <std::size_t N>
126 struct char_type_of<char const[N]> : mpl::identity<char const> {};
128 template <std::size_t N>
129 struct char_type_of<wchar_t const[N]> : mpl::identity<wchar_t const> {};
131 template <std::size_t N>
132 struct char_type_of<char(&)[N]> : mpl::identity<char> {};
134 template <std::size_t N>
135 struct char_type_of<wchar_t(&)[N]> : mpl::identity<wchar_t> {};
137 template <std::size_t N>
138 struct char_type_of<char const(&)[N]> : mpl::identity<char const> {};
140 template <std::size_t N>
141 struct char_type_of<wchar_t const(&)[N]> : mpl::identity<wchar_t const> {};
143 template <typename T, typename Traits, typename Allocator>
144 struct char_type_of<std::basic_string<T, Traits, Allocator> >
145 : mpl::identity<T> {};
147 ///////////////////////////////////////////////////////////////////////////
148 // Get the C string from a string
149 ///////////////////////////////////////////////////////////////////////////
150 template <typename String>
151 struct extract_c_string;
153 template <typename String>
154 struct extract_c_string
156 typedef typename char_type_of<String>::type char_type;
158 template <typename T>
159 static T const* call (T* str)
161 return (T const*)str;
164 template <typename T>
165 static T const* call (T const* str)
171 // Forwarder that strips const
172 template <typename T>
173 struct extract_c_string<T const>
175 typedef typename extract_c_string<T>::char_type char_type;
177 static typename extract_c_string<T>::char_type const* call (T const str)
179 return extract_c_string<T>::call(str);
183 // Forwarder that strips references
184 template <typename T>
185 struct extract_c_string<T&>
187 typedef typename extract_c_string<T>::char_type char_type;
189 static typename extract_c_string<T>::char_type const* call (T& str)
191 return extract_c_string<T>::call(str);
195 // Forwarder that strips const references
196 template <typename T>
197 struct extract_c_string<T const&>
199 typedef typename extract_c_string<T>::char_type char_type;
201 static typename extract_c_string<T>::char_type const* call (T const& str)
203 return extract_c_string<T>::call(str);
207 template <typename T, typename Traits, typename Allocator>
208 struct extract_c_string<std::basic_string<T, Traits, Allocator> >
212 typedef std::basic_string<T, Traits, Allocator> string;
214 static T const* call (string const& str)
220 template <typename T>
221 typename extract_c_string<T*>::char_type const*
222 get_c_string (T* str)
224 return extract_c_string<T*>::call(str);
227 template <typename T>
228 typename extract_c_string<T const*>::char_type const*
229 get_c_string (T const* str)
231 return extract_c_string<T const*>::call(str);
234 template <typename String>
235 typename extract_c_string<String>::char_type const*
236 get_c_string (String& str)
238 return extract_c_string<String>::call(str);
241 template <typename String>
242 typename extract_c_string<String>::char_type const*
243 get_c_string (String const& str)
245 return extract_c_string<String>::call(str);
248 ///////////////////////////////////////////////////////////////////////////
249 // Get the begin/end iterators from a string
250 ///////////////////////////////////////////////////////////////////////////
252 // Implementation for C-style strings.
254 // gcc 3.x.x has problems resolving ambiguities here
255 #if defined(__GNUC__) && (__GNUC__ < 4)
256 template <typename T>
257 inline typename add_const<T>::type * get_begin(T* str) { return str; }
259 template <typename T>
260 inline typename add_const<T>::type* get_end(T* str)
268 template <typename T>
269 inline T const* get_begin(T const* str) { return str; }
271 template <typename T>
272 inline T* get_begin(T* str) { return str; }
274 template <typename T>
275 inline T const* get_end(T const* str)
283 template <typename T>
284 inline T* get_end(T* str)
293 // Implementation for containers (includes basic_string).
294 template <typename T, typename Str>
295 inline typename Str::const_iterator get_begin(Str const& str)
296 { return str.begin(); }
298 template <typename T, typename Str>
299 inline typename Str::iterator
300 get_begin(Str& str BOOST_PROTO_DISABLE_IF_IS_CONST(Str))
301 { return str.begin(); }
303 template <typename T, typename Str>
304 inline typename Str::const_iterator get_end(Str const& str)
305 { return str.end(); }
307 template <typename T, typename Str>
308 inline typename Str::iterator
309 get_end(Str& str BOOST_PROTO_DISABLE_IF_IS_CONST(Str))
310 { return str.end(); }
312 // Default implementation for other types: try a C-style string
314 // These overloads are explicitly disabled for containers,
315 // as they would be ambiguous with the previous ones.
316 template <typename T, typename Str>
317 inline typename disable_if<is_container<Str>
318 , T const*>::type get_begin(Str const& str)
321 template <typename T, typename Str>
322 inline typename disable_if<is_container<Str>
323 , T const*>::type get_end(Str const& str)
324 { return get_end(get_begin<T>(str)); }
329 template <typename Char, typename T, typename Enable = void>
332 typedef typename traits::char_type_of<T>::type char_type;
334 typedef typename mpl::if_<
341 template <typename Char, typename Str>
342 struct get_begin<Char, Str
343 , typename enable_if<traits::is_container<Str> >::type>
345 typedef typename mpl::if_<
347 , typename Str::const_iterator
348 , typename Str::iterator
352 template <typename Char, typename T>
353 struct get_end : get_begin<Char, T> {};