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