]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /*============================================================================= |
2 | Copyright (c) 2001-2014 Joel de Guzman | |
3 | Copyright (c) 2001-2011 Hartmut Kaiser | |
4 | Copyright (c) 2010 Bryce Lelbach | |
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(BOOST_SPIRIT_X3_STRING_TRAITS_OCTOBER_2008_1252PM) | |
10 | #define BOOST_SPIRIT_X3_STRING_TRAITS_OCTOBER_2008_1252PM | |
11 | ||
12 | #include <string> | |
13 | #include <boost/mpl/bool.hpp> | |
7c673cae FG |
14 | |
15 | namespace boost { namespace spirit { namespace x3 { namespace traits | |
16 | { | |
7c673cae FG |
17 | /////////////////////////////////////////////////////////////////////////// |
18 | // Get the C string from a string | |
19 | /////////////////////////////////////////////////////////////////////////// | |
20 | template <typename String> | |
21 | struct extract_c_string; | |
22 | ||
23 | template <typename String> | |
24 | struct extract_c_string | |
25 | { | |
7c673cae FG |
26 | template <typename T> |
27 | static T const* call (T* str) | |
28 | { | |
29 | return (T const*)str; | |
30 | } | |
31 | ||
32 | template <typename T> | |
33 | static T const* call (T const* str) | |
34 | { | |
35 | return str; | |
36 | } | |
37 | }; | |
38 | ||
39 | // Forwarder that strips const | |
40 | template <typename T> | |
41 | struct extract_c_string<T const> | |
42 | { | |
92f5a8d4 | 43 | static decltype(auto) call(T const str) |
7c673cae FG |
44 | { |
45 | return extract_c_string<T>::call(str); | |
46 | } | |
47 | }; | |
48 | ||
49 | // Forwarder that strips references | |
50 | template <typename T> | |
51 | struct extract_c_string<T&> | |
52 | { | |
92f5a8d4 | 53 | static decltype(auto) call(T& str) |
7c673cae FG |
54 | { |
55 | return extract_c_string<T>::call(str); | |
56 | } | |
57 | }; | |
58 | ||
59 | // Forwarder that strips const references | |
60 | template <typename T> | |
61 | struct extract_c_string<T const&> | |
62 | { | |
92f5a8d4 | 63 | static decltype(auto) call(T const& str) |
7c673cae FG |
64 | { |
65 | return extract_c_string<T>::call(str); | |
66 | } | |
67 | }; | |
68 | ||
69 | template <typename T, typename Traits, typename Allocator> | |
70 | struct extract_c_string<std::basic_string<T, Traits, Allocator> > | |
71 | { | |
7c673cae FG |
72 | typedef std::basic_string<T, Traits, Allocator> string; |
73 | ||
74 | static T const* call (string const& str) | |
75 | { | |
76 | return str.c_str(); | |
77 | } | |
78 | }; | |
79 | ||
80 | template <typename T> | |
92f5a8d4 | 81 | decltype(auto) get_c_string(T* str) |
7c673cae FG |
82 | { |
83 | return extract_c_string<T*>::call(str); | |
84 | } | |
85 | ||
86 | template <typename T> | |
92f5a8d4 | 87 | decltype(auto) get_c_string(T const* str) |
7c673cae FG |
88 | { |
89 | return extract_c_string<T const*>::call(str); | |
90 | } | |
91 | ||
92 | template <typename String> | |
92f5a8d4 | 93 | decltype(auto) get_c_string(String& str) |
7c673cae FG |
94 | { |
95 | return extract_c_string<String>::call(str); | |
96 | } | |
97 | ||
98 | template <typename String> | |
92f5a8d4 | 99 | decltype(auto) get_c_string(String const& str) |
7c673cae FG |
100 | { |
101 | return extract_c_string<String>::call(str); | |
102 | } | |
103 | ||
104 | /////////////////////////////////////////////////////////////////////////// | |
105 | // Get the begin/end iterators from a string | |
106 | /////////////////////////////////////////////////////////////////////////// | |
107 | ||
108 | // Implementation for C-style strings. | |
109 | ||
110 | template <typename T> | |
111 | inline T const* get_string_begin(T const* str) { return str; } | |
112 | ||
113 | template <typename T> | |
114 | inline T* get_string_begin(T* str) { return str; } | |
115 | ||
116 | template <typename T> | |
117 | inline T const* get_string_end(T const* str) | |
118 | { | |
119 | T const* last = str; | |
120 | while (*last) | |
121 | last++; | |
122 | return last; | |
123 | } | |
124 | ||
125 | template <typename T> | |
126 | inline T* get_string_end(T* str) | |
127 | { | |
128 | T* last = str; | |
129 | while (*last) | |
130 | last++; | |
131 | return last; | |
132 | } | |
133 | ||
134 | // Implementation for containers (includes basic_string). | |
135 | template <typename T, typename Str> | |
136 | inline typename Str::const_iterator get_string_begin(Str const& str) | |
137 | { return str.begin(); } | |
138 | ||
139 | template <typename T, typename Str> | |
140 | inline typename Str::iterator | |
141 | get_string_begin(Str& str) | |
142 | { return str.begin(); } | |
143 | ||
144 | template <typename T, typename Str> | |
145 | inline typename Str::const_iterator get_string_end(Str const& str) | |
146 | { return str.end(); } | |
147 | ||
148 | template <typename T, typename Str> | |
149 | inline typename Str::iterator | |
150 | get_string_end(Str& str) | |
151 | { return str.end(); } | |
152 | }}}} | |
153 | ||
154 | #endif |