Merged trunk at revision 161680 into branch.
[official-gcc.git] / libstdc++-v3 / include / ext / string_conversions.h
bloba4276f30ba2cfe86fb3e1b0c97445716dbcc3401
1 // String Conversions -*- C++ -*-
3 // Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 #ifndef _STRING_CONVERSIONS_H
26 #define _STRING_CONVERSIONS_H 1
28 #pragma GCC system_header
30 #include <bits/c++config.h>
31 #include <ext/numeric_traits.h>
32 #include <bits/functexcept.h>
33 #include <cstdlib>
34 #include <cwchar>
35 #include <cstdio>
36 #include <cerrno>
38 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
40 // Helper for all the sto* functions.
41 template<typename _TRet, typename _Ret = _TRet, typename _CharT,
42 typename... _Base>
43 _Ret
44 __stoa(_TRet (*__convf) (const _CharT*, _CharT**, _Base...),
45 const char* __name, const _CharT* __str, std::size_t* __idx,
46 _Base... __base)
48 _Ret __ret;
50 _CharT* __endptr;
51 errno = 0;
52 const _TRet __tmp = __convf(__str, &__endptr, __base...);
54 if (__endptr == __str)
55 std::__throw_invalid_argument(__name);
56 else if (errno == ERANGE
57 || (std::__are_same<_Ret, int>::__value
58 && (__tmp < __numeric_traits<int>::__min
59 || __tmp > __numeric_traits<int>::__max)))
60 std::__throw_out_of_range(__name);
61 else
62 __ret = __tmp;
64 if (__idx)
65 *__idx = __endptr - __str;
67 return __ret;
70 // Helper for the to_string / to_wstring functions.
71 template<typename _String, typename _CharT = typename _String::value_type>
72 _String
73 __to_xstring(int (*__convf) (_CharT*, std::size_t, const _CharT*,
74 __builtin_va_list), std::size_t __n,
75 const _CharT* __fmt, ...)
77 // XXX Eventually the result will be constructed in place in
78 // the C++0x string, likely with the help of internal hooks.
79 _CharT* __s = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
80 * __n));
82 __builtin_va_list __args;
83 __builtin_va_start(__args, __fmt);
85 const int __len = __convf(__s, __n, __fmt, __args);
87 __builtin_va_end(__args);
89 return _String(__s, __s + __len);
92 _GLIBCXX_END_NAMESPACE
94 #endif // _STRING_CONVERSIONS_H