1 // Locale support -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
32 // ISO C++ 14882: 22.1 Locales
36 * This is an internal header file, included by other library headers.
37 * You should not attempt to use it directly.
40 #ifndef _CPP_BITS_LOCCORE_H
41 #define _CPP_BITS_LOCCORE_H 1
43 #pragma GCC system_header
45 #include <bits/c++config.h>
46 #include <bits/c++locale.h> // Defines __c_locale, config-specific includes
47 #include <climits> // For CHAR_BIT
48 #include <cctype> // For isspace, etc.
49 #include <string> // For string
50 #include <bits/functexcept.h>
52 #include <bits/atomicity.h>
56 // NB: Don't instantiate required wchar_t facets if no wchar_t support.
57 #ifdef _GLIBCPP_USE_WCHAR_T
58 # define _GLIBCPP_NUM_FACETS 28
60 # define _GLIBCPP_NUM_FACETS 14
64 template<typename _Tp, typename _Alloc>
68 // 22.1.3 Convenience interfaces
69 template<typename _CharT>
71 isspace(_CharT, const locale&);
73 template<typename _CharT>
75 isprint(_CharT, const locale&);
77 template<typename _CharT>
79 iscntrl(_CharT, const locale&);
81 template<typename _CharT>
83 isupper(_CharT, const locale&);
85 template<typename _CharT>
87 islower(_CharT, const locale&);
89 template<typename _CharT>
91 isalpha(_CharT, const locale&);
93 template<typename _CharT>
95 isdigit(_CharT, const locale&);
97 template<typename _CharT>
99 ispunct(_CharT, const locale&);
101 template<typename _CharT>
103 isxdigit(_CharT, const locale&);
105 template<typename _CharT>
107 isalnum(_CharT, const locale&);
109 template<typename _CharT>
111 isgraph(_CharT, const locale&);
113 template<typename _CharT>
115 toupper(_CharT, const locale&);
117 template<typename _CharT>
119 tolower(_CharT, const locale&);
122 // 22.2.1 and 22.2.1.3 ctype
124 template<typename _CharT>
126 template<> class ctype<char>;
127 #ifdef _GLIBCPP_USE_WCHAR_T
128 template<> class ctype<wchar_t>;
130 template<typename _CharT>
132 // NB: Specialized for char and wchar_t in locale_facets.h.
136 template<typename _InternT, typename _ExternT, typename _StateT>
138 template<> class codecvt<char, char, mbstate_t>;
139 #ifdef _GLIBCPP_USE_WCHAR_T
140 template<> class codecvt<wchar_t, char, mbstate_t>;
142 template<typename _InternT, typename _ExternT, typename _StateT>
143 class codecvt_byname;
145 // 22.2.2 and 22.2.3 numeric
146 template<typename _CharT, typename _InIter = istreambuf_iterator<_CharT> >
148 template<typename _CharT, typename _OutIter = ostreambuf_iterator<_CharT> >
150 template<typename _CharT> class numpunct;
151 template<typename _CharT> class numpunct_byname;
154 template<typename _CharT>
156 template<typename _CharT> class
159 // 22.2.5 date and time
161 template<typename _CharT, typename _InIter = istreambuf_iterator<_CharT> >
163 template<typename _CharT, typename _InIter = istreambuf_iterator<_CharT> >
164 class time_get_byname;
165 template<typename _CharT, typename _OutIter = ostreambuf_iterator<_CharT> >
167 template<typename _CharT, typename _OutIter = ostreambuf_iterator<_CharT> >
168 class time_put_byname;
172 template<typename _CharT, typename _InIter = istreambuf_iterator<_CharT> >
174 template<typename _CharT, typename _OutIter = ostreambuf_iterator<_CharT> >
176 template<typename _CharT, bool _Intl = false>
178 template<typename _CharT, bool _Intl = false>
179 class moneypunct_byname;
181 // 22.2.7 message retrieval
183 template<typename _CharT>
185 template<typename _CharT>
186 class messages_byname;
188 // 22.1.1 Class locale
193 typedef unsigned int category;
195 // Forward decls and friends:
203 template<typename _Facet>
205 use_facet(const locale&);
207 template<typename _Facet>
209 has_facet(const locale&) throw();
212 // NB: Order must match _S_facet_categories definition in locale.cc
213 static const category none = 0;
214 static const category ctype = 1L << 0;
215 static const category numeric = 1L << 1;
216 static const category collate = 1L << 2;
217 static const category time = 1L << 3;
218 static const category monetary = 1L << 4;
219 static const category messages = 1L << 5;
220 static const category all = (collate | ctype | monetary |
221 numeric | time | messages);
223 // Construct/copy/destroy:
226 locale(const locale& __other) throw();
229 locale(const char* __std_name);
231 locale(const locale& __base, const char* __s, category __cat);
233 locale(const locale& __base, const locale& __add, category __cat);
235 template<typename _Facet>
236 locale(const locale& __other, _Facet* __f);
241 operator=(const locale& __other) throw();
243 template<typename _Facet>
245 combine(const locale& __other) const;
247 // Locale operations:
252 operator==(const locale& __other) const throw ();
255 operator!=(const locale& __other) const throw ()
256 { return !(this->operator==(__other)); }
258 template<typename _Char, typename _Traits, typename _Alloc>
260 operator()(const basic_string<_Char, _Traits, _Alloc>& __s1,
261 const basic_string<_Char, _Traits, _Alloc>& __s2) const;
263 // Global locale objects:
265 global(const locale&);
271 // The (shared) implementation
274 // The "C" reference locale
275 static _Impl* _S_classic;
277 // Current global reference locale
278 static _Impl* _S_global;
280 static const size_t _S_num_categories = 6;
281 static const size_t _S_num_facets = _GLIBCPP_NUM_FACETS;
284 locale(_Impl*) throw();
294 _S_normalize_category(category);
297 _M_coalesce(const locale& __base, const locale& __add, category __cat);
301 // Implementation object for locale
306 typedef vector<facet*, allocator<facet*> > __vec_facet;
310 friend class locale::facet;
312 template<typename _Facet>
314 use_facet(const locale&);
316 template<typename _Facet>
318 has_facet(const locale&) throw();
322 _Atomic_word _M_references;
323 __vec_facet* _M_facets;
324 string _M_names[_S_num_categories];
325 static const locale::id* const _S_id_ctype[];
326 static const locale::id* const _S_id_numeric[];
327 static const locale::id* const _S_id_collate[];
328 static const locale::id* const _S_id_time[];
329 static const locale::id* const _S_id_monetary[];
330 static const locale::id* const _S_id_messages[];
331 static const locale::id* const* const _S_facet_categories[];
334 _M_add_reference() throw()
335 { __atomic_add(&_M_references, 1); }
338 _M_remove_reference() throw()
340 if (__exchange_and_add(&_M_references, -1) == 1)
349 _Impl(const _Impl&, size_t);
350 _Impl(string __name, size_t);
357 for (size_t i = 0; i < _S_num_categories - 1; ++i)
358 __ret &= _M_names[i] == _M_names[i + 1];
363 _M_replace_categories(const _Impl*, category);
366 _M_replace_category(const _Impl*, const locale::id* const*);
369 _M_replace_facet(const _Impl*, const locale::id*);
372 _M_install_facet(const locale::id*, facet*);
374 template<typename _Facet>
376 _M_init_facet(_Facet* __facet)
377 { _M_install_facet(&_Facet::id, __facet); }
380 template<typename _Facet>
381 locale::locale(const locale& __other, _Facet* __f)
383 _M_impl = new _Impl(*__other._M_impl, 1);
384 _M_impl->_M_install_facet(&_Facet::id, __f);
385 for (size_t __i = 0; __i < _S_num_categories; ++__i)
386 _M_impl->_M_names[__i] = "*";
389 // 22.1.1.1.2 Class locale::facet
393 friend class locale::_Impl;
394 friend class __enc_traits;
397 _Atomic_word _M_references;
400 // Contains data from the underlying "C" library for default "C"
401 // or "POSIX" locale.
402 static __c_locale _S_c_locale;
405 facet(size_t __refs = 0) throw();
411 _S_create_c_locale(__c_locale& __cloc, const char* __s);
414 _S_clone_c_locale(__c_locale& __cloc);
417 _S_destroy_c_locale(__c_locale& __cloc);
421 _M_add_reference() throw();
424 _M_remove_reference() throw();
426 facet(const facet&); // Not defined.
429 operator=(const facet&); // Not defined.
433 // 22.1.1.1.3 Class locale::id
438 friend class locale::_Impl;
439 template<typename _Facet>
441 use_facet(const locale&);
442 template<typename _Facet>
444 has_facet(const locale&) throw ();
446 // NB: There is no accessor for _M_index because it may be used
447 // before the constructor is run; the effect of calling a member
448 // function (even an inline) would be undefined.
449 mutable size_t _M_index;
451 // Last id number assigned
452 static _Atomic_word _S_highwater;
455 operator=(const id&); // not defined
457 id(const id&); // not defined
460 // NB: This class is always a static data member, and thus can be
461 // counted on to be zero-initialized.
465 template<typename _Facet>
467 use_facet(const locale& __loc);
469 template<typename _Facet>
471 has_facet(const locale& __loc) throw();