Install gcc-4.4.0-tdm-1-core-2.tar.gz
[msysgit.git] / mingw / lib / gcc / mingw32 / 4.3.3 / include / c++ / bits / locale_classes.h
blobcab5812160ecd607d2a46da1079b39a6f5362133
1 // Locale support -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2007, 2008
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 2, 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 // You should have received a copy of the GNU General Public License along
19 // with this library; see the file COPYING. If not, write to the Free
20 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // USA.
23 // As a special exception, you may use this file as part of a free software
24 // library without restriction. Specifically, if other files instantiate
25 // templates or use macros or inline functions from this file, or you compile
26 // this file and link it with other files to produce an executable, this
27 // file does not by itself cause the resulting executable to be covered by
28 // the GNU General Public License. This exception does not however
29 // invalidate any other reasons why the executable file might be covered by
30 // the GNU General Public License.
32 /** @file locale_classes.h
33 * This is an internal header file, included by other library headers.
34 * You should not attempt to use it directly.
38 // ISO C++ 14882: 22.1 Locales
41 #ifndef _LOCALE_CLASSES_H
42 #define _LOCALE_CLASSES_H 1
44 #pragma GCC system_header
46 #include <bits/localefwd.h>
47 #include <string>
48 #include <ext/atomicity.h>
50 _GLIBCXX_BEGIN_NAMESPACE(std)
52 // 22.1.1 Class locale
53 /**
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.
66 class locale
68 public:
69 // Types:
70 /// Definition of locale::category.
71 typedef int category;
73 // Forward decls and friends:
74 class facet;
75 class id;
76 class _Impl;
78 friend class facet;
79 friend class _Impl;
81 template<typename _Facet>
82 friend bool
83 has_facet(const locale&) throw();
85 template<typename _Facet>
86 friend const _Facet&
87 use_facet(const locale&);
89 template<typename _Cache>
90 friend struct __use_cache;
92 //@{
93 /**
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.
100 * NB: Order must match _S_facet_categories definition in locale.cc
102 static const category none = 0;
103 static const category ctype = 1L << 0;
104 static const category numeric = 1L << 1;
105 static const category collate = 1L << 2;
106 static const category time = 1L << 3;
107 static const category monetary = 1L << 4;
108 static const category messages = 1L << 5;
109 static const category all = (ctype | numeric | collate |
110 time | monetary | messages);
111 //@}
113 // Construct/copy/destroy:
116 * @brief Default constructor.
118 * Constructs a copy of the global locale. If no locale has been
119 * explicitly set, this is the "C" locale.
121 locale() throw();
124 * @brief Copy constructor.
126 * Constructs a copy of @a other.
128 * @param other The locale to copy.
130 locale(const locale& __other) throw();
133 * @brief Named locale constructor.
135 * Constructs a copy of the named C library locale.
137 * @param s Name of the locale to construct.
138 * @throw std::runtime_error if s is null or an undefined locale.
140 explicit
141 locale(const char* __s);
144 * @brief Construct locale with facets from another locale.
146 * Constructs a copy of the locale @a base. The facets specified by @a
147 * cat are replaced with those from the locale named by @a s. If base is
148 * named, this locale instance will also be named.
150 * @param base The locale to copy.
151 * @param s Name of the locale to use facets from.
152 * @param cat Set of categories defining the facets to use from s.
153 * @throw std::runtime_error if s is null or an undefined locale.
155 locale(const locale& __base, const char* __s, category __cat);
158 * @brief Construct locale with facets from another locale.
160 * Constructs a copy of the locale @a base. The facets specified by @a
161 * cat are replaced with those from the locale @a add. If @a base and @a
162 * add are named, this locale instance will also be named.
164 * @param base The locale to copy.
165 * @param add The locale to use facets from.
166 * @param cat Set of categories defining the facets to use from add.
168 locale(const locale& __base, const locale& __add, category __cat);
171 * @brief Construct locale with another facet.
173 * Constructs a copy of the locale @a other. The facet @f is added to
174 * @other, replacing an existing facet of type Facet if there is one. If
175 * @f is null, this locale is a copy of @a other.
177 * @param other The locale to copy.
178 * @param f The facet to add in.
180 template<typename _Facet>
181 locale(const locale& __other, _Facet* __f);
183 /// Locale destructor.
184 ~locale() throw();
187 * @brief Assignment operator.
189 * Set this locale to be a copy of @a other.
191 * @param other The locale to copy.
192 * @return A reference to this locale.
194 const locale&
195 operator=(const locale& __other) throw();
198 * @brief Construct locale with another facet.
200 * Constructs and returns a new copy of this locale. Adds or replaces an
201 * existing facet of type Facet from the locale @a other into the new
202 * locale.
204 * @param Facet The facet type to copy from other
205 * @param other The locale to copy from.
206 * @return Newly constructed locale.
207 * @throw std::runtime_error if other has no facet of type Facet.
209 template<typename _Facet>
210 locale
211 combine(const locale& __other) const;
213 // Locale operations:
215 * @brief Return locale name.
216 * @return Locale name or "*" if unnamed.
218 string
219 name() const;
222 * @brief Locale equality.
224 * @param other The locale to compare against.
225 * @return True if other and this refer to the same locale instance, are
226 * copies, or have the same name. False otherwise.
228 bool
229 operator==(const locale& __other) const throw ();
232 * @brief Locale inequality.
234 * @param other The locale to compare against.
235 * @return ! (*this == other)
237 bool
238 operator!=(const locale& __other) const throw ()
239 { return !(this->operator==(__other)); }
242 * @brief Compare two strings according to collate.
244 * Template operator to compare two strings using the compare function of
245 * the collate facet in this locale. One use is to provide the locale to
246 * the sort function. For example, a vector v of strings could be sorted
247 * according to locale loc by doing:
248 * @code
249 * std::sort(v.begin(), v.end(), loc);
250 * @endcode
252 * @param s1 First string to compare.
253 * @param s2 Second string to compare.
254 * @return True if collate<Char> facet compares s1 < s2, else false.
256 template<typename _Char, typename _Traits, typename _Alloc>
257 bool
258 operator()(const basic_string<_Char, _Traits, _Alloc>& __s1,
259 const basic_string<_Char, _Traits, _Alloc>& __s2) const;
261 // Global locale objects:
263 * @brief Set global locale
265 * This function sets the global locale to the argument and returns a
266 * copy of the previous global locale. If the argument has a name, it
267 * will also call std::setlocale(LC_ALL, loc.name()).
269 * @param locale The new locale to make global.
270 * @return Copy of the old global locale.
272 static locale
273 global(const locale&);
276 * @brief Return reference to the "C" locale.
278 static const locale&
279 classic();
281 private:
282 // The (shared) implementation
283 _Impl* _M_impl;
285 // The "C" reference locale
286 static _Impl* _S_classic;
288 // Current global locale
289 static _Impl* _S_global;
291 // Names of underlying locale categories.
292 // NB: locale::global() has to know how to modify all the
293 // underlying categories, not just the ones required by the C++
294 // standard.
295 static const char* const* const _S_categories;
297 // Number of standard categories. For C++, these categories are
298 // collate, ctype, monetary, numeric, time, and messages. These
299 // directly correspond to ISO C99 macros LC_COLLATE, LC_CTYPE,
300 // LC_MONETARY, LC_NUMERIC, and LC_TIME. In addition, POSIX (IEEE
301 // 1003.1-2001) specifies LC_MESSAGES.
302 // In addition to the standard categories, the underlying
303 // operating system is allowed to define extra LC_*
304 // macros. For GNU systems, the following are also valid:
305 // LC_PAPER, LC_NAME, LC_ADDRESS, LC_TELEPHONE, LC_MEASUREMENT,
306 // and LC_IDENTIFICATION.
307 enum { _S_categories_size = 6 + _GLIBCXX_NUM_CATEGORIES };
309 #ifdef __GTHREADS
310 static __gthread_once_t _S_once;
311 #endif
313 explicit
314 locale(_Impl*) throw();
316 static void
317 _S_initialize();
319 static void
320 _S_initialize_once();
322 static category
323 _S_normalize_category(category);
325 void
326 _M_coalesce(const locale& __base, const locale& __add, category __cat);
330 // 22.1.1.1.2 Class locale::facet
332 * @brief Localization functionality base class.
334 * The facet class is the base class for a localization feature, such as
335 * money, time, and number printing. It provides common support for facets
336 * and reference management.
338 * Facets may not be copied or assigned.
340 class locale::facet
342 private:
343 friend class locale;
344 friend class locale::_Impl;
346 mutable _Atomic_word _M_refcount;
348 // Contains data from the underlying "C" library for the classic locale.
349 static __c_locale _S_c_locale;
351 // String literal for the name of the classic locale.
352 static const char _S_c_name[2];
354 #ifdef __GTHREADS
355 static __gthread_once_t _S_once;
356 #endif
358 static void
359 _S_initialize_once();
361 protected:
363 * @brief Facet constructor.
365 * This is the constructor provided by the standard. If refs is 0, the
366 * facet is destroyed when the last referencing locale is destroyed.
367 * Otherwise the facet will never be destroyed.
369 * @param refs The initial value for reference count.
371 explicit
372 facet(size_t __refs = 0) throw() : _M_refcount(__refs ? 1 : 0)
375 /// Facet destructor.
376 virtual
377 ~facet();
379 static void
380 _S_create_c_locale(__c_locale& __cloc, const char* __s,
381 __c_locale __old = 0);
383 static __c_locale
384 _S_clone_c_locale(__c_locale& __cloc);
386 static void
387 _S_destroy_c_locale(__c_locale& __cloc);
389 // Returns data from the underlying "C" library data for the
390 // classic locale.
391 static __c_locale
392 _S_get_c_locale();
394 static const char*
395 _S_get_c_name();
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 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_refcount, -1) == 1)
408 { delete this; }
409 catch(...)
414 facet(const facet&); // Not defined.
416 facet&
417 operator=(const facet&); // Not defined.
421 // 22.1.1.1.3 Class locale::id
423 * @brief Facet ID class.
425 * The ID class provides facets with an index used to identify them.
426 * Every facet class must define a public static member locale::id, or be
427 * derived from a facet that provides this member, otherwise the facet
428 * cannot be used in a locale. The locale::id ensures that each class
429 * type gets a unique identifier.
431 class locale::id
433 private:
434 friend class locale;
435 friend class locale::_Impl;
437 template<typename _Facet>
438 friend const _Facet&
439 use_facet(const locale&);
441 template<typename _Facet>
442 friend bool
443 has_facet(const locale&) throw ();
445 // NB: There is no accessor for _M_index because it may be used
446 // before the constructor is run; the effect of calling a member
447 // function (even an inline) would be undefined.
448 mutable size_t _M_index;
450 // Last id number assigned.
451 static _Atomic_word _S_refcount;
453 void
454 operator=(const id&); // Not defined.
456 id(const id&); // Not defined.
458 public:
459 // NB: This class is always a static data member, and thus can be
460 // counted on to be zero-initialized.
461 /// Constructor.
462 id() { }
464 size_t
465 _M_id() const;
469 // Implementation object for locale.
470 class locale::_Impl
472 public:
473 // Friends.
474 friend class locale;
475 friend class locale::facet;
477 template<typename _Facet>
478 friend bool
479 has_facet(const locale&) throw();
481 template<typename _Facet>
482 friend const _Facet&
483 use_facet(const locale&);
485 template<typename _Cache>
486 friend struct __use_cache;
488 private:
489 // Data Members.
490 _Atomic_word _M_refcount;
491 const facet** _M_facets;
492 size_t _M_facets_size;
493 const facet** _M_caches;
494 char** _M_names;
495 static const locale::id* const _S_id_ctype[];
496 static const locale::id* const _S_id_numeric[];
497 static const locale::id* const _S_id_collate[];
498 static const locale::id* const _S_id_time[];
499 static const locale::id* const _S_id_monetary[];
500 static const locale::id* const _S_id_messages[];
501 static const locale::id* const* const _S_facet_categories[];
503 void
504 _M_add_reference() throw()
505 { __gnu_cxx::__atomic_add_dispatch(&_M_refcount, 1); }
507 void
508 _M_remove_reference() throw()
510 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_refcount, -1) == 1)
513 { delete this; }
514 catch(...)
519 _Impl(const _Impl&, size_t);
520 _Impl(const char*, size_t);
521 _Impl(size_t) throw();
523 ~_Impl() throw();
525 _Impl(const _Impl&); // Not defined.
527 void
528 operator=(const _Impl&); // Not defined.
530 bool
531 _M_check_same_name()
533 bool __ret = true;
534 if (_M_names[1])
535 // We must actually compare all the _M_names: can be all equal!
536 for (size_t __i = 0; __ret && __i < _S_categories_size - 1; ++__i)
537 __ret = __builtin_strcmp(_M_names[__i], _M_names[__i + 1]) == 0;
538 return __ret;
541 void
542 _M_replace_categories(const _Impl*, category);
544 void
545 _M_replace_category(const _Impl*, const locale::id* const*);
547 void
548 _M_replace_facet(const _Impl*, const locale::id*);
550 void
551 _M_install_facet(const locale::id*, const facet*);
553 template<typename _Facet>
554 void
555 _M_init_facet(_Facet* __facet)
556 { _M_install_facet(&_Facet::id, __facet); }
558 void
559 _M_install_cache(const facet*, size_t);
564 * @brief Test for the presence of a facet.
566 * has_facet tests the locale argument for the presence of the facet type
567 * provided as the template parameter. Facets derived from the facet
568 * parameter will also return true.
570 * @param Facet The facet type to test the presence of.
571 * @param locale The locale to test.
572 * @return true if locale contains a facet of type Facet, else false.
574 template<typename _Facet>
575 bool
576 has_facet(const locale& __loc) throw();
579 * @brief Return a facet.
581 * use_facet looks for and returns a reference to a facet of type Facet
582 * where Facet is the template parameter. If has_facet(locale) is true,
583 * there is a suitable facet to return. It throws std::bad_cast if the
584 * locale doesn't contain a facet of type Facet.
586 * @param Facet The facet type to access.
587 * @param locale The locale to use.
588 * @return Reference to facet of type Facet.
589 * @throw std::bad_cast if locale doesn't contain a facet of type Facet.
591 template<typename _Facet>
592 const _Facet&
593 use_facet(const locale& __loc);
597 * @brief Facet for localized string comparison.
599 * This facet encapsulates the code to compare strings in a localized
600 * manner.
602 * The collate template uses protected virtual functions to provide
603 * the actual results. The public accessors forward the call to
604 * the virtual functions. These virtual functions are hooks for
605 * developers to implement the behavior they require from the
606 * collate facet.
608 template<typename _CharT>
609 class collate : public locale::facet
611 public:
612 // Types:
613 //@{
614 /// Public typedefs
615 typedef _CharT char_type;
616 typedef basic_string<_CharT> string_type;
617 //@}
619 protected:
620 // Underlying "C" library locale information saved from
621 // initialization, needed by collate_byname as well.
622 __c_locale _M_c_locale_collate;
624 public:
625 /// Numpunct facet id.
626 static locale::id id;
629 * @brief Constructor performs initialization.
631 * This is the constructor provided by the standard.
633 * @param refs Passed to the base facet class.
635 explicit
636 collate(size_t __refs = 0)
637 : facet(__refs), _M_c_locale_collate(_S_get_c_locale())
641 * @brief Internal constructor. Not for general use.
643 * This is a constructor for use by the library itself to set up new
644 * locales.
646 * @param cloc The "C" locale.
647 * @param refs Passed to the base facet class.
649 explicit
650 collate(__c_locale __cloc, size_t __refs = 0)
651 : facet(__refs), _M_c_locale_collate(_S_clone_c_locale(__cloc))
655 * @brief Compare two strings.
657 * This function compares two strings and returns the result by calling
658 * collate::do_compare().
660 * @param lo1 Start of string 1.
661 * @param hi1 End of string 1.
662 * @param lo2 Start of string 2.
663 * @param hi2 End of string 2.
664 * @return 1 if string1 > string2, -1 if string1 < string2, else 0.
667 compare(const _CharT* __lo1, const _CharT* __hi1,
668 const _CharT* __lo2, const _CharT* __hi2) const
669 { return this->do_compare(__lo1, __hi1, __lo2, __hi2); }
672 * @brief Transform string to comparable form.
674 * This function is a wrapper for strxfrm functionality. It takes the
675 * input string and returns a modified string that can be directly
676 * compared to other transformed strings. In the "C" locale, this
677 * function just returns a copy of the input string. In some other
678 * locales, it may replace two chars with one, change a char for
679 * another, etc. It does so by returning collate::do_transform().
681 * @param lo Start of string.
682 * @param hi End of string.
683 * @return Transformed string_type.
685 string_type
686 transform(const _CharT* __lo, const _CharT* __hi) const
687 { return this->do_transform(__lo, __hi); }
690 * @brief Return hash of a string.
692 * This function computes and returns a hash on the input string. It
693 * does so by returning collate::do_hash().
695 * @param lo Start of string.
696 * @param hi End of string.
697 * @return Hash value.
699 long
700 hash(const _CharT* __lo, const _CharT* __hi) const
701 { return this->do_hash(__lo, __hi); }
703 // Used to abstract out _CharT bits in virtual member functions, below.
705 _M_compare(const _CharT*, const _CharT*) const;
707 size_t
708 _M_transform(_CharT*, const _CharT*, size_t) const;
710 protected:
711 /// Destructor.
712 virtual
713 ~collate()
714 { _S_destroy_c_locale(_M_c_locale_collate); }
717 * @brief Compare two strings.
719 * This function is a hook for derived classes to change the value
720 * returned. @see compare().
722 * @param lo1 Start of string 1.
723 * @param hi1 End of string 1.
724 * @param lo2 Start of string 2.
725 * @param hi2 End of string 2.
726 * @return 1 if string1 > string2, -1 if string1 < string2, else 0.
728 virtual int
729 do_compare(const _CharT* __lo1, const _CharT* __hi1,
730 const _CharT* __lo2, const _CharT* __hi2) const;
733 * @brief Transform string to comparable form.
735 * This function is a hook for derived classes to change the value
736 * returned.
738 * @param lo1 Start of string 1.
739 * @param hi1 End of string 1.
740 * @param lo2 Start of string 2.
741 * @param hi2 End of string 2.
742 * @return 1 if string1 > string2, -1 if string1 < string2, else 0.
744 virtual string_type
745 do_transform(const _CharT* __lo, const _CharT* __hi) const;
748 * @brief Return hash of a string.
750 * This function computes and returns a hash on the input string. This
751 * function is a hook for derived classes to change the value returned.
753 * @param lo Start of string.
754 * @param hi End of string.
755 * @return Hash value.
757 virtual long
758 do_hash(const _CharT* __lo, const _CharT* __hi) const;
761 template<typename _CharT>
762 locale::id collate<_CharT>::id;
764 // Specializations.
765 template<>
767 collate<char>::_M_compare(const char*, const char*) const;
769 template<>
770 size_t
771 collate<char>::_M_transform(char*, const char*, size_t) const;
773 #ifdef _GLIBCXX_USE_WCHAR_T
774 template<>
776 collate<wchar_t>::_M_compare(const wchar_t*, const wchar_t*) const;
778 template<>
779 size_t
780 collate<wchar_t>::_M_transform(wchar_t*, const wchar_t*, size_t) const;
781 #endif
783 /// class collate_byname [22.2.4.2].
784 template<typename _CharT>
785 class collate_byname : public collate<_CharT>
787 public:
788 //@{
789 /// Public typedefs
790 typedef _CharT char_type;
791 typedef basic_string<_CharT> string_type;
792 //@}
794 explicit
795 collate_byname(const char* __s, size_t __refs = 0)
796 : collate<_CharT>(__refs)
798 if (__builtin_strcmp(__s, "C") != 0
799 && __builtin_strcmp(__s, "POSIX") != 0)
801 this->_S_destroy_c_locale(this->_M_c_locale_collate);
802 this->_S_create_c_locale(this->_M_c_locale_collate, __s);
806 protected:
807 virtual
808 ~collate_byname() { }
811 _GLIBCXX_END_NAMESPACE
813 #ifndef _GLIBCXX_EXPORT_TEMPLATE
814 # include <bits/locale_classes.tcc>
815 #endif
817 #endif