Merge from mainline (165734:167278).
[official-gcc/graphite-test-results.git] / libstdc++-v3 / include / bits / locale_classes.h
blob8a846bce0c6166a0c999ff8245705c37bb955d3a
1 // Locale support -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2007, 2008, 2009, 2010
5 // Free Software Foundation, Inc.
6 //
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 3, or (at your option)
11 // any later version.
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // Under Section 7 of GPL version 3, you are granted additional
19 // permissions described in the GCC Runtime Library Exception, version
20 // 3.1, as published by the Free Software Foundation.
22 // You should have received a copy of the GNU General Public License and
23 // a copy of the GCC Runtime Library Exception along with this program;
24 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 // <http://www.gnu.org/licenses/>.
27 /** @file locale_classes.h
28 * This is an internal header file, included by other library headers.
29 * You should not attempt to use it directly.
33 // ISO C++ 14882: 22.1 Locales
36 #ifndef _LOCALE_CLASSES_H
37 #define _LOCALE_CLASSES_H 1
39 #pragma GCC system_header
41 #include <bits/localefwd.h>
42 #include <string>
43 #include <ext/atomicity.h>
45 _GLIBCXX_BEGIN_NAMESPACE(std)
47 // 22.1.1 Class locale
48 /**
49 * @brief Container class for localization functionality.
50 * @ingroup locales
52 * The locale class is first a class wrapper for C library locales. It is
53 * also an extensible container for user-defined localization. A locale is
54 * a collection of facets that implement various localization features such
55 * as money, time, and number printing.
57 * Constructing C++ locales does not change the C library locale.
59 * This library supports efficient construction and copying of locales
60 * through a reference counting implementation of the locale class.
62 class locale
64 public:
65 // Types:
66 /// Definition of locale::category.
67 typedef int category;
69 // Forward decls and friends:
70 class facet;
71 class id;
72 class _Impl;
74 friend class facet;
75 friend class _Impl;
77 template<typename _Facet>
78 friend bool
79 has_facet(const locale&) throw();
81 template<typename _Facet>
82 friend const _Facet&
83 use_facet(const locale&);
85 template<typename _Cache>
86 friend struct __use_cache;
88 //@{
89 /**
90 * @brief Category values.
92 * The standard category values are none, ctype, numeric, collate, time,
93 * monetary, and messages. They form a bitmask that supports union and
94 * intersection. The category all is the union of these values.
96 * NB: Order must match _S_facet_categories definition in locale.cc
98 static const category none = 0;
99 static const category ctype = 1L << 0;
100 static const category numeric = 1L << 1;
101 static const category collate = 1L << 2;
102 static const category time = 1L << 3;
103 static const category monetary = 1L << 4;
104 static const category messages = 1L << 5;
105 static const category all = (ctype | numeric | collate |
106 time | monetary | messages);
107 //@}
109 // Construct/copy/destroy:
112 * @brief Default constructor.
114 * Constructs a copy of the global locale. If no locale has been
115 * explicitly set, this is the C locale.
117 locale() throw();
120 * @brief Copy constructor.
122 * Constructs a copy of @a other.
124 * @param other The locale to copy.
126 locale(const locale& __other) throw();
129 * @brief Named locale constructor.
131 * Constructs a copy of the named C library locale.
133 * @param s Name of the locale to construct.
134 * @throw std::runtime_error if s is null or an undefined locale.
136 explicit
137 locale(const char* __s);
140 * @brief Construct locale with facets from another locale.
142 * Constructs a copy of the locale @a base. The facets specified by @a
143 * cat are replaced with those from the locale named by @a s. If base is
144 * named, this locale instance will also be named.
146 * @param base The locale to copy.
147 * @param s Name of the locale to use facets from.
148 * @param cat Set of categories defining the facets to use from s.
149 * @throw std::runtime_error if s is null or an undefined locale.
151 locale(const locale& __base, const char* __s, category __cat);
154 * @brief Construct locale with facets from another locale.
156 * Constructs a copy of the locale @a base. The facets specified by @a
157 * cat are replaced with those from the locale @a add. If @a base and @a
158 * add are named, this locale instance will also be named.
160 * @param base The locale to copy.
161 * @param add The locale to use facets from.
162 * @param cat Set of categories defining the facets to use from add.
164 locale(const locale& __base, const locale& __add, category __cat);
167 * @brief Construct locale with another facet.
169 * Constructs a copy of the locale @a other. The facet @f is added to
170 * @other, replacing an existing facet of type Facet if there is one. If
171 * @f is null, this locale is a copy of @a other.
173 * @param other The locale to copy.
174 * @param f The facet to add in.
176 template<typename _Facet>
177 locale(const locale& __other, _Facet* __f);
179 /// Locale destructor.
180 ~locale() throw();
183 * @brief Assignment operator.
185 * Set this locale to be a copy of @a other.
187 * @param other The locale to copy.
188 * @return A reference to this locale.
190 const locale&
191 operator=(const locale& __other) throw();
194 * @brief Construct locale with another facet.
196 * Constructs and returns a new copy of this locale. Adds or replaces an
197 * existing facet of type Facet from the locale @a other into the new
198 * locale.
200 * @param Facet The facet type to copy from other
201 * @param other The locale to copy from.
202 * @return Newly constructed locale.
203 * @throw std::runtime_error if other has no facet of type Facet.
205 template<typename _Facet>
206 locale
207 combine(const locale& __other) const;
209 // Locale operations:
211 * @brief Return locale name.
212 * @return Locale name or "*" if unnamed.
214 string
215 name() const;
218 * @brief Locale equality.
220 * @param other The locale to compare against.
221 * @return True if other and this refer to the same locale instance, are
222 * copies, or have the same name. False otherwise.
224 bool
225 operator==(const locale& __other) const throw();
228 * @brief Locale inequality.
230 * @param other The locale to compare against.
231 * @return ! (*this == other)
233 bool
234 operator!=(const locale& __other) const throw()
235 { return !(this->operator==(__other)); }
238 * @brief Compare two strings according to collate.
240 * Template operator to compare two strings using the compare function of
241 * the collate facet in this locale. One use is to provide the locale to
242 * the sort function. For example, a vector v of strings could be sorted
243 * according to locale loc by doing:
244 * @code
245 * std::sort(v.begin(), v.end(), loc);
246 * @endcode
248 * @param s1 First string to compare.
249 * @param s2 Second string to compare.
250 * @return True if collate<Char> facet compares s1 < s2, else false.
252 template<typename _Char, typename _Traits, typename _Alloc>
253 bool
254 operator()(const basic_string<_Char, _Traits, _Alloc>& __s1,
255 const basic_string<_Char, _Traits, _Alloc>& __s2) const;
257 // Global locale objects:
259 * @brief Set global locale
261 * This function sets the global locale to the argument and returns a
262 * copy of the previous global locale. If the argument has a name, it
263 * will also call std::setlocale(LC_ALL, loc.name()).
265 * @param locale The new locale to make global.
266 * @return Copy of the old global locale.
268 static locale
269 global(const locale&);
272 * @brief Return reference to the C locale.
274 static const locale&
275 classic();
277 private:
278 // The (shared) implementation
279 _Impl* _M_impl;
281 // The "C" reference locale
282 static _Impl* _S_classic;
284 // Current global locale
285 static _Impl* _S_global;
287 // Names of underlying locale categories.
288 // NB: locale::global() has to know how to modify all the
289 // underlying categories, not just the ones required by the C++
290 // standard.
291 static const char* const* const _S_categories;
293 // Number of standard categories. For C++, these categories are
294 // collate, ctype, monetary, numeric, time, and messages. These
295 // directly correspond to ISO C99 macros LC_COLLATE, LC_CTYPE,
296 // LC_MONETARY, LC_NUMERIC, and LC_TIME. In addition, POSIX (IEEE
297 // 1003.1-2001) specifies LC_MESSAGES.
298 // In addition to the standard categories, the underlying
299 // operating system is allowed to define extra LC_*
300 // macros. For GNU systems, the following are also valid:
301 // LC_PAPER, LC_NAME, LC_ADDRESS, LC_TELEPHONE, LC_MEASUREMENT,
302 // and LC_IDENTIFICATION.
303 enum { _S_categories_size = 6 + _GLIBCXX_NUM_CATEGORIES };
305 #ifdef __GTHREADS
306 static __gthread_once_t _S_once;
307 #endif
309 explicit
310 locale(_Impl*) throw();
312 static void
313 _S_initialize();
315 static void
316 _S_initialize_once() throw();
318 static category
319 _S_normalize_category(category);
321 void
322 _M_coalesce(const locale& __base, const locale& __add, category __cat);
326 // 22.1.1.1.2 Class locale::facet
328 * @brief Localization functionality base class.
329 * @ingroup locales
331 * The facet class is the base class for a localization feature, such as
332 * money, time, and number printing. It provides common support for facets
333 * and reference management.
335 * Facets may not be copied or assigned.
337 class locale::facet
339 private:
340 friend class locale;
341 friend class locale::_Impl;
343 mutable _Atomic_word _M_refcount;
345 // Contains data from the underlying "C" library for the classic locale.
346 static __c_locale _S_c_locale;
348 // String literal for the name of the classic locale.
349 static const char _S_c_name[2];
351 #ifdef __GTHREADS
352 static __gthread_once_t _S_once;
353 #endif
355 static void
356 _S_initialize_once();
358 protected:
360 * @brief Facet constructor.
362 * This is the constructor provided by the standard. If refs is 0, the
363 * facet is destroyed when the last referencing locale is destroyed.
364 * Otherwise the facet will never be destroyed.
366 * @param refs The initial value for reference count.
368 explicit
369 facet(size_t __refs = 0) throw() : _M_refcount(__refs ? 1 : 0)
372 /// Facet destructor.
373 virtual
374 ~facet();
376 static void
377 _S_create_c_locale(__c_locale& __cloc, const char* __s,
378 __c_locale __old = 0);
380 static __c_locale
381 _S_clone_c_locale(__c_locale& __cloc) throw();
383 static void
384 _S_destroy_c_locale(__c_locale& __cloc);
386 static __c_locale
387 _S_lc_ctype_c_locale(__c_locale __cloc, const char* __s);
389 // Returns data from the underlying "C" library data for the
390 // classic locale.
391 static __c_locale
392 _S_get_c_locale();
394 _GLIBCXX_CONST static const char*
395 _S_get_c_name() throw();
397 private:
398 void
399 _M_add_reference() const throw()
400 { __gnu_cxx::__atomic_add_dispatch(&_M_refcount, 1); }
402 void
403 _M_remove_reference() const throw()
405 // Be race-detector-friendly. For more info see bits/c++config.
406 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_refcount);
407 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_refcount, -1) == 1)
409 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_refcount);
410 __try
411 { delete this; }
412 __catch(...)
417 facet(const facet&); // Not defined.
419 facet&
420 operator=(const facet&); // Not defined.
424 // 22.1.1.1.3 Class locale::id
426 * @brief Facet ID class.
427 * @ingroup locales
429 * The ID class provides facets with an index used to identify them.
430 * Every facet class must define a public static member locale::id, or be
431 * derived from a facet that provides this member, otherwise the facet
432 * cannot be used in a locale. The locale::id ensures that each class
433 * type gets a unique identifier.
435 class locale::id
437 private:
438 friend class locale;
439 friend class locale::_Impl;
441 template<typename _Facet>
442 friend const _Facet&
443 use_facet(const locale&);
445 template<typename _Facet>
446 friend bool
447 has_facet(const locale&) throw();
449 // NB: There is no accessor for _M_index because it may be used
450 // before the constructor is run; the effect of calling a member
451 // function (even an inline) would be undefined.
452 mutable size_t _M_index;
454 // Last id number assigned.
455 static _Atomic_word _S_refcount;
457 void
458 operator=(const id&); // Not defined.
460 id(const id&); // Not defined.
462 public:
463 // NB: This class is always a static data member, and thus can be
464 // counted on to be zero-initialized.
465 /// Constructor.
466 id() { }
468 size_t
469 _M_id() const throw();
473 // Implementation object for locale.
474 class locale::_Impl
476 public:
477 // Friends.
478 friend class locale;
479 friend class locale::facet;
481 template<typename _Facet>
482 friend bool
483 has_facet(const locale&) throw();
485 template<typename _Facet>
486 friend const _Facet&
487 use_facet(const locale&);
489 template<typename _Cache>
490 friend struct __use_cache;
492 private:
493 // Data Members.
494 _Atomic_word _M_refcount;
495 const facet** _M_facets;
496 size_t _M_facets_size;
497 const facet** _M_caches;
498 char** _M_names;
499 static const locale::id* const _S_id_ctype[];
500 static const locale::id* const _S_id_numeric[];
501 static const locale::id* const _S_id_collate[];
502 static const locale::id* const _S_id_time[];
503 static const locale::id* const _S_id_monetary[];
504 static const locale::id* const _S_id_messages[];
505 static const locale::id* const* const _S_facet_categories[];
507 void
508 _M_add_reference() throw()
509 { __gnu_cxx::__atomic_add_dispatch(&_M_refcount, 1); }
511 void
512 _M_remove_reference() throw()
514 // Be race-detector-friendly. For more info see bits/c++config.
515 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_refcount);
516 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_refcount, -1) == 1)
518 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_refcount);
519 __try
520 { delete this; }
521 __catch(...)
526 _Impl(const _Impl&, size_t);
527 _Impl(const char*, size_t);
528 _Impl(size_t) throw();
530 ~_Impl() throw();
532 _Impl(const _Impl&); // Not defined.
534 void
535 operator=(const _Impl&); // Not defined.
537 bool
538 _M_check_same_name()
540 bool __ret = true;
541 if (_M_names[1])
542 // We must actually compare all the _M_names: can be all equal!
543 for (size_t __i = 0; __ret && __i < _S_categories_size - 1; ++__i)
544 __ret = __builtin_strcmp(_M_names[__i], _M_names[__i + 1]) == 0;
545 return __ret;
548 void
549 _M_replace_categories(const _Impl*, category);
551 void
552 _M_replace_category(const _Impl*, const locale::id* const*);
554 void
555 _M_replace_facet(const _Impl*, const locale::id*);
557 void
558 _M_install_facet(const locale::id*, const facet*);
560 template<typename _Facet>
561 void
562 _M_init_facet(_Facet* __facet)
563 { _M_install_facet(&_Facet::id, __facet); }
565 void
566 _M_install_cache(const facet*, size_t);
571 * @brief Test for the presence of a facet.
573 * has_facet tests the locale argument for the presence of the facet type
574 * provided as the template parameter. Facets derived from the facet
575 * parameter will also return true.
577 * @param Facet The facet type to test the presence of.
578 * @param locale The locale to test.
579 * @return true if locale contains a facet of type Facet, else false.
581 template<typename _Facet>
582 bool
583 has_facet(const locale& __loc) throw();
586 * @brief Return a facet.
588 * use_facet looks for and returns a reference to a facet of type Facet
589 * where Facet is the template parameter. If has_facet(locale) is true,
590 * there is a suitable facet to return. It throws std::bad_cast if the
591 * locale doesn't contain a facet of type Facet.
593 * @param Facet The facet type to access.
594 * @param locale The locale to use.
595 * @return Reference to facet of type Facet.
596 * @throw std::bad_cast if locale doesn't contain a facet of type Facet.
598 template<typename _Facet>
599 const _Facet&
600 use_facet(const locale& __loc);
604 * @brief Facet for localized string comparison.
606 * This facet encapsulates the code to compare strings in a localized
607 * manner.
609 * The collate template uses protected virtual functions to provide
610 * the actual results. The public accessors forward the call to
611 * the virtual functions. These virtual functions are hooks for
612 * developers to implement the behavior they require from the
613 * collate facet.
615 template<typename _CharT>
616 class collate : public locale::facet
618 public:
619 // Types:
620 //@{
621 /// Public typedefs
622 typedef _CharT char_type;
623 typedef basic_string<_CharT> string_type;
624 //@}
626 protected:
627 // Underlying "C" library locale information saved from
628 // initialization, needed by collate_byname as well.
629 __c_locale _M_c_locale_collate;
631 public:
632 /// Numpunct facet id.
633 static locale::id id;
636 * @brief Constructor performs initialization.
638 * This is the constructor provided by the standard.
640 * @param refs Passed to the base facet class.
642 explicit
643 collate(size_t __refs = 0)
644 : facet(__refs), _M_c_locale_collate(_S_get_c_locale())
648 * @brief Internal constructor. Not for general use.
650 * This is a constructor for use by the library itself to set up new
651 * locales.
653 * @param cloc The C locale.
654 * @param refs Passed to the base facet class.
656 explicit
657 collate(__c_locale __cloc, size_t __refs = 0)
658 : facet(__refs), _M_c_locale_collate(_S_clone_c_locale(__cloc))
662 * @brief Compare two strings.
664 * This function compares two strings and returns the result by calling
665 * collate::do_compare().
667 * @param lo1 Start of string 1.
668 * @param hi1 End of string 1.
669 * @param lo2 Start of string 2.
670 * @param hi2 End of string 2.
671 * @return 1 if string1 > string2, -1 if string1 < string2, else 0.
674 compare(const _CharT* __lo1, const _CharT* __hi1,
675 const _CharT* __lo2, const _CharT* __hi2) const
676 { return this->do_compare(__lo1, __hi1, __lo2, __hi2); }
679 * @brief Transform string to comparable form.
681 * This function is a wrapper for strxfrm functionality. It takes the
682 * input string and returns a modified string that can be directly
683 * compared to other transformed strings. In the C locale, this
684 * function just returns a copy of the input string. In some other
685 * locales, it may replace two chars with one, change a char for
686 * another, etc. It does so by returning collate::do_transform().
688 * @param lo Start of string.
689 * @param hi End of string.
690 * @return Transformed string_type.
692 string_type
693 transform(const _CharT* __lo, const _CharT* __hi) const
694 { return this->do_transform(__lo, __hi); }
697 * @brief Return hash of a string.
699 * This function computes and returns a hash on the input string. It
700 * does so by returning collate::do_hash().
702 * @param lo Start of string.
703 * @param hi End of string.
704 * @return Hash value.
706 long
707 hash(const _CharT* __lo, const _CharT* __hi) const
708 { return this->do_hash(__lo, __hi); }
710 // Used to abstract out _CharT bits in virtual member functions, below.
712 _M_compare(const _CharT*, const _CharT*) const throw();
714 size_t
715 _M_transform(_CharT*, const _CharT*, size_t) const throw();
717 protected:
718 /// Destructor.
719 virtual
720 ~collate()
721 { _S_destroy_c_locale(_M_c_locale_collate); }
724 * @brief Compare two strings.
726 * This function is a hook for derived classes to change the value
727 * returned. @see compare().
729 * @param lo1 Start of string 1.
730 * @param hi1 End of string 1.
731 * @param lo2 Start of string 2.
732 * @param hi2 End of string 2.
733 * @return 1 if string1 > string2, -1 if string1 < string2, else 0.
735 virtual int
736 do_compare(const _CharT* __lo1, const _CharT* __hi1,
737 const _CharT* __lo2, const _CharT* __hi2) const;
740 * @brief Transform string to comparable form.
742 * This function is a hook for derived classes to change the value
743 * returned.
745 * @param lo1 Start of string 1.
746 * @param hi1 End of string 1.
747 * @param lo2 Start of string 2.
748 * @param hi2 End of string 2.
749 * @return 1 if string1 > string2, -1 if string1 < string2, else 0.
751 virtual string_type
752 do_transform(const _CharT* __lo, const _CharT* __hi) const;
755 * @brief Return hash of a string.
757 * This function computes and returns a hash on the input string. This
758 * function is a hook for derived classes to change the value returned.
760 * @param lo Start of string.
761 * @param hi End of string.
762 * @return Hash value.
764 virtual long
765 do_hash(const _CharT* __lo, const _CharT* __hi) const;
768 template<typename _CharT>
769 locale::id collate<_CharT>::id;
771 // Specializations.
772 template<>
774 collate<char>::_M_compare(const char*, const char*) const throw();
776 template<>
777 size_t
778 collate<char>::_M_transform(char*, const char*, size_t) const throw();
780 #ifdef _GLIBCXX_USE_WCHAR_T
781 template<>
783 collate<wchar_t>::_M_compare(const wchar_t*, const wchar_t*) const throw();
785 template<>
786 size_t
787 collate<wchar_t>::_M_transform(wchar_t*, const wchar_t*, size_t) const throw();
788 #endif
790 /// class collate_byname [22.2.4.2].
791 template<typename _CharT>
792 class collate_byname : public collate<_CharT>
794 public:
795 //@{
796 /// Public typedefs
797 typedef _CharT char_type;
798 typedef basic_string<_CharT> string_type;
799 //@}
801 explicit
802 collate_byname(const char* __s, size_t __refs = 0)
803 : collate<_CharT>(__refs)
805 if (__builtin_strcmp(__s, "C") != 0
806 && __builtin_strcmp(__s, "POSIX") != 0)
808 this->_S_destroy_c_locale(this->_M_c_locale_collate);
809 this->_S_create_c_locale(this->_M_c_locale_collate, __s);
813 protected:
814 virtual
815 ~collate_byname() { }
818 _GLIBCXX_END_NAMESPACE
820 #ifndef _GLIBCXX_EXPORT_TEMPLATE
821 # include <bits/locale_classes.tcc>
822 #endif
824 #endif