]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/ext/string_conversions.h
string_conversions.cc: Remove.
[thirdparty/gcc.git] / libstdc++-v3 / include / ext / string_conversions.h
1 // String Conversions -*- C++ -*-
2
3 // Copyright (C) 2008 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 2, or (at your option)
9 // any later version.
10
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.
15
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 // USA.
20
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29
30 #ifndef _STRING_CONVERSIONS_H
31 #define _STRING_CONVERSIONS_H 1
32
33 #pragma GCC system_header
34
35 #include <ext/numeric_traits.h>
36 #include <bits/functexcept.h>
37 #include <cstddef>
38 #include <cstdlib>
39 #include <cwchar>
40 #include <cstdio>
41 #include <cerrno>
42
43 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
44
45 // Helper for all the sto* functions.
46 template<typename _TRet, typename _Ret = _TRet, typename _CharT,
47 typename... _Base>
48 _Ret
49 __stoa(_TRet (*__convf) (const _CharT*, _CharT**, _Base...),
50 const char* __name, const _CharT* __str, std::size_t* __idx,
51 _Base... __base)
52 {
53 _Ret __ret;
54
55 _CharT* __endptr;
56 errno = 0;
57 const _TRet __tmp = __convf(__str, &__endptr, __base...);
58
59 if (__endptr == __str)
60 std::__throw_invalid_argument(__name);
61 else if (errno == ERANGE
62 || (std::__are_same<_Ret, int>::__value
63 && (__tmp < __numeric_traits<int>::__min
64 || __tmp > __numeric_traits<int>::__max)))
65 std::__throw_out_of_range(__name);
66 else
67 __ret = __tmp;
68
69 if (__idx)
70 *__idx = __endptr - __str;
71
72 return __ret;
73 }
74
75 // Helper for the to_string / to_wstring functions.
76 template<typename _String, typename _CharT = typename _String::value_type>
77 _String
78 __to_xstring(int (*__convf) (_CharT*, std::size_t, const _CharT*,
79 __builtin_va_list), std::size_t __n,
80 const _CharT* __fmt, ...)
81 {
82 // XXX Eventually the result will be constructed in place in
83 // the C++0x string, likely with the help of internal hooks.
84 _CharT* __s = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
85 * __n));
86
87 __builtin_va_list __args;
88 __builtin_va_start(__args, __fmt);
89
90 const int __len = __convf(__s, __n, __fmt, __args);
91
92 __builtin_va_end(__args);
93
94 return _String(__s, __s + __len);
95 }
96
97 _GLIBCXX_END_NAMESPACE
98
99 #endif // _STRING_CONVERSIONS_H