libstdc++
locale_classes.h
Go to the documentation of this file.
00001 // Locale support -*- C++ -*-
00002 
00003 // Copyright (C) 1997-2016 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /** @file bits/locale_classes.h
00026  *  This is an internal header file, included by other library headers.
00027  *  Do not attempt to use it directly. @headername{locale}
00028  */
00029 
00030 //
00031 // ISO C++ 14882: 22.1  Locales
00032 //
00033 
00034 #ifndef _LOCALE_CLASSES_H
00035 #define _LOCALE_CLASSES_H 1
00036 
00037 #pragma GCC system_header
00038 
00039 #include <bits/localefwd.h>
00040 #include <string>
00041 #include <ext/atomicity.h>
00042 
00043 namespace std _GLIBCXX_VISIBILITY(default)
00044 {
00045 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00046 
00047   // 22.1.1 Class locale
00048   /**
00049    *  @brief  Container class for localization functionality.
00050    *  @ingroup locales
00051    *
00052    *  The locale class is first a class wrapper for C library locales.  It is
00053    *  also an extensible container for user-defined localization.  A locale is
00054    *  a collection of facets that implement various localization features such
00055    *  as money, time, and number printing.
00056    *
00057    *  Constructing C++ locales does not change the C library locale.
00058    *
00059    *  This library supports efficient construction and copying of locales
00060    *  through a reference counting implementation of the locale class.
00061   */
00062   class locale
00063   {
00064   public:
00065     // Types:
00066     /// Definition of locale::category.
00067     typedef int category;
00068 
00069     // Forward decls and friends:
00070     class facet;
00071     class id;
00072     class _Impl;
00073 
00074     friend class facet;
00075     friend class _Impl;
00076 
00077     template<typename _Facet>
00078       friend bool
00079       has_facet(const locale&) throw();
00080 
00081     template<typename _Facet>
00082       friend const _Facet&
00083       use_facet(const locale&);
00084 
00085     template<typename _Cache>
00086       friend struct __use_cache;
00087 
00088     //@{
00089     /**
00090      *  @brief  Category values.
00091      *
00092      *  The standard category values are none, ctype, numeric, collate, time,
00093      *  monetary, and messages.  They form a bitmask that supports union and
00094      *  intersection.  The category all is the union of these values.
00095      *
00096      *  NB: Order must match _S_facet_categories definition in locale.cc
00097     */
00098     static const category none          = 0;
00099     static const category ctype         = 1L << 0;
00100     static const category numeric       = 1L << 1;
00101     static const category collate       = 1L << 2;
00102     static const category time          = 1L << 3;
00103     static const category monetary      = 1L << 4;
00104     static const category messages      = 1L << 5;
00105     static const category all           = (ctype | numeric | collate |
00106                                            time  | monetary | messages);
00107     //@}
00108 
00109     // Construct/copy/destroy:
00110 
00111     /**
00112      *  @brief  Default constructor.
00113      *
00114      *  Constructs a copy of the global locale.  If no locale has been
00115      *  explicitly set, this is the C locale.
00116     */
00117     locale() throw();
00118 
00119     /**
00120      *  @brief  Copy constructor.
00121      *
00122      *  Constructs a copy of @a other.
00123      *
00124      *  @param  __other  The locale to copy.
00125     */
00126     locale(const locale& __other) throw();
00127 
00128     /**
00129      *  @brief  Named locale constructor.
00130      *
00131      *  Constructs a copy of the named C library locale.
00132      *
00133      *  @param  __s  Name of the locale to construct.
00134      *  @throw  std::runtime_error if __s is null or an undefined locale.
00135     */
00136     explicit
00137     locale(const char* __s);
00138 
00139     /**
00140      *  @brief  Construct locale with facets from another locale.
00141      *
00142      *  Constructs a copy of the locale @a base.  The facets specified by @a
00143      *  cat are replaced with those from the locale named by @a s.  If base is
00144      *  named, this locale instance will also be named.
00145      *
00146      *  @param  __base  The locale to copy.
00147      *  @param  __s  Name of the locale to use facets from.
00148      *  @param  __cat  Set of categories defining the facets to use from __s.
00149      *  @throw  std::runtime_error if __s is null or an undefined locale.
00150     */
00151     locale(const locale& __base, const char* __s, category __cat);
00152 
00153 #if __cplusplus >= 201103L
00154     /**
00155      *  @brief  Named locale constructor.
00156      *
00157      *  Constructs a copy of the named C library locale.
00158      *
00159      *  @param  __s  Name of the locale to construct.
00160      *  @throw  std::runtime_error if __s is an undefined locale.
00161     */
00162     explicit
00163     locale(const std::string& __s) : locale(__s.c_str()) { }
00164 
00165     /**
00166      *  @brief  Construct locale with facets from another locale.
00167      *
00168      *  Constructs a copy of the locale @a base.  The facets specified by @a
00169      *  cat are replaced with those from the locale named by @a s.  If base is
00170      *  named, this locale instance will also be named.
00171      *
00172      *  @param  __base  The locale to copy.
00173      *  @param  __s  Name of the locale to use facets from.
00174      *  @param  __cat  Set of categories defining the facets to use from __s.
00175      *  @throw  std::runtime_error if __s is an undefined locale.
00176     */
00177     locale(const locale& __base, const std::string& __s, category __cat)
00178     : locale(__base, __s.c_str(), __cat) { }
00179 #endif
00180 
00181     /**
00182      *  @brief  Construct locale with facets from another locale.
00183      *
00184      *  Constructs a copy of the locale @a base.  The facets specified by @a
00185      *  cat are replaced with those from the locale @a add.  If @a base and @a
00186      *  add are named, this locale instance will also be named.
00187      *
00188      *  @param  __base  The locale to copy.
00189      *  @param  __add  The locale to use facets from.
00190      *  @param  __cat  Set of categories defining the facets to use from add.
00191     */
00192     locale(const locale& __base, const locale& __add, category __cat);
00193 
00194     /**
00195      *  @brief  Construct locale with another facet.
00196      *
00197      *  Constructs a copy of the locale @a __other.  The facet @a __f
00198      *  is added to @a __other, replacing an existing facet of type
00199      *  Facet if there is one.  If @a __f is null, this locale is a
00200      *  copy of @a __other.
00201      *
00202      *  @param  __other  The locale to copy.
00203      *  @param  __f  The facet to add in.
00204     */
00205     template<typename _Facet>
00206       locale(const locale& __other, _Facet* __f);
00207 
00208     /// Locale destructor.
00209     ~locale() throw();
00210 
00211     /**
00212      *  @brief  Assignment operator.
00213      *
00214      *  Set this locale to be a copy of @a other.
00215      *
00216      *  @param  __other  The locale to copy.
00217      *  @return  A reference to this locale.
00218     */
00219     const locale&
00220     operator=(const locale& __other) throw();
00221 
00222     /**
00223      *  @brief  Construct locale with another facet.
00224      *
00225      *  Constructs and returns a new copy of this locale.  Adds or replaces an
00226      *  existing facet of type Facet from the locale @a other into the new
00227      *  locale.
00228      *
00229      *  @tparam  _Facet  The facet type to copy from other
00230      *  @param  __other  The locale to copy from.
00231      *  @return  Newly constructed locale.
00232      *  @throw  std::runtime_error if __other has no facet of type _Facet.
00233     */
00234     template<typename _Facet>
00235       locale
00236       combine(const locale& __other) const;
00237 
00238     // Locale operations:
00239     /**
00240      *  @brief  Return locale name.
00241      *  @return  Locale name or "*" if unnamed.
00242     */
00243     _GLIBCXX_DEFAULT_ABI_TAG
00244     string
00245     name() const;
00246 
00247     /**
00248      *  @brief  Locale equality.
00249      *
00250      *  @param  __other  The locale to compare against.
00251      *  @return  True if other and this refer to the same locale instance, are
00252      *           copies, or have the same name.  False otherwise.
00253     */
00254     bool
00255     operator==(const locale& __other) const throw();
00256 
00257     /**
00258      *  @brief  Locale inequality.
00259      *
00260      *  @param  __other  The locale to compare against.
00261      *  @return  ! (*this == __other)
00262     */
00263     bool
00264     operator!=(const locale& __other) const throw()
00265     { return !(this->operator==(__other)); }
00266 
00267     /**
00268      *  @brief  Compare two strings according to collate.
00269      *
00270      *  Template operator to compare two strings using the compare function of
00271      *  the collate facet in this locale.  One use is to provide the locale to
00272      *  the sort function.  For example, a vector v of strings could be sorted
00273      *  according to locale loc by doing:
00274      *  @code
00275      *  std::sort(v.begin(), v.end(), loc);
00276      *  @endcode
00277      *
00278      *  @param  __s1  First string to compare.
00279      *  @param  __s2  Second string to compare.
00280      *  @return  True if collate<_Char> facet compares __s1 < __s2, else false.
00281     */
00282     template<typename _Char, typename _Traits, typename _Alloc>
00283       bool
00284       operator()(const basic_string<_Char, _Traits, _Alloc>& __s1,
00285                  const basic_string<_Char, _Traits, _Alloc>& __s2) const;
00286 
00287     // Global locale objects:
00288     /**
00289      *  @brief  Set global locale
00290      *
00291      *  This function sets the global locale to the argument and returns a
00292      *  copy of the previous global locale.  If the argument has a name, it
00293      *  will also call std::setlocale(LC_ALL, loc.name()).
00294      *
00295      *  @param  __loc  The new locale to make global.
00296      *  @return  Copy of the old global locale.
00297     */
00298     static locale
00299     global(const locale& __loc);
00300 
00301     /**
00302      *  @brief  Return reference to the C locale.
00303     */
00304     static const locale&
00305     classic();
00306 
00307   private:
00308     // The (shared) implementation
00309     _Impl*              _M_impl;
00310 
00311     // The "C" reference locale
00312     static _Impl*       _S_classic;
00313 
00314     // Current global locale
00315     static _Impl*       _S_global;
00316 
00317     // Names of underlying locale categories.
00318     // NB: locale::global() has to know how to modify all the
00319     // underlying categories, not just the ones required by the C++
00320     // standard.
00321     static const char* const* const _S_categories;
00322 
00323     // Number of standard categories. For C++, these categories are
00324     // collate, ctype, monetary, numeric, time, and messages. These
00325     // directly correspond to ISO C99 macros LC_COLLATE, LC_CTYPE,
00326     // LC_MONETARY, LC_NUMERIC, and LC_TIME. In addition, POSIX (IEEE
00327     // 1003.1-2001) specifies LC_MESSAGES.
00328     // In addition to the standard categories, the underlying
00329     // operating system is allowed to define extra LC_*
00330     // macros. For GNU systems, the following are also valid:
00331     // LC_PAPER, LC_NAME, LC_ADDRESS, LC_TELEPHONE, LC_MEASUREMENT,
00332     // and LC_IDENTIFICATION.
00333     enum { _S_categories_size = 6 + _GLIBCXX_NUM_CATEGORIES };
00334 
00335 #ifdef __GTHREADS
00336     static __gthread_once_t _S_once;
00337 #endif
00338 
00339     explicit
00340     locale(_Impl*) throw();
00341 
00342     static void
00343     _S_initialize();
00344 
00345     static void
00346     _S_initialize_once() throw();
00347 
00348     static category
00349     _S_normalize_category(category);
00350 
00351     void
00352     _M_coalesce(const locale& __base, const locale& __add, category __cat);
00353 
00354 #if _GLIBCXX_USE_CXX11_ABI
00355     static const id* const _S_twinned_facets[];
00356 #endif
00357   };
00358 
00359 
00360   // 22.1.1.1.2  Class locale::facet
00361   /**
00362    *  @brief  Localization functionality base class.
00363    *  @ingroup locales
00364    *
00365    *  The facet class is the base class for a localization feature, such as
00366    *  money, time, and number printing.  It provides common support for facets
00367    *  and reference management.
00368    *
00369    *  Facets may not be copied or assigned.
00370   */
00371   class locale::facet
00372   {
00373   private:
00374     friend class locale;
00375     friend class locale::_Impl;
00376 
00377     mutable _Atomic_word                _M_refcount;
00378 
00379     // Contains data from the underlying "C" library for the classic locale.
00380     static __c_locale                   _S_c_locale;
00381 
00382     // String literal for the name of the classic locale.
00383     static const char                   _S_c_name[2];
00384 
00385 #ifdef __GTHREADS
00386     static __gthread_once_t             _S_once;
00387 #endif
00388 
00389     static void
00390     _S_initialize_once();
00391 
00392   protected:
00393     /**
00394      *  @brief  Facet constructor.
00395      *
00396      *  This is the constructor provided by the standard.  If refs is 0, the
00397      *  facet is destroyed when the last referencing locale is destroyed.
00398      *  Otherwise the facet will never be destroyed.
00399      *
00400      *  @param __refs  The initial value for reference count.
00401     */
00402     explicit
00403     facet(size_t __refs = 0) throw() : _M_refcount(__refs ? 1 : 0)
00404     { }
00405 
00406     /// Facet destructor.
00407     virtual
00408     ~facet();
00409 
00410     static void
00411     _S_create_c_locale(__c_locale& __cloc, const char* __s,
00412                        __c_locale __old = 0);
00413 
00414     static __c_locale
00415     _S_clone_c_locale(__c_locale& __cloc) throw();
00416 
00417     static void
00418     _S_destroy_c_locale(__c_locale& __cloc);
00419 
00420     static __c_locale
00421     _S_lc_ctype_c_locale(__c_locale __cloc, const char* __s);
00422 
00423     // Returns data from the underlying "C" library data for the
00424     // classic locale.
00425     static __c_locale
00426     _S_get_c_locale();
00427 
00428     _GLIBCXX_CONST static const char*
00429     _S_get_c_name() throw();
00430 
00431 #if __cplusplus < 201103L
00432   private:
00433     facet(const facet&);  // Not defined.
00434 
00435     facet&
00436     operator=(const facet&);  // Not defined.
00437 #else
00438     facet(const facet&) = delete;
00439 
00440     facet&
00441     operator=(const facet&) = delete;
00442 #endif
00443 
00444   private:
00445     void
00446     _M_add_reference() const throw()
00447     { __gnu_cxx::__atomic_add_dispatch(&_M_refcount, 1); }
00448 
00449     void
00450     _M_remove_reference() const throw()
00451     {
00452       // Be race-detector-friendly.  For more info see bits/c++config.
00453       _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_refcount);
00454       if (__gnu_cxx::__exchange_and_add_dispatch(&_M_refcount, -1) == 1)
00455         {
00456           _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_refcount);
00457           __try
00458             { delete this; }
00459           __catch(...)
00460             { }
00461         }
00462     }
00463 
00464     class __shim;
00465 
00466     const facet* _M_sso_shim(const id*) const;
00467     const facet* _M_cow_shim(const id*) const;
00468   };
00469 
00470 
00471   // 22.1.1.1.3 Class locale::id
00472   /**
00473    *  @brief  Facet ID class.
00474    *  @ingroup locales
00475    *
00476    *  The ID class provides facets with an index used to identify them.
00477    *  Every facet class must define a public static member locale::id, or be
00478    *  derived from a facet that provides this member, otherwise the facet
00479    *  cannot be used in a locale.  The locale::id ensures that each class
00480    *  type gets a unique identifier.
00481   */
00482   class locale::id
00483   {
00484   private:
00485     friend class locale;
00486     friend class locale::_Impl;
00487 
00488     template<typename _Facet>
00489       friend const _Facet&
00490       use_facet(const locale&);
00491 
00492     template<typename _Facet>
00493       friend bool
00494       has_facet(const locale&) throw();
00495 
00496     // NB: There is no accessor for _M_index because it may be used
00497     // before the constructor is run; the effect of calling a member
00498     // function (even an inline) would be undefined.
00499     mutable size_t              _M_index;
00500 
00501     // Last id number assigned.
00502     static _Atomic_word         _S_refcount;
00503 
00504     void
00505     operator=(const id&);  // Not defined.
00506 
00507     id(const id&);  // Not defined.
00508 
00509   public:
00510     // NB: This class is always a static data member, and thus can be
00511     // counted on to be zero-initialized.
00512     /// Constructor.
00513     id() { }
00514 
00515     size_t
00516     _M_id() const throw();
00517   };
00518 
00519 
00520   // Implementation object for locale.
00521   class locale::_Impl
00522   {
00523   public:
00524     // Friends.
00525     friend class locale;
00526     friend class locale::facet;
00527 
00528     template<typename _Facet>
00529       friend bool
00530       has_facet(const locale&) throw();
00531 
00532     template<typename _Facet>
00533       friend const _Facet&
00534       use_facet(const locale&);
00535 
00536     template<typename _Cache>
00537       friend struct __use_cache;
00538 
00539   private:
00540     // Data Members.
00541     _Atomic_word                        _M_refcount;
00542     const facet**                       _M_facets;
00543     size_t                              _M_facets_size;
00544     const facet**                       _M_caches;
00545     char**                              _M_names;
00546     static const locale::id* const      _S_id_ctype[];
00547     static const locale::id* const      _S_id_numeric[];
00548     static const locale::id* const      _S_id_collate[];
00549     static const locale::id* const      _S_id_time[];
00550     static const locale::id* const      _S_id_monetary[];
00551     static const locale::id* const      _S_id_messages[];
00552     static const locale::id* const* const _S_facet_categories[];
00553 
00554     void
00555     _M_add_reference() throw()
00556     { __gnu_cxx::__atomic_add_dispatch(&_M_refcount, 1); }
00557 
00558     void
00559     _M_remove_reference() throw()
00560     {
00561       // Be race-detector-friendly.  For more info see bits/c++config.
00562       _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_refcount);
00563       if (__gnu_cxx::__exchange_and_add_dispatch(&_M_refcount, -1) == 1)
00564         {
00565           _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_refcount);
00566           __try
00567             { delete this; }
00568           __catch(...)
00569             { }
00570         }
00571     }
00572 
00573     _Impl(const _Impl&, size_t);
00574     _Impl(const char*, size_t);
00575     _Impl(size_t) throw();
00576 
00577    ~_Impl() throw();
00578 
00579     _Impl(const _Impl&);  // Not defined.
00580 
00581     void
00582     operator=(const _Impl&);  // Not defined.
00583 
00584     bool
00585     _M_check_same_name()
00586     {
00587       bool __ret = true;
00588       if (_M_names[1])
00589         // We must actually compare all the _M_names: can be all equal!
00590         for (size_t __i = 0; __ret && __i < _S_categories_size - 1; ++__i)
00591           __ret = __builtin_strcmp(_M_names[__i], _M_names[__i + 1]) == 0;
00592       return __ret;
00593     }
00594 
00595     void
00596     _M_replace_categories(const _Impl*, category);
00597 
00598     void
00599     _M_replace_category(const _Impl*, const locale::id* const*);
00600 
00601     void
00602     _M_replace_facet(const _Impl*, const locale::id*);
00603 
00604     void
00605     _M_install_facet(const locale::id*, const facet*);
00606 
00607     template<typename _Facet>
00608       void
00609       _M_init_facet(_Facet* __facet)
00610       { _M_install_facet(&_Facet::id, __facet); }
00611 
00612     template<typename _Facet>
00613       void
00614       _M_init_facet_unchecked(_Facet* __facet)
00615       {
00616         __facet->_M_add_reference();
00617         _M_facets[_Facet::id._M_id()] = __facet;
00618       }
00619 
00620     void
00621     _M_install_cache(const facet*, size_t);
00622 
00623     void _M_init_extra(facet**);
00624     void _M_init_extra(void*, void*, const char*, const char*);
00625   };
00626 
00627 
00628   /**
00629    *  @brief  Facet for localized string comparison.
00630    *
00631    *  This facet encapsulates the code to compare strings in a localized
00632    *  manner.
00633    *
00634    *  The collate template uses protected virtual functions to provide
00635    *  the actual results.  The public accessors forward the call to
00636    *  the virtual functions.  These virtual functions are hooks for
00637    *  developers to implement the behavior they require from the
00638    *  collate facet.
00639   */
00640   template<typename _CharT>
00641     class _GLIBCXX_NAMESPACE_CXX11 collate : public locale::facet
00642     {
00643     public:
00644       // Types:
00645       //@{
00646       /// Public typedefs
00647       typedef _CharT                    char_type;
00648       typedef basic_string<_CharT>      string_type;
00649       //@}
00650 
00651     protected:
00652       // Underlying "C" library locale information saved from
00653       // initialization, needed by collate_byname as well.
00654       __c_locale                        _M_c_locale_collate;
00655 
00656     public:
00657       /// Numpunct facet id.
00658       static locale::id                 id;
00659 
00660       /**
00661        *  @brief  Constructor performs initialization.
00662        *
00663        *  This is the constructor provided by the standard.
00664        *
00665        *  @param __refs  Passed to the base facet class.
00666       */
00667       explicit
00668       collate(size_t __refs = 0)
00669       : facet(__refs), _M_c_locale_collate(_S_get_c_locale())
00670       { }
00671 
00672       /**
00673        *  @brief  Internal constructor. Not for general use.
00674        *
00675        *  This is a constructor for use by the library itself to set up new
00676        *  locales.
00677        *
00678        *  @param __cloc  The C locale.
00679        *  @param __refs  Passed to the base facet class.
00680       */
00681       explicit
00682       collate(__c_locale __cloc, size_t __refs = 0)
00683       : facet(__refs), _M_c_locale_collate(_S_clone_c_locale(__cloc))
00684       { }
00685 
00686       /**
00687        *  @brief  Compare two strings.
00688        *
00689        *  This function compares two strings and returns the result by calling
00690        *  collate::do_compare().
00691        *
00692        *  @param __lo1  Start of string 1.
00693        *  @param __hi1  End of string 1.
00694        *  @param __lo2  Start of string 2.
00695        *  @param __hi2  End of string 2.
00696        *  @return  1 if string1 > string2, -1 if string1 < string2, else 0.
00697       */
00698       int
00699       compare(const _CharT* __lo1, const _CharT* __hi1,
00700               const _CharT* __lo2, const _CharT* __hi2) const
00701       { return this->do_compare(__lo1, __hi1, __lo2, __hi2); }
00702 
00703       /**
00704        *  @brief  Transform string to comparable form.
00705        *
00706        *  This function is a wrapper for strxfrm functionality.  It takes the
00707        *  input string and returns a modified string that can be directly
00708        *  compared to other transformed strings.  In the C locale, this
00709        *  function just returns a copy of the input string.  In some other
00710        *  locales, it may replace two chars with one, change a char for
00711        *  another, etc.  It does so by returning collate::do_transform().
00712        *
00713        *  @param __lo  Start of string.
00714        *  @param __hi  End of string.
00715        *  @return  Transformed string_type.
00716       */
00717       string_type
00718       transform(const _CharT* __lo, const _CharT* __hi) const
00719       { return this->do_transform(__lo, __hi); }
00720 
00721       /**
00722        *  @brief  Return hash of a string.
00723        *
00724        *  This function computes and returns a hash on the input string.  It
00725        *  does so by returning collate::do_hash().
00726        *
00727        *  @param __lo  Start of string.
00728        *  @param __hi  End of string.
00729        *  @return  Hash value.
00730       */
00731       long
00732       hash(const _CharT* __lo, const _CharT* __hi) const
00733       { return this->do_hash(__lo, __hi); }
00734 
00735       // Used to abstract out _CharT bits in virtual member functions, below.
00736       int
00737       _M_compare(const _CharT*, const _CharT*) const throw();
00738 
00739       size_t
00740       _M_transform(_CharT*, const _CharT*, size_t) const throw();
00741 
00742   protected:
00743       /// Destructor.
00744       virtual
00745       ~collate()
00746       { _S_destroy_c_locale(_M_c_locale_collate); }
00747 
00748       /**
00749        *  @brief  Compare two strings.
00750        *
00751        *  This function is a hook for derived classes to change the value
00752        *  returned.  @see compare().
00753        *
00754        *  @param __lo1  Start of string 1.
00755        *  @param __hi1  End of string 1.
00756        *  @param __lo2  Start of string 2.
00757        *  @param __hi2  End of string 2.
00758        *  @return  1 if string1 > string2, -1 if string1 < string2, else 0.
00759       */
00760       virtual int
00761       do_compare(const _CharT* __lo1, const _CharT* __hi1,
00762                  const _CharT* __lo2, const _CharT* __hi2) const;
00763 
00764       /**
00765        *  @brief  Transform string to comparable form.
00766        *
00767        *  This function is a hook for derived classes to change the value
00768        *  returned.
00769        *
00770        *  @param __lo  Start.
00771        *  @param __hi  End.
00772        *  @return  transformed string.
00773       */
00774       virtual string_type
00775       do_transform(const _CharT* __lo, const _CharT* __hi) const;
00776 
00777       /**
00778        *  @brief  Return hash of a string.
00779        *
00780        *  This function computes and returns a hash on the input string.  This
00781        *  function is a hook for derived classes to change the value returned.
00782        *
00783        *  @param __lo  Start of string.
00784        *  @param __hi  End of string.
00785        *  @return  Hash value.
00786       */
00787       virtual long
00788       do_hash(const _CharT* __lo, const _CharT* __hi) const;
00789     };
00790 
00791   template<typename _CharT>
00792     locale::id collate<_CharT>::id;
00793 
00794   // Specializations.
00795   template<>
00796     int
00797     collate<char>::_M_compare(const char*, const char*) const throw();
00798 
00799   template<>
00800     size_t
00801     collate<char>::_M_transform(char*, const char*, size_t) const throw();
00802 
00803 #ifdef _GLIBCXX_USE_WCHAR_T
00804   template<>
00805     int
00806     collate<wchar_t>::_M_compare(const wchar_t*, const wchar_t*) const throw();
00807 
00808   template<>
00809     size_t
00810     collate<wchar_t>::_M_transform(wchar_t*, const wchar_t*, size_t) const throw();
00811 #endif
00812 
00813   /// class collate_byname [22.2.4.2].
00814   template<typename _CharT>
00815     class _GLIBCXX_NAMESPACE_CXX11 collate_byname : public collate<_CharT>
00816     {
00817     public:
00818       //@{
00819       /// Public typedefs
00820       typedef _CharT               char_type;
00821       typedef basic_string<_CharT> string_type;
00822       //@}
00823 
00824       explicit
00825       collate_byname(const char* __s, size_t __refs = 0)
00826       : collate<_CharT>(__refs)
00827       {
00828         if (__builtin_strcmp(__s, "C") != 0
00829             && __builtin_strcmp(__s, "POSIX") != 0)
00830           {
00831             this->_S_destroy_c_locale(this->_M_c_locale_collate);
00832             this->_S_create_c_locale(this->_M_c_locale_collate, __s);
00833           }
00834       }
00835 
00836 #if __cplusplus >= 201103L
00837       explicit
00838       collate_byname(const string& __s, size_t __refs = 0)
00839       : collate_byname(__s.c_str(), __refs) { }
00840 #endif
00841 
00842     protected:
00843       virtual
00844       ~collate_byname() { }
00845     };
00846 
00847 _GLIBCXX_END_NAMESPACE_VERSION
00848 } // namespace
00849 
00850 # include <bits/locale_classes.tcc>
00851 
00852 #endif