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>
51 #include <bits/atomicity.h>
58 // 22.1.3 Convenience interfaces
59 template<typename _CharT>
61 isspace(_CharT, const locale&);
63 template<typename _CharT>
65 isprint(_CharT, const locale&);
67 template<typename _CharT>
69 iscntrl(_CharT, const locale&);
71 template<typename _CharT>
73 isupper(_CharT, const locale&);
75 template<typename _CharT>
77 islower(_CharT, const locale&);
79 template<typename _CharT>
81 isalpha(_CharT, const locale&);
83 template<typename _CharT>
85 isdigit(_CharT, const locale&);
87 template<typename _CharT>
89 ispunct(_CharT, const locale&);
91 template<typename _CharT>
93 isxdigit(_CharT, const locale&);
95 template<typename _CharT>
97 isalnum(_CharT, const locale&);
99 template<typename _CharT>
101 isgraph(_CharT, const locale&);
103 template<typename _CharT>
105 toupper(_CharT, const locale&);
107 template<typename _CharT>
109 tolower(_CharT, const locale&);
112 // 22.2.1 and 22.2.1.3 ctype
114 template<typename _CharT>
116 template<> class ctype<char>;
117 #ifdef _GLIBCPP_USE_WCHAR_T
118 template<> class ctype<wchar_t>;
120 template<typename _CharT>
122 // NB: Specialized for char and wchar_t in locale_facets.h.
126 template<typename _InternT, typename _ExternT, typename _StateT>
128 template<> class codecvt<char, char, mbstate_t>;
129 #ifdef _GLIBCPP_USE_WCHAR_T
130 template<> class codecvt<wchar_t, char, mbstate_t>;
132 template<typename _InternT, typename _ExternT, typename _StateT>
133 class codecvt_byname;
135 // 22.2.2 and 22.2.3 numeric
136 template<typename _CharT, typename _InIter = istreambuf_iterator<_CharT> >
138 template<typename _CharT, typename _OutIter = ostreambuf_iterator<_CharT> >
140 template<typename _CharT> class numpunct;
141 template<typename _CharT> class numpunct_byname;
144 template<typename _CharT>
146 template<typename _CharT> class
149 // 22.2.5 date and time
151 template<typename _CharT, typename _InIter = istreambuf_iterator<_CharT> >
153 template<typename _CharT, typename _InIter = istreambuf_iterator<_CharT> >
154 class time_get_byname;
155 template<typename _CharT, typename _OutIter = ostreambuf_iterator<_CharT> >
157 template<typename _CharT, typename _OutIter = ostreambuf_iterator<_CharT> >
158 class time_put_byname;
162 template<typename _CharT, typename _InIter = istreambuf_iterator<_CharT> >
164 template<typename _CharT, typename _OutIter = ostreambuf_iterator<_CharT> >
166 template<typename _CharT, bool _Intl = false>
168 template<typename _CharT, bool _Intl = false>
169 class moneypunct_byname;
171 // 22.2.7 message retrieval
173 template<typename _CharT>
175 template<typename _CharT>
176 class messages_byname;
178 // 22.1.1 Class locale
183 typedef unsigned int category;
185 // Forward decls and friends:
193 template<typename _Facet>
195 use_facet(const locale&);
197 template<typename _Facet>
199 has_facet(const locale&) throw();
202 // NB: Order must match _S_facet_categories definition in locale.cc
203 static const category none = 0;
204 static const category ctype = 1L << 0;
205 static const category numeric = 1L << 1;
206 static const category collate = 1L << 2;
207 static const category time = 1L << 3;
208 static const category monetary = 1L << 4;
209 static const category messages = 1L << 5;
210 static const category all = (ctype | numeric | collate |
211 time | monetary | messages);
213 // Construct/copy/destroy:
216 locale(const locale& __other) throw();
219 locale(const char* __s);
221 locale(const locale& __base, const char* __s, category __cat);
223 locale(const locale& __base, const locale& __add, category __cat);
225 template<typename _Facet>
226 locale(const locale& __other, _Facet* __f);
231 operator=(const locale& __other) throw();
233 template<typename _Facet>
235 combine(const locale& __other) const;
237 // Locale operations:
242 operator==(const locale& __other) const throw ();
245 operator!=(const locale& __other) const throw ()
246 { return !(this->operator==(__other)); }
248 template<typename _Char, typename _Traits, typename _Alloc>
250 operator()(const basic_string<_Char, _Traits, _Alloc>& __s1,
251 const basic_string<_Char, _Traits, _Alloc>& __s2) const;
253 // Global locale objects:
255 global(const locale&);
261 // The (shared) implementation
264 // The "C" reference locale
265 static _Impl* _S_classic;
267 // Current global reference locale
268 static _Impl* _S_global;
270 // Number of standard categories. For C++, these categories are
271 // collate, ctype, monetary, numeric, time, and messages. These
272 // directly correspond to ISO C99 macros LC_COLLATE, LC_CTYPE,
273 // LC_MONETARY, LC_NUMERIC, and LC_TIME. In addition, POSIX (IEEE
274 // 1003.1-2001) specifies LC_MESSAGES.
275 static const size_t _S_categories_size = 6;
277 // In addition to the standard categories, the underlying
278 // operating system is allowed to define extra LC_*
279 // macros. For GNU systems, the following are also valid:
280 // LC_PAPER, LC_NAME, LC_ADDRESS, LC_TELEPHONE, LC_MEASUREMENT,
281 // and LC_IDENTIFICATION.
282 static const size_t _S_extra_categories_size = _GLIBCPP_NUM_CATEGORIES;
284 // Names of underlying locale categories.
285 // NB: locale::global() has to know how to modify all the
286 // underlying categories, not just the ones required by the C++
288 static const char* _S_categories[_S_categories_size
289 + _S_extra_categories_size];
292 locale(_Impl*) throw();
302 _S_normalize_category(category);
305 _M_coalesce(const locale& __base, const locale& __add, category __cat);
309 // Implementation object for locale
315 friend class locale::facet;
317 template<typename _Facet>
319 use_facet(const locale&);
321 template<typename _Facet>
323 has_facet(const locale&) throw();
327 _Atomic_word _M_references;
329 size_t _M_facets_size;
331 char* _M_names[_S_categories_size
332 + _S_extra_categories_size];
333 static const locale::id* const _S_id_ctype[];
334 static const locale::id* const _S_id_numeric[];
335 static const locale::id* const _S_id_collate[];
336 static const locale::id* const _S_id_time[];
337 static const locale::id* const _S_id_monetary[];
338 static const locale::id* const _S_id_messages[];
339 static const locale::id* const* const _S_facet_categories[];
342 _M_add_reference() throw()
343 { __atomic_add(&_M_references, 1); }
346 _M_remove_reference() throw()
348 if (__exchange_and_add(&_M_references, -1) == 1)
357 _Impl(const _Impl&, size_t);
358 _Impl(const char*, size_t);
359 _Impl(facet**, size_t, bool);
363 _Impl(const _Impl&); // Not defined.
366 operator=(const _Impl&); // Not defined.
373 __ret && __i < _S_categories_size + _S_extra_categories_size - 1;
375 __ret &= (strcmp(_M_names[__i], _M_names[__i + 1]) == 0);
380 _M_replace_categories(const _Impl*, category);
383 _M_replace_category(const _Impl*, const locale::id* const*);
386 _M_replace_facet(const _Impl*, const locale::id*);
389 _M_install_facet(const locale::id*, facet*);
391 template<typename _Facet>
393 _M_init_facet(_Facet* __facet)
394 { _M_install_facet(&_Facet::id, __facet); }
397 template<typename _Facet>
398 locale::locale(const locale& __other, _Facet* __f)
400 _M_impl = new _Impl(*__other._M_impl, 1);
401 _M_impl->_M_install_facet(&_Facet::id, __f);
403 __i < _S_categories_size + _S_extra_categories_size; ++__i)
405 delete [] _M_impl->_M_names[__i];
406 char* __new = new char[2];
408 _M_impl->_M_names[__i] = __new;
412 // 22.1.1.1.2 Class locale::facet
417 friend class locale::_Impl;
419 _Atomic_word _M_references;
422 // Contains data from the underlying "C" library for default "C"
423 // or "POSIX" locale.
424 static __c_locale _S_c_locale;
427 facet(size_t __refs = 0) throw();
433 _S_create_c_locale(__c_locale& __cloc, const char* __s,
434 __c_locale __old = 0);
437 _S_clone_c_locale(__c_locale& __cloc);
440 _S_destroy_c_locale(__c_locale& __cloc);
444 _M_add_reference() throw();
447 _M_remove_reference() throw();
449 facet(const facet&); // Not defined.
452 operator=(const facet&); // Not defined.
456 // 22.1.1.1.3 Class locale::id
461 friend class locale::_Impl;
462 template<typename _Facet>
464 use_facet(const locale&);
465 template<typename _Facet>
467 has_facet(const locale&) throw ();
469 // NB: There is no accessor for _M_index because it may be used
470 // before the constructor is run; the effect of calling a member
471 // function (even an inline) would be undefined.
472 mutable size_t _M_index;
474 // Last id number assigned.
475 static _Atomic_word _S_highwater;
478 operator=(const id&); // Not defined.
480 id(const id&); // Not defined.
483 // NB: This class is always a static data member, and thus can be
484 // counted on to be zero-initialized.
491 _M_index = 1 + __exchange_and_add(&_S_highwater, 1);
496 template<typename _Facet>
498 use_facet(const locale& __loc);
500 template<typename _Facet>
502 has_facet(const locale& __loc) throw();