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, 2006
00004 // Free Software Foundation, Inc.
00005 //
00006 // This file is part of the GNU ISO C++ Library.  This library is free
00007 // software; you can redistribute it and/or modify it under the
00008 // terms of the GNU General Public License as published by the
00009 // Free Software Foundation; either version 2, or (at your option)
00010 // any later version.
00011 
00012 // This library is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 
00017 // You should have received a copy of the GNU General Public License along
00018 // with this library; see the file COPYING.  If not, write to the Free
00019 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
00020 // USA.
00021 
00022 // As a special exception, you may use this file as part of a free software
00023 // library without restriction.  Specifically, if other files instantiate
00024 // templates or use macros or inline functions from this file, or you compile
00025 // this file and link it with other files to produce an executable, this
00026 // file does not by itself cause the resulting executable to be covered by
00027 // the GNU General Public License.  This exception does not however
00028 // invalidate any other reasons why the executable file might be covered by
00029 // the GNU General Public License.
00030 
00031 /** @file locale_classes.h
00032  *  This is an internal header file, included by other library headers.
00033  *  You should not attempt to use it directly.
00034  */
00035 
00036 //
00037 // ISO C++ 14882: 22.1  Locales
00038 //
00039 
00040 #ifndef _LOCALE_CLASSES_H
00041 #define _LOCALE_CLASSES_H 1
00042 
00043 #pragma GCC system_header
00044 
00045 #include <bits/localefwd.h>
00046 #include <cstring>      // For strcmp.
00047 #include <string>
00048 #include <ext/atomicity.h>
00049 
00050 _GLIBCXX_BEGIN_NAMESPACE(std)
00051 
00052   // 22.1.1 Class locale
00053   /**
00054    *  @brief  Container class for localization functionality.
00055    *
00056    *  The locale class is first a class wrapper for C library locales.  It is
00057    *  also an extensible container for user-defined localization.  A locale is
00058    *  a collection of facets that implement various localization features such
00059    *  as money, time, and number printing.
00060    *
00061    *  Constructing C++ locales does not change the C library locale.
00062    *
00063    *  This library supports efficient construction and copying of locales
00064    *  through a reference counting implementation of the locale class.
00065   */
00066   class locale
00067   {
00068   public:
00069     // Types:
00070     /// Definition of locale::category.
00071     typedef int category;
00072 
00073     // Forward decls and friends:
00074     class facet;
00075     class id;
00076     class _Impl;
00077 
00078     friend class facet;
00079     friend class _Impl;
00080 
00081     template<typename _Facet>
00082       friend bool
00083       has_facet(const locale&) throw();
00084 
00085     template<typename _Facet>
00086       friend const _Facet&
00087       use_facet(const locale&);
00088 
00089     template<typename _Cache>
00090       friend struct __use_cache;
00091 
00092     //@{
00093     /**
00094      *  @brief  Category values.
00095      *
00096      *  The standard category values are none, ctype, numeric, collate, time,
00097      *  monetary, and messages.  They form a bitmask that supports union and
00098      *  intersection.  The category all is the union of these values.
00099      *
00100      *  @if maint
00101      *  NB: Order must match _S_facet_categories definition in locale.cc
00102      *  @endif
00103     */
00104     static const category none      = 0;
00105     static const category ctype     = 1L << 0;
00106     static const category numeric   = 1L << 1;
00107     static const category collate   = 1L << 2;
00108     static const category time      = 1L << 3;
00109     static const category monetary  = 1L << 4;
00110     static const category messages  = 1L << 5;
00111     static const category all       = (ctype | numeric | collate |
00112                        time  | monetary | messages);
00113     //@}
00114 
00115     // Construct/copy/destroy:
00116 
00117     /**
00118      *  @brief  Default constructor.
00119      *
00120      *  Constructs a copy of the global locale.  If no locale has been
00121      *  explicitly set, this is the "C" locale.
00122     */
00123     locale() throw();
00124 
00125     /**
00126      *  @brief  Copy constructor.
00127      *
00128      *  Constructs a copy of @a other.
00129      *
00130      *  @param  other  The locale to copy.
00131     */
00132     locale(const locale& __other) throw();
00133 
00134     /**
00135      *  @brief  Named locale constructor.
00136      *
00137      *  Constructs a copy of the named C library locale.
00138      *
00139      *  @param  s  Name of the locale to construct.
00140      *  @throw  std::runtime_error if s is null or an undefined locale.
00141     */
00142     explicit
00143     locale(const char* __s);
00144 
00145     /**
00146      *  @brief  Construct locale with facets from another locale.
00147      *
00148      *  Constructs a copy of the locale @a base.  The facets specified by @a
00149      *  cat are replaced with those from the locale named by @a s.  If base is
00150      *  named, this locale instance will also be named.
00151      *
00152      *  @param  base  The locale to copy.
00153      *  @param  s  Name of the locale to use facets from.
00154      *  @param  cat  Set of categories defining the facets to use from s.
00155      *  @throw  std::runtime_error if s is null or an undefined locale.
00156     */
00157     locale(const locale& __base, const char* __s, category __cat);
00158 
00159     /**
00160      *  @brief  Construct locale with facets from another locale.
00161      *
00162      *  Constructs a copy of the locale @a base.  The facets specified by @a
00163      *  cat are replaced with those from the locale @a add.  If @a base and @a
00164      *  add are named, this locale instance will also be named.
00165      *
00166      *  @param  base  The locale to copy.
00167      *  @param  add  The locale to use facets from.
00168      *  @param  cat  Set of categories defining the facets to use from add.
00169     */
00170     locale(const locale& __base, const locale& __add, category __cat);
00171 
00172     /**
00173      *  @brief  Construct locale with another facet.
00174      *
00175      *  Constructs a copy of the locale @a other.  The facet @f is added to
00176      *  @other, replacing an existing facet of type Facet if there is one.  If
00177      *  @f is null, this locale is a copy of @a other.
00178      *
00179      *  @param  other  The locale to copy.
00180      *  @param  f  The facet to add in.
00181     */
00182     template<typename _Facet>
00183       locale(const locale& __other, _Facet* __f);
00184 
00185     /// Locale destructor.
00186     ~locale() throw();
00187 
00188     /**
00189      *  @brief  Assignment operator.
00190      *
00191      *  Set this locale to be a copy of @a other.
00192      *
00193      *  @param  other  The locale to copy.
00194      *  @return  A reference to this locale.
00195     */
00196     const locale&
00197     operator=(const locale& __other) throw();
00198 
00199     /**
00200      *  @brief  Construct locale with another facet.
00201      *
00202      *  Constructs and returns a new copy of this locale.  Adds or replaces an
00203      *  existing facet of type Facet from the locale @a other into the new
00204      *  locale.
00205      *
00206      *  @param  Facet  The facet type to copy from other
00207      *  @param  other  The locale to copy from.
00208      *  @return  Newly constructed locale.
00209      *  @throw  std::runtime_error if other has no facet of type Facet.
00210     */
00211     template<typename _Facet>
00212       locale
00213       combine(const locale& __other) const;
00214 
00215     // Locale operations:
00216     /**
00217      *  @brief  Return locale name.
00218      *  @return  Locale name or "*" if unnamed.
00219     */
00220     string
00221     name() const;
00222 
00223     /**
00224      *  @brief  Locale equality.
00225      *
00226      *  @param  other  The locale to compare against.
00227      *  @return  True if other and this refer to the same locale instance, are
00228      *       copies, or have the same name.  False otherwise.
00229     */
00230     bool
00231     operator==(const locale& __other) const throw ();
00232 
00233     /**
00234      *  @brief  Locale inequality.
00235      *
00236      *  @param  other  The locale to compare against.
00237      *  @return  ! (*this == other)
00238     */
00239     inline bool
00240     operator!=(const locale& __other) const throw ()
00241     { return !(this->operator==(__other));  }
00242 
00243     /**
00244      *  @brief  Compare two strings according to collate.
00245      *
00246      *  Template operator to compare two strings using the compare function of
00247      *  the collate facet in this locale.  One use is to provide the locale to
00248      *  the sort function.  For example, a vector v of strings could be sorted
00249      *  according to locale loc by doing:
00250      *  @code
00251      *  std::sort(v.begin(), v.end(), loc);
00252      *  @endcode
00253      *
00254      *  @param  s1  First string to compare.
00255      *  @param  s2  Second string to compare.
00256      *  @return  True if collate<Char> facet compares s1 < s2, else false.
00257     */
00258     template<typename _Char, typename _Traits, typename _Alloc>
00259       bool
00260       operator()(const basic_string<_Char, _Traits, _Alloc>& __s1,
00261          const basic_string<_Char, _Traits, _Alloc>& __s2) const;
00262 
00263     // Global locale objects:
00264     /**
00265      *  @brief  Set global locale
00266      *
00267      *  This function sets the global locale to the argument and returns a
00268      *  copy of the previous global locale.  If the argument has a name, it
00269      *  will also call std::setlocale(LC_ALL, loc.name()).
00270      *
00271      *  @param  locale  The new locale to make global.
00272      *  @return  Copy of the old global locale.
00273     */
00274     static locale
00275     global(const locale&);
00276 
00277     /**
00278      *  @brief  Return reference to the "C" locale.
00279     */
00280     static const locale&
00281     classic();
00282 
00283   private:
00284     // The (shared) implementation
00285     _Impl*      _M_impl;
00286 
00287     // The "C" reference locale
00288     static _Impl*       _S_classic;
00289 
00290     // Current global locale
00291     static _Impl*   _S_global;
00292 
00293     // Names of underlying locale categories.
00294     // NB: locale::global() has to know how to modify all the
00295     // underlying categories, not just the ones required by the C++
00296     // standard.
00297     static const char* const* const _S_categories;
00298 
00299     // Number of standard categories. For C++, these categories are
00300     // collate, ctype, monetary, numeric, time, and messages. These
00301     // directly correspond to ISO C99 macros LC_COLLATE, LC_CTYPE,
00302     // LC_MONETARY, LC_NUMERIC, and LC_TIME. In addition, POSIX (IEEE
00303     // 1003.1-2001) specifies LC_MESSAGES.
00304     // In addition to the standard categories, the underlying
00305     // operating system is allowed to define extra LC_*
00306     // macros. For GNU systems, the following are also valid:
00307     // LC_PAPER, LC_NAME, LC_ADDRESS, LC_TELEPHONE, LC_MEASUREMENT,
00308     // and LC_IDENTIFICATION.
00309     enum { _S_categories_size = 6 + _GLIBCXX_NUM_CATEGORIES };
00310 
00311 #ifdef __GTHREADS
00312     static __gthread_once_t _S_once;
00313 #endif
00314 
00315     explicit
00316     locale(_Impl*) throw();
00317 
00318     static void
00319     _S_initialize();
00320 
00321     static void
00322     _S_initialize_once();
00323 
00324     static category
00325     _S_normalize_category(category);
00326 
00327     void
00328     _M_coalesce(const locale& __base, const locale& __add, category __cat);
00329   };
00330 
00331 
00332   // 22.1.1.1.2  Class locale::facet
00333   /**
00334    *  @brief  Localization functionality base class.
00335    *
00336    *  The facet class is the base class for a localization feature, such as
00337    *  money, time, and number printing.  It provides common support for facets
00338    *  and reference management.
00339    *
00340    *  Facets may not be copied or assigned.
00341   */
00342   class locale::facet
00343   {
00344   private:
00345     friend class locale;
00346     friend class locale::_Impl;
00347 
00348     mutable _Atomic_word        _M_refcount;
00349 
00350     // Contains data from the underlying "C" library for the classic locale.
00351     static __c_locale                   _S_c_locale;
00352 
00353     // String literal for the name of the classic locale.
00354     static const char           _S_c_name[2];
00355 
00356 #ifdef __GTHREADS
00357     static __gthread_once_t     _S_once;
00358 #endif
00359 
00360     static void
00361     _S_initialize_once();
00362 
00363   protected:
00364     /**
00365      *  @brief  Facet constructor.
00366      *
00367      *  This is the constructor provided by the standard.  If refs is 0, the
00368      *  facet is destroyed when the last referencing locale is destroyed.
00369      *  Otherwise the facet will never be destroyed.
00370      *
00371      *  @param refs  The initial value for reference count.
00372     */
00373     explicit
00374     facet(size_t __refs = 0) throw() : _M_refcount(__refs ? 1 : 0)
00375     { }
00376 
00377     /// Facet destructor.
00378     virtual
00379     ~facet();
00380 
00381     static void
00382     _S_create_c_locale(__c_locale& __cloc, const char* __s,
00383                __c_locale __old = 0);
00384 
00385     static __c_locale
00386     _S_clone_c_locale(__c_locale& __cloc);
00387 
00388     static void
00389     _S_destroy_c_locale(__c_locale& __cloc);
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     static const char*
00397     _S_get_c_name();
00398 
00399   private:
00400     inline void
00401     _M_add_reference() const throw()
00402     { __gnu_cxx::__atomic_add_dispatch(&_M_refcount, 1); }
00403 
00404     inline void
00405     _M_remove_reference() const throw()
00406     {
00407       if (__gnu_cxx::__exchange_and_add_dispatch(&_M_refcount, -1) == 1)
00408     {
00409       try
00410         { delete this; }
00411       catch (...)
00412         { }
00413     }
00414     }
00415 
00416     facet(const facet&);  // Not defined.
00417 
00418     facet&
00419     operator=(const facet&);  // Not defined.
00420   };
00421 
00422 
00423   // 22.1.1.1.3 Class locale::id
00424   /**
00425    *  @brief  Facet ID class.
00426    *
00427    *  The ID class provides facets with an index used to identify them.
00428    *  Every facet class must define a public static member locale::id, or be
00429    *  derived from a facet that provides this member, otherwise the facet
00430    *  cannot be used in a locale.  The locale::id ensures that each class
00431    *  type gets a unique identifier.
00432   */
00433   class locale::id
00434   {
00435   private:
00436     friend class locale;
00437     friend class locale::_Impl;
00438 
00439     template<typename _Facet>
00440       friend const _Facet&
00441       use_facet(const locale&);
00442 
00443     template<typename _Facet>
00444       friend bool
00445       has_facet(const locale&) throw ();
00446 
00447     // NB: There is no accessor for _M_index because it may be used
00448     // before the constructor is run; the effect of calling a member
00449     // function (even an inline) would be undefined.
00450     mutable size_t      _M_index;
00451 
00452     // Last id number assigned.
00453     static _Atomic_word     _S_refcount;
00454 
00455     void
00456     operator=(const id&);  // Not defined.
00457 
00458     id(const id&);  // Not defined.
00459 
00460   public:
00461     // NB: This class is always a static data member, and thus can be
00462     // counted on to be zero-initialized.
00463     /// Constructor.
00464     id() { }
00465 
00466     size_t
00467     _M_id() const;
00468   };
00469 
00470 
00471   // Implementation object for locale.
00472   class locale::_Impl
00473   {
00474   public:
00475     // Friends.
00476     friend class locale;
00477     friend class locale::facet;
00478 
00479     template<typename _Facet>
00480       friend bool
00481       has_facet(const locale&) throw();
00482 
00483     template<typename _Facet>
00484       friend const _Facet&
00485       use_facet(const locale&);
00486 
00487     template<typename _Cache>
00488       friend struct __use_cache;
00489 
00490   private:
00491     // Data Members.
00492     _Atomic_word            _M_refcount;
00493     const facet**           _M_facets;
00494     size_t              _M_facets_size;
00495     const facet**           _M_caches;
00496     char**              _M_names;
00497     static const locale::id* const  _S_id_ctype[];
00498     static const locale::id* const  _S_id_numeric[];
00499     static const locale::id* const  _S_id_collate[];
00500     static const locale::id* const  _S_id_time[];
00501     static const locale::id* const  _S_id_monetary[];
00502     static const locale::id* const  _S_id_messages[];
00503     static const locale::id* const* const _S_facet_categories[];
00504 
00505     inline void
00506     _M_add_reference() throw()
00507     { __gnu_cxx::__atomic_add_dispatch(&_M_refcount, 1); }
00508 
00509     inline void
00510     _M_remove_reference() throw()
00511     {
00512       if (__gnu_cxx::__exchange_and_add_dispatch(&_M_refcount, -1) == 1)
00513     {
00514       try
00515         { delete this; }
00516       catch(...)
00517         { }
00518     }
00519     }
00520 
00521     _Impl(const _Impl&, size_t);
00522     _Impl(const char*, size_t);
00523     _Impl(size_t) throw();
00524 
00525    ~_Impl() throw();
00526 
00527     _Impl(const _Impl&);  // Not defined.
00528 
00529     void
00530     operator=(const _Impl&);  // Not defined.
00531 
00532     inline bool
00533     _M_check_same_name()
00534     {
00535       bool __ret = true;
00536       if (_M_names[1])
00537     // We must actually compare all the _M_names: can be all equal!
00538     for (size_t __i = 0; __ret && __i < _S_categories_size - 1; ++__i)
00539       __ret = std::strcmp(_M_names[__i], _M_names[__i + 1]) == 0;
00540       return __ret;
00541     }
00542 
00543     void
00544     _M_replace_categories(const _Impl*, category);
00545 
00546     void
00547     _M_replace_category(const _Impl*, const locale::id* const*);
00548 
00549     void
00550     _M_replace_facet(const _Impl*, const locale::id*);
00551 
00552     void
00553     _M_install_facet(const locale::id*, const facet*);
00554 
00555     template<typename _Facet>
00556       inline void
00557       _M_init_facet(_Facet* __facet)
00558       { _M_install_facet(&_Facet::id, __facet); }
00559 
00560     void
00561     _M_install_cache(const facet*, size_t);
00562   };
00563 
00564   template<typename _Facet>
00565     locale::locale(const locale& __other, _Facet* __f)
00566     {
00567       _M_impl = new _Impl(*__other._M_impl, 1);
00568 
00569       try
00570     { _M_impl->_M_install_facet(&_Facet::id, __f); }
00571       catch(...)
00572     {
00573       _M_impl->_M_remove_reference();
00574       __throw_exception_again;
00575     }
00576       delete [] _M_impl->_M_names[0];
00577       _M_impl->_M_names[0] = 0;   // Unnamed.
00578     }
00579 
00580 _GLIBCXX_END_NAMESPACE
00581 
00582 #endif

Generated on Thu Nov 1 13:12:03 2007 for libstdc++ by  doxygen 1.5.1