1 // Locale support -*- C++ -*-
3 // Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
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)
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 // 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,
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.
30 /** @file locale_classes.tcc
31 * This is an internal header file, included by other library headers.
32 * You should not attempt to use it directly.
36 // ISO C++ 14882: 22.1 Locales
39 #ifndef _LOCALE_CLASSES_TCC
40 #define _LOCALE_CLASSES_TCC 1
42 #pragma GCC system_header
44 _GLIBCXX_BEGIN_NAMESPACE(std)
46 template<typename _Facet>
48 locale(const locale& __other, _Facet* __f)
50 _M_impl = new _Impl(*__other._M_impl, 1);
53 { _M_impl->_M_install_facet(&_Facet::id, __f); }
56 _M_impl->_M_remove_reference();
57 __throw_exception_again;
59 delete [] _M_impl->_M_names[0];
60 _M_impl->_M_names[0] = 0; // Unnamed.
63 template<typename _Facet>
66 combine(const locale& __other) const
68 _Impl* __tmp = new _Impl(*_M_impl, 1);
71 __tmp->_M_replace_facet(__other._M_impl, &_Facet::id);
75 __tmp->_M_remove_reference();
76 __throw_exception_again;
81 template<typename _CharT, typename _Traits, typename _Alloc>
84 operator()(const basic_string<_CharT, _Traits, _Alloc>& __s1,
85 const basic_string<_CharT, _Traits, _Alloc>& __s2) const
87 typedef std::collate<_CharT> __collate_type;
88 const __collate_type& __collate = use_facet<__collate_type>(*this);
89 return (__collate.compare(__s1.data(), __s1.data() + __s1.length(),
90 __s2.data(), __s2.data() + __s2.length()) < 0);
94 template<typename _Facet>
96 has_facet(const locale& __loc) throw()
98 const size_t __i = _Facet::id._M_id();
99 const locale::facet** __facets = __loc._M_impl->_M_facets;
100 return (__i < __loc._M_impl->_M_facets_size
102 && dynamic_cast<const _Facet*>(__facets[__i]));
104 && static_cast<const _Facet*>(__facets[__i]));
108 template<typename _Facet>
110 use_facet(const locale& __loc)
112 const size_t __i = _Facet::id._M_id();
113 const locale::facet** __facets = __loc._M_impl->_M_facets;
114 if (__i >= __loc._M_impl->_M_facets_size || !__facets[__i])
117 return dynamic_cast<const _Facet&>(*__facets[__i]);
119 return static_cast<const _Facet&>(*__facets[__i]);
124 // Generic version does nothing.
125 template<typename _CharT>
127 collate<_CharT>::_M_compare(const _CharT*, const _CharT*) const
130 // Generic version does nothing.
131 template<typename _CharT>
133 collate<_CharT>::_M_transform(_CharT*, const _CharT*, size_t) const
136 template<typename _CharT>
139 do_compare(const _CharT* __lo1, const _CharT* __hi1,
140 const _CharT* __lo2, const _CharT* __hi2) const
142 // strcoll assumes zero-terminated strings so we make a copy
143 // and then put a zero at the end.
144 const string_type __one(__lo1, __hi1);
145 const string_type __two(__lo2, __hi2);
147 const _CharT* __p = __one.c_str();
148 const _CharT* __pend = __one.data() + __one.length();
149 const _CharT* __q = __two.c_str();
150 const _CharT* __qend = __two.data() + __two.length();
152 // strcoll stops when it sees a nul character so we break
153 // the strings into zero-terminated substrings and pass those
157 const int __res = _M_compare(__p, __q);
161 __p += char_traits<_CharT>::length(__p);
162 __q += char_traits<_CharT>::length(__q);
163 if (__p == __pend && __q == __qend)
165 else if (__p == __pend)
167 else if (__q == __qend)
175 template<typename _CharT>
176 typename collate<_CharT>::string_type
178 do_transform(const _CharT* __lo, const _CharT* __hi) const
182 // strxfrm assumes zero-terminated strings so we make a copy
183 const string_type __str(__lo, __hi);
185 const _CharT* __p = __str.c_str();
186 const _CharT* __pend = __str.data() + __str.length();
188 size_t __len = (__hi - __lo) * 2;
190 _CharT* __c = new _CharT[__len];
194 // strxfrm stops when it sees a nul character so we break
195 // the string into zero-terminated substrings and pass those
199 // First try a buffer perhaps big enough.
200 size_t __res = _M_transform(__c, __p, __len);
201 // If the buffer was not large enough, try again with the
206 delete [] __c, __c = 0;
207 __c = new _CharT[__len];
208 __res = _M_transform(__c, __p, __len);
211 __ret.append(__c, __res);
212 __p += char_traits<_CharT>::length(__p);
217 __ret.push_back(_CharT());
223 __throw_exception_again;
231 template<typename _CharT>
234 do_hash(const _CharT* __lo, const _CharT* __hi) const
236 unsigned long __val = 0;
237 for (; __lo < __hi; ++__lo)
239 *__lo + ((__val << 7)
240 | (__val >> (__gnu_cxx::__numeric_traits<unsigned long>::
242 return static_cast<long>(__val);
245 // Inhibit implicit instantiations for required instantiations,
246 // which are defined via explicit instantiations elsewhere.
247 // NB: This syntax is a GNU extension.
248 #if _GLIBCXX_EXTERN_TEMPLATE
249 extern template class collate<char>;
250 extern template class collate_byname<char>;
254 use_facet<collate<char> >(const locale&);
258 has_facet<collate<char> >(const locale&);
260 #ifdef _GLIBCXX_USE_WCHAR_T
261 extern template class collate<wchar_t>;
262 extern template class collate_byname<wchar_t>;
265 const collate<wchar_t>&
266 use_facet<collate<wchar_t> >(const locale&);
270 has_facet<collate<wchar_t> >(const locale&);
274 _GLIBCXX_END_NAMESPACE