1 // Locale support -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
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.
31 /** @file locale_classes.h
32 * This is an internal header file, included by other library headers.
33 * You should not attempt to use it directly.
37 // ISO C++ 14882: 22.1 Locales
40 #ifndef _LOCALE_CLASSES_H
41 #define _LOCALE_CLASSES_H 1
43 #pragma GCC system_header
45 #include <bits/localefwd.h>
46 #include <cstring> // For strcmp.
48 #include <ext/atomicity.h>
50 _GLIBCXX_BEGIN_NAMESPACE(std)
52 // 22.1.1 Class locale
54 * @brief Container class for localization functionality.
56 * The locale class is first a class wrapper for C library locales. It is
57 * also an extensible container for user-defined localization. A locale is
58 * a collection of facets that implement various localization features such
59 * as money, time, and number printing.
61 * Constructing C++ locales does not change the C library locale.
63 * This library supports efficient construction and copying of locales
64 * through a reference counting implementation of the locale class.
70 /// Definition of locale::category.
73 // Forward decls and friends:
81 template<typename _Facet>
83 has_facet(const locale&) throw();
85 template<typename _Facet>
87 use_facet(const locale&);
89 template<typename _Cache>
90 friend struct __use_cache;
94 * @brief Category values.
96 * The standard category values are none, ctype, numeric, collate, time,
97 * monetary, and messages. They form a bitmask that supports union and
98 * intersection. The category all is the union of these values.
101 * NB: Order must match _S_facet_categories definition in locale.cc
104 static const category none = 0;
105 static const category ctype = 1L << 0;
106 static const category numeric = 1L << 1;
107 static const category collate = 1L << 2;
108 static const category time = 1L << 3;
109 static const category monetary = 1L << 4;
110 static const category messages = 1L << 5;
111 static const category all = (ctype | numeric | collate |
112 time | monetary | messages);
115 // Construct/copy/destroy:
118 * @brief Default constructor.
120 * Constructs a copy of the global locale. If no locale has been
121 * explicitly set, this is the "C" locale.
126 * @brief Copy constructor.
128 * Constructs a copy of @a other.
130 * @param other The locale to copy.
132 locale(const locale& __other) throw();
135 * @brief Named locale constructor.
137 * Constructs a copy of the named C library locale.
139 * @param s Name of the locale to construct.
140 * @throw std::runtime_error if s is null or an undefined locale.
143 locale(const char* __s);
146 * @brief Construct locale with facets from another locale.
148 * Constructs a copy of the locale @a base. The facets specified by @a
149 * cat are replaced with those from the locale named by @a s. If base is
150 * named, this locale instance will also be named.
152 * @param base The locale to copy.
153 * @param s Name of the locale to use facets from.
154 * @param cat Set of categories defining the facets to use from s.
155 * @throw std::runtime_error if s is null or an undefined locale.
157 locale(const locale& __base, const char* __s, category __cat);
160 * @brief Construct locale with facets from another locale.
162 * Constructs a copy of the locale @a base. The facets specified by @a
163 * cat are replaced with those from the locale @a add. If @a base and @a
164 * add are named, this locale instance will also be named.
166 * @param base The locale to copy.
167 * @param add The locale to use facets from.
168 * @param cat Set of categories defining the facets to use from add.
170 locale(const locale& __base, const locale& __add, category __cat);
173 * @brief Construct locale with another facet.
175 * Constructs a copy of the locale @a other. The facet @f is added to
176 * @other, replacing an existing facet of type Facet if there is one. If
177 * @f is null, this locale is a copy of @a other.
179 * @param other The locale to copy.
180 * @param f The facet to add in.
182 template<typename _Facet>
183 locale(const locale& __other, _Facet* __f);
185 /// Locale destructor.
189 * @brief Assignment operator.
191 * Set this locale to be a copy of @a other.
193 * @param other The locale to copy.
194 * @return A reference to this locale.
197 operator=(const locale& __other) throw();
200 * @brief Construct locale with another facet.
202 * Constructs and returns a new copy of this locale. Adds or replaces an
203 * existing facet of type Facet from the locale @a other into the new
206 * @param Facet The facet type to copy from other
207 * @param other The locale to copy from.
208 * @return Newly constructed locale.
209 * @throw std::runtime_error if other has no facet of type Facet.
211 template<typename _Facet>
213 combine(const locale& __other) const;
215 // Locale operations:
217 * @brief Return locale name.
218 * @return Locale name or "*" if unnamed.
224 * @brief Locale equality.
226 * @param other The locale to compare against.
227 * @return True if other and this refer to the same locale instance, are
228 * copies, or have the same name. False otherwise.
231 operator==(const locale& __other) const throw ();
234 * @brief Locale inequality.
236 * @param other The locale to compare against.
237 * @return ! (*this == other)
240 operator!=(const locale& __other) const throw ()
241 { return !(this->operator==(__other)); }
244 * @brief Compare two strings according to collate.
246 * Template operator to compare two strings using the compare function of
247 * the collate facet in this locale. One use is to provide the locale to
248 * the sort function. For example, a vector v of strings could be sorted
249 * according to locale loc by doing:
251 * std::sort(v.begin(), v.end(), loc);
254 * @param s1 First string to compare.
255 * @param s2 Second string to compare.
256 * @return True if collate<Char> facet compares s1 < s2, else false.
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 * @brief Set global locale
267 * This function sets the global locale to the argument and returns a
268 * copy of the previous global locale. If the argument has a name, it
269 * will also call std::setlocale(LC_ALL, loc.name()).
271 * @param locale The new locale to make global.
272 * @return Copy of the old global locale.
275 global(const locale&);
278 * @brief Return reference to the "C" locale.
284 // The (shared) implementation
287 // The "C" reference locale
288 static _Impl* _S_classic;
290 // Current global locale
291 static _Impl* _S_global;
293 // Names of underlying locale categories.
294 // NB: locale::global() has to know how to modify all the
295 // underlying categories, not just the ones required by the C++
297 static const char* const* const _S_categories;
299 // Number of standard categories. For C++, these categories are
300 // collate, ctype, monetary, numeric, time, and messages. These
301 // directly correspond to ISO C99 macros LC_COLLATE, LC_CTYPE,
302 // LC_MONETARY, LC_NUMERIC, and LC_TIME. In addition, POSIX (IEEE
303 // 1003.1-2001) specifies LC_MESSAGES.
304 // In addition to the standard categories, the underlying
305 // operating system is allowed to define extra LC_*
306 // macros. For GNU systems, the following are also valid:
307 // LC_PAPER, LC_NAME, LC_ADDRESS, LC_TELEPHONE, LC_MEASUREMENT,
308 // and LC_IDENTIFICATION.
309 enum { _S_categories_size = 6 + _GLIBCXX_NUM_CATEGORIES };
312 static __gthread_once_t _S_once;
316 locale(_Impl*) throw();
322 _S_initialize_once();
325 _S_normalize_category(category);
328 _M_coalesce(const locale& __base, const locale& __add, category __cat);
332 // 22.1.1.1.2 Class locale::facet
334 * @brief Localization functionality base class.
336 * The facet class is the base class for a localization feature, such as
337 * money, time, and number printing. It provides common support for facets
338 * and reference management.
340 * Facets may not be copied or assigned.
346 friend class locale::_Impl;
348 mutable _Atomic_word _M_refcount;
350 // Contains data from the underlying "C" library for the classic locale.
351 static __c_locale _S_c_locale;
353 // String literal for the name of the classic locale.
354 static const char _S_c_name[2];
357 static __gthread_once_t _S_once;
361 _S_initialize_once();
365 * @brief Facet constructor.
367 * This is the constructor provided by the standard. If refs is 0, the
368 * facet is destroyed when the last referencing locale is destroyed.
369 * Otherwise the facet will never be destroyed.
371 * @param refs The initial value for reference count.
374 facet(size_t __refs = 0) throw() : _M_refcount(__refs ? 1 : 0)
377 /// Facet destructor.
382 _S_create_c_locale(__c_locale& __cloc, const char* __s,
383 __c_locale __old = 0);
386 _S_clone_c_locale(__c_locale& __cloc);
389 _S_destroy_c_locale(__c_locale& __cloc);
391 // Returns data from the underlying "C" library data for the
401 _M_add_reference() const throw()
402 { __gnu_cxx::__atomic_add_dispatch(&_M_refcount, 1); }
405 _M_remove_reference() const throw()
407 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_refcount, -1) == 1)
416 facet(const facet&); // Not defined.
419 operator=(const facet&); // Not defined.
423 // 22.1.1.1.3 Class locale::id
425 * @brief Facet ID class.
427 * The ID class provides facets with an index used to identify them.
428 * Every facet class must define a public static member locale::id, or be
429 * derived from a facet that provides this member, otherwise the facet
430 * cannot be used in a locale. The locale::id ensures that each class
431 * type gets a unique identifier.
437 friend class locale::_Impl;
439 template<typename _Facet>
441 use_facet(const locale&);
443 template<typename _Facet>
445 has_facet(const locale&) throw ();
447 // NB: There is no accessor for _M_index because it may be used
448 // before the constructor is run; the effect of calling a member
449 // function (even an inline) would be undefined.
450 mutable size_t _M_index;
452 // Last id number assigned.
453 static _Atomic_word _S_refcount;
456 operator=(const id&); // Not defined.
458 id(const id&); // Not defined.
461 // NB: This class is always a static data member, and thus can be
462 // counted on to be zero-initialized.
471 // Implementation object for locale.
477 friend class locale::facet;
479 template<typename _Facet>
481 has_facet(const locale&) throw();
483 template<typename _Facet>
485 use_facet(const locale&);
487 template<typename _Cache>
488 friend struct __use_cache;
492 _Atomic_word _M_refcount;
493 const facet** _M_facets;
494 size_t _M_facets_size;
495 const facet** _M_caches;
497 static const locale::id* const _S_id_ctype[];
498 static const locale::id* const _S_id_numeric[];
499 static const locale::id* const _S_id_collate[];
500 static const locale::id* const _S_id_time[];
501 static const locale::id* const _S_id_monetary[];
502 static const locale::id* const _S_id_messages[];
503 static const locale::id* const* const _S_facet_categories[];
506 _M_add_reference() throw()
507 { __gnu_cxx::__atomic_add_dispatch(&_M_refcount, 1); }
510 _M_remove_reference() throw()
512 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_refcount, -1) == 1)
521 _Impl(const _Impl&, size_t);
522 _Impl(const char*, size_t);
523 _Impl(size_t) throw();
527 _Impl(const _Impl&); // Not defined.
530 operator=(const _Impl&); // Not defined.
537 // We must actually compare all the _M_names: can be all equal!
538 for (size_t __i = 0; __ret && __i < _S_categories_size - 1; ++__i)
539 __ret = std::strcmp(_M_names[__i], _M_names[__i + 1]) == 0;
544 _M_replace_categories(const _Impl*, category);
547 _M_replace_category(const _Impl*, const locale::id* const*);
550 _M_replace_facet(const _Impl*, const locale::id*);
553 _M_install_facet(const locale::id*, const facet*);
555 template<typename _Facet>
557 _M_init_facet(_Facet* __facet)
558 { _M_install_facet(&_Facet::id, __facet); }
561 _M_install_cache(const facet*, size_t);
564 template<typename _Facet>
565 locale::locale(const locale& __other, _Facet* __f)
567 _M_impl = new _Impl(*__other._M_impl, 1);
570 { _M_impl->_M_install_facet(&_Facet::id, __f); }
573 _M_impl->_M_remove_reference();
574 __throw_exception_again;
576 delete [] _M_impl->_M_names[0];
577 _M_impl->_M_names[0] = 0; // Unnamed.
580 _GLIBCXX_END_NAMESPACE