1 // Locale support -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003
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_LOCALE_CLASSES_H
41 #define _CPP_BITS_LOCALE_CLASSES_H 1
43 #pragma GCC system_header
45 #include <bits/localefwd.h>
46 #include <cstring> // For strcmp.
48 #include <bits/atomicity.h>
52 // 22.1.1 Class locale
57 typedef unsigned int category;
59 // Forward decls and friends:
67 template<typename _Facet>
69 use_facet(const locale&);
71 template<typename _Facet>
73 has_facet(const locale&) throw();
76 // NB: Order must match _S_facet_categories definition in locale.cc
77 static const category none = 0;
78 static const category ctype = 1L << 0;
79 static const category numeric = 1L << 1;
80 static const category collate = 1L << 2;
81 static const category time = 1L << 3;
82 static const category monetary = 1L << 4;
83 static const category messages = 1L << 5;
84 static const category all = (ctype | numeric | collate |
85 time | monetary | messages);
87 // Construct/copy/destroy:
90 locale(const locale& __other) throw();
93 locale(const char* __s);
95 locale(const locale& __base, const char* __s, category __cat);
97 locale(const locale& __base, const locale& __add, category __cat);
99 template<typename _Facet>
100 locale(const locale& __other, _Facet* __f);
105 operator=(const locale& __other) throw();
107 template<typename _Facet>
109 combine(const locale& __other) const;
111 // Locale operations:
116 operator==(const locale& __other) const throw ();
119 operator!=(const locale& __other) const throw ()
120 { return !(this->operator==(__other)); }
122 template<typename _Char, typename _Traits, typename _Alloc>
124 operator()(const basic_string<_Char, _Traits, _Alloc>& __s1,
125 const basic_string<_Char, _Traits, _Alloc>& __s2) const;
127 // Global locale objects:
129 global(const locale&);
135 // The (shared) implementation
138 // The "C" reference locale
139 static _Impl* _S_classic;
141 // Current global locale
142 static _Impl* _S_global;
144 // Number of standard categories. For C++, these categories are
145 // collate, ctype, monetary, numeric, time, and messages. These
146 // directly correspond to ISO C99 macros LC_COLLATE, LC_CTYPE,
147 // LC_MONETARY, LC_NUMERIC, and LC_TIME. In addition, POSIX (IEEE
148 // 1003.1-2001) specifies LC_MESSAGES.
149 static const size_t _S_categories_size = 6;
151 // In addition to the standard categories, the underlying
152 // operating system is allowed to define extra LC_*
153 // macros. For GNU systems, the following are also valid:
154 // LC_PAPER, LC_NAME, LC_ADDRESS, LC_TELEPHONE, LC_MEASUREMENT,
155 // and LC_IDENTIFICATION.
156 static const size_t _S_extra_categories_size = _GLIBCPP_NUM_CATEGORIES;
158 // Names of underlying locale categories.
159 // NB: locale::global() has to know how to modify all the
160 // underlying categories, not just the ones required by the C++
162 static const char* _S_categories[_S_categories_size
163 + _S_extra_categories_size];
166 locale(_Impl*) throw();
176 _S_normalize_category(category);
179 _M_coalesce(const locale& __base, const locale& __add, category __cat);
183 // Implementation object for locale
189 friend class locale::facet;
191 template<typename _Facet>
193 use_facet(const locale&);
195 template<typename _Facet>
197 has_facet(const locale&) throw();
201 _Atomic_word _M_references;
202 const facet** _M_facets;
203 size_t _M_facets_size;
205 char* _M_names[_S_categories_size
206 + _S_extra_categories_size];
207 static const locale::id* const _S_id_ctype[];
208 static const locale::id* const _S_id_numeric[];
209 static const locale::id* const _S_id_collate[];
210 static const locale::id* const _S_id_time[];
211 static const locale::id* const _S_id_monetary[];
212 static const locale::id* const _S_id_messages[];
213 static const locale::id* const* const _S_facet_categories[];
216 _M_add_reference() throw()
217 { __atomic_add(&_M_references, 1); }
220 _M_remove_reference() throw()
222 if (__exchange_and_add(&_M_references, -1) == 1)
231 _Impl(const _Impl&, size_t);
232 _Impl(const char*, size_t);
233 _Impl(facet**, size_t, bool);
237 _Impl(const _Impl&); // Not defined.
240 operator=(const _Impl&); // Not defined.
247 __ret && __i < _S_categories_size + _S_extra_categories_size - 1;
249 __ret &= (strcmp(_M_names[__i], _M_names[__i + 1]) == 0);
254 _M_replace_categories(const _Impl*, category);
257 _M_replace_category(const _Impl*, const locale::id* const*);
260 _M_replace_facet(const _Impl*, const locale::id*);
263 _M_install_facet(const locale::id*, const facet*);
265 template<typename _Facet>
267 _M_init_facet(_Facet* __facet)
268 { _M_install_facet(&_Facet::id, __facet); }
271 template<typename _Facet>
272 locale::locale(const locale& __other, _Facet* __f)
274 _M_impl = new _Impl(*__other._M_impl, 1);
275 _M_impl->_M_install_facet(&_Facet::id, __f);
277 __i < _S_categories_size + _S_extra_categories_size; ++__i)
279 delete [] _M_impl->_M_names[__i];
280 char* __new = new char[2];
282 _M_impl->_M_names[__i] = __new;
287 // 22.1.1.1.2 Class locale::facet
292 friend class locale::_Impl;
294 mutable _Atomic_word _M_references;
297 // Contains data from the underlying "C" library for the classic locale.
298 static __c_locale _S_c_locale;
300 // String literal for the name of the classic locale.
301 static char _S_c_name[2];
304 facet(size_t __refs = 0) throw();
310 _S_create_c_locale(__c_locale& __cloc, const char* __s,
311 __c_locale __old = 0);
314 _S_clone_c_locale(__c_locale& __cloc);
317 _S_destroy_c_locale(__c_locale& __cloc);
321 _M_add_reference() const throw();
324 _M_remove_reference() const throw();
326 facet(const facet&); // Not defined.
329 operator=(const facet&); // Not defined.
333 // 22.1.1.1.3 Class locale::id
338 friend class locale::_Impl;
339 template<typename _Facet>
341 use_facet(const locale&);
342 template<typename _Facet>
344 has_facet(const locale&) throw ();
346 // NB: There is no accessor for _M_index because it may be used
347 // before the constructor is run; the effect of calling a member
348 // function (even an inline) would be undefined.
349 mutable size_t _M_index;
351 // Last id number assigned.
352 static _Atomic_word _S_highwater;
355 operator=(const id&); // Not defined.
357 id(const id&); // Not defined.
360 // NB: This class is always a static data member, and thus can be
361 // counted on to be zero-initialized.
368 _M_index = 1 + __exchange_and_add(&_S_highwater, 1);