]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/filesystem/path_traits.hpp
bump version to 18.2.2-pve1
[ceph.git] / ceph / src / boost / boost / filesystem / path_traits.hpp
1 // filesystem path_traits.hpp --------------------------------------------------------//
2
3 // Copyright Beman Dawes 2009
4
5 // Distributed under the Boost Software License, Version 1.0.
6 // See http://www.boost.org/LICENSE_1_0.txt
7
8 // Library home page: http://www.boost.org/libs/filesystem
9
10 #ifndef BOOST_FILESYSTEM_PATH_TRAITS_HPP
11 #define BOOST_FILESYSTEM_PATH_TRAITS_HPP
12
13 #include <boost/filesystem/config.hpp>
14 #include <boost/system/error_category.hpp>
15 #include <boost/type_traits/is_array.hpp>
16 #include <boost/type_traits/decay.hpp>
17 #include <boost/core/enable_if.hpp>
18 #include <cstddef>
19 #include <cwchar> // for mbstate_t
20 #include <string>
21 #include <vector>
22 #include <list>
23 #include <iterator>
24 #include <locale>
25 #include <boost/assert.hpp>
26
27 #include <boost/filesystem/detail/header.hpp> // must be the last #include
28
29 namespace boost {
30 namespace filesystem {
31
32 BOOST_FILESYSTEM_DECL system::error_category const& codecvt_error_category() BOOST_NOEXCEPT;
33 // uses std::codecvt_base::result used for error codes:
34 //
35 // ok: Conversion successful.
36 // partial: Not all source characters converted; one or more additional source
37 // characters are needed to produce the final target character, or the
38 // size of the target intermediate buffer was too small to hold the result.
39 // error: A character in the source could not be converted to the target encoding.
40 // noconv: The source and target characters have the same type and encoding, so no
41 // conversion was necessary.
42
43 class directory_entry;
44
45 namespace path_traits {
46
47 typedef std::codecvt< wchar_t, char, std::mbstate_t > codecvt_type;
48
49 // is_pathable type trait; allows disabling over-agressive class path member templates
50
51 template< class T >
52 struct is_pathable
53 {
54 static const bool value = false;
55 };
56
57 template<>
58 struct is_pathable< char* >
59 {
60 static const bool value = true;
61 };
62
63 template<>
64 struct is_pathable< const char* >
65 {
66 static const bool value = true;
67 };
68
69 template<>
70 struct is_pathable< wchar_t* >
71 {
72 static const bool value = true;
73 };
74
75 template<>
76 struct is_pathable< const wchar_t* >
77 {
78 static const bool value = true;
79 };
80
81 template<>
82 struct is_pathable< std::string >
83 {
84 static const bool value = true;
85 };
86
87 template<>
88 struct is_pathable< std::wstring >
89 {
90 static const bool value = true;
91 };
92
93 template<>
94 struct is_pathable< std::vector< char > >
95 {
96 static const bool value = true;
97 };
98
99 template<>
100 struct is_pathable< std::vector< wchar_t > >
101 {
102 static const bool value = true;
103 };
104
105 template<>
106 struct is_pathable< std::list< char > >
107 {
108 static const bool value = true;
109 };
110
111 template<>
112 struct is_pathable< std::list< wchar_t > >
113 {
114 static const bool value = true;
115 };
116
117 template<>
118 struct is_pathable< directory_entry >
119 {
120 static const bool value = true;
121 };
122
123 // Pathable empty
124
125 template< class Container >
126 inline
127 // disable_if aids broken compilers (IBM, old GCC, etc.) and is harmless for
128 // conforming compilers. Replace by plain "bool" at some future date (2012?)
129 typename boost::disable_if< boost::is_array< Container >, bool >::type
130 empty(Container const& c)
131 {
132 return c.begin() == c.end();
133 }
134
135 template< class T >
136 inline bool empty(T* const& c_str)
137 {
138 BOOST_ASSERT(c_str);
139 return !*c_str;
140 }
141
142 template< typename T, std::size_t N >
143 inline bool empty(T (&x)[N])
144 {
145 return !x[0];
146 }
147
148 // value types differ ---------------------------------------------------------------//
149 //
150 // A from_end argument of 0 is less efficient than a known end, so use only if needed
151
152 // with codecvt
153
154 BOOST_FILESYSTEM_DECL
155 void convert(const char* from,
156 const char* from_end, // 0 for null terminated MBCS
157 std::wstring& to, codecvt_type const& cvt);
158
159 BOOST_FILESYSTEM_DECL
160 void convert(const wchar_t* from,
161 const wchar_t* from_end, // 0 for null terminated MBCS
162 std::string& to, codecvt_type const& cvt);
163
164 inline void convert(const char* from, std::wstring& to, codecvt_type const& cvt)
165 {
166 BOOST_ASSERT(from);
167 convert(from, 0, to, cvt);
168 }
169
170 inline void convert(const wchar_t* from, std::string& to, codecvt_type const& cvt)
171 {
172 BOOST_ASSERT(from);
173 convert(from, 0, to, cvt);
174 }
175
176 // without codecvt
177
178 inline void convert(const char* from,
179 const char* from_end, // 0 for null terminated MBCS
180 std::wstring& to);
181
182 inline void convert(const wchar_t* from,
183 const wchar_t* from_end, // 0 for null terminated MBCS
184 std::string& to);
185
186 inline void convert(const char* from, std::wstring& to);
187
188 inline void convert(const wchar_t* from, std::string& to);
189
190 // value types same -----------------------------------------------------------------//
191
192 // char with codecvt
193
194 inline void convert(const char* from, const char* from_end, std::string& to, codecvt_type const&)
195 {
196 BOOST_ASSERT(from);
197 BOOST_ASSERT(from_end);
198 to.append(from, from_end);
199 }
200
201 inline void convert(const char* from, std::string& to, codecvt_type const&)
202 {
203 BOOST_ASSERT(from);
204 to += from;
205 }
206
207 // wchar_t with codecvt
208
209 inline void convert(const wchar_t* from, const wchar_t* from_end, std::wstring& to, codecvt_type const&)
210 {
211 BOOST_ASSERT(from);
212 BOOST_ASSERT(from_end);
213 to.append(from, from_end);
214 }
215
216 inline void convert(const wchar_t* from, std::wstring& to, codecvt_type const&)
217 {
218 BOOST_ASSERT(from);
219 to += from;
220 }
221
222 // char without codecvt
223
224 inline void convert(const char* from, const char* from_end, std::string& to)
225 {
226 BOOST_ASSERT(from);
227 BOOST_ASSERT(from_end);
228 to.append(from, from_end);
229 }
230
231 inline void convert(const char* from, std::string& to)
232 {
233 BOOST_ASSERT(from);
234 to += from;
235 }
236
237 // wchar_t without codecvt
238
239 inline void convert(const wchar_t* from, const wchar_t* from_end, std::wstring& to)
240 {
241 BOOST_ASSERT(from);
242 BOOST_ASSERT(from_end);
243 to.append(from, from_end);
244 }
245
246 inline void convert(const wchar_t* from, std::wstring& to)
247 {
248 BOOST_ASSERT(from);
249 to += from;
250 }
251
252 // Source dispatch -----------------------------------------------------------------//
253
254 // contiguous containers with codecvt
255 template< class U >
256 inline void dispatch(std::string const& c, U& to, codecvt_type const& cvt)
257 {
258 if (!c.empty())
259 convert(&*c.begin(), &*c.begin() + c.size(), to, cvt);
260 }
261 template< class U >
262 inline void dispatch(std::wstring const& c, U& to, codecvt_type const& cvt)
263 {
264 if (!c.empty())
265 convert(&*c.begin(), &*c.begin() + c.size(), to, cvt);
266 }
267 template< class U >
268 inline void dispatch(std::vector< char > const& c, U& to, codecvt_type const& cvt)
269 {
270 if (!c.empty())
271 convert(&*c.begin(), &*c.begin() + c.size(), to, cvt);
272 }
273 template< class U >
274 inline void dispatch(std::vector< wchar_t > const& c, U& to, codecvt_type const& cvt)
275 {
276 if (!c.empty())
277 convert(&*c.begin(), &*c.begin() + c.size(), to, cvt);
278 }
279
280 // contiguous containers without codecvt
281 template< class U >
282 inline void dispatch(std::string const& c, U& to)
283 {
284 if (!c.empty())
285 convert(&*c.begin(), &*c.begin() + c.size(), to);
286 }
287 template< class U >
288 inline void dispatch(std::wstring const& c, U& to)
289 {
290 if (!c.empty())
291 convert(&*c.begin(), &*c.begin() + c.size(), to);
292 }
293 template< class U >
294 inline void dispatch(std::vector< char > const& c, U& to)
295 {
296 if (!c.empty())
297 convert(&*c.begin(), &*c.begin() + c.size(), to);
298 }
299 template< class U >
300 inline void dispatch(std::vector< wchar_t > const& c, U& to)
301 {
302 if (!c.empty())
303 convert(&*c.begin(), &*c.begin() + c.size(), to);
304 }
305
306 // non-contiguous containers with codecvt
307 template< class Container, class U >
308 inline
309 // disable_if aids broken compilers (IBM, old GCC, etc.) and is harmless for
310 // conforming compilers. Replace by plain "void" at some future date (2012?)
311 typename boost::disable_if< boost::is_array< Container >, void >::type
312 dispatch(Container const& c, U& to, codecvt_type const& cvt)
313 {
314 if (!c.empty())
315 {
316 std::basic_string< typename Container::value_type > s(c.begin(), c.end());
317 convert(s.c_str(), s.c_str() + s.size(), to, cvt);
318 }
319 }
320
321 // c_str
322 template< class T, class U >
323 inline void dispatch(T* const& c_str, U& to, codecvt_type const& cvt)
324 {
325 // std::cout << "dispatch() const T *\n";
326 BOOST_ASSERT(c_str);
327 convert(c_str, to, cvt);
328 }
329
330 // Note: there is no dispatch on C-style arrays because the array may
331 // contain a string smaller than the array size.
332
333 BOOST_FILESYSTEM_DECL
334 void dispatch(directory_entry const& de,
335 #ifdef BOOST_WINDOWS_API
336 std::wstring& to,
337 #else
338 std::string& to,
339 #endif
340 codecvt_type const&);
341
342 // non-contiguous containers without codecvt
343 template< class Container, class U >
344 inline
345 // disable_if aids broken compilers (IBM, old GCC, etc.) and is harmless for
346 // conforming compilers. Replace by plain "void" at some future date (2012?)
347 typename boost::disable_if< boost::is_array< Container >, void >::type
348 dispatch(Container const& c, U& to)
349 {
350 if (!c.empty())
351 {
352 std::basic_string< typename Container::value_type > seq(c.begin(), c.end());
353 convert(seq.c_str(), seq.c_str() + seq.size(), to);
354 }
355 }
356
357 // c_str
358 template< class T, class U >
359 inline void dispatch(T* const& c_str, U& to)
360 {
361 // std::cout << "dispatch() const T *\n";
362 BOOST_ASSERT(c_str);
363 convert(c_str, to);
364 }
365
366 // Note: there is no dispatch on C-style arrays because the array may
367 // contain a string smaller than the array size.
368
369 BOOST_FILESYSTEM_DECL
370 void dispatch(directory_entry const& de,
371 #ifdef BOOST_WINDOWS_API
372 std::wstring& to
373 #else
374 std::string& to
375 #endif
376 );
377
378 } // namespace path_traits
379 } // namespace filesystem
380 } // namespace boost
381
382 #include <boost/filesystem/detail/footer.hpp>
383
384 #endif // BOOST_FILESYSTEM_PATH_TRAITS_HPP