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
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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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 //
00032 // ISO C++ 14882: 22.1  Locales
00033 //
00034 
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 <bits/atomicity.h>
00049 #include <bits/gthr.h>
00050 
00051 namespace std
00052 {
00053   // 22.1.1 Class locale
00067   class locale
00068   {
00069   public:
00070     // Types:
00072     typedef int category;
00073 
00074     // Forward decls and friends:
00075     class facet;
00076     class id;
00077     class _Impl;
00078 
00079     friend class facet;
00080     friend class _Impl;
00081 
00082     template<typename _Facet>
00083       friend bool
00084       has_facet(const locale&) throw();
00085 
00086     template<typename _Facet>
00087       friend const _Facet&
00088       use_facet(const locale&);
00089 
00090     template<typename _Cache>
00091       friend struct __use_cache;
00092 
00094 
00105     static const category none      = 0;
00106     static const category ctype     = 1L << 0;
00107     static const category numeric   = 1L << 1;
00108     static const category collate   = 1L << 2;
00109     static const category time      = 1L << 3;
00110     static const category monetary  = 1L << 4;
00111     static const category messages  = 1L << 5;
00112     static const category all       = (ctype | numeric | collate |
00113                        time  | monetary | messages);
00115 
00116     // Construct/copy/destroy:
00117 
00124     locale() throw();
00125 
00133     locale(const locale& __other) throw();
00134 
00143     explicit
00144     locale(const char* __s);
00145 
00158     locale(const locale& __base, const char* __s, category __cat);
00159 
00171     locale(const locale& __base, const locale& __add, category __cat);
00172 
00183     template<typename _Facet>
00184       locale(const locale& __other, _Facet* __f);
00185 
00187     ~locale() throw();
00188 
00197     const locale&
00198     operator=(const locale& __other) throw();
00199 
00212     template<typename _Facet>
00213       locale
00214       combine(const locale& __other) const;
00215 
00216     // Locale operations:
00221     string
00222     name() const;
00223 
00231     bool
00232     operator==(const locale& __other) const throw ();
00233 
00240     inline bool
00241     operator!=(const locale& __other) const throw ()
00242     { return !(this->operator==(__other));  }
00243 
00259     template<typename _Char, typename _Traits, typename _Alloc>
00260       bool
00261       operator()(const basic_string<_Char, _Traits, _Alloc>& __s1,
00262          const basic_string<_Char, _Traits, _Alloc>& __s2) const;
00263 
00264     // Global locale objects:
00275     static locale
00276     global(const locale&);
00277 
00281     static const locale&
00282     classic();
00283 
00284   private:
00285     // The (shared) implementation
00286     _Impl*      _M_impl;
00287 
00288     // The "C" reference locale
00289     static _Impl*       _S_classic;
00290 
00291     // Current global locale
00292     static _Impl*   _S_global;
00293 
00294     // Names of underlying locale categories.
00295     // NB: locale::global() has to know how to modify all the
00296     // underlying categories, not just the ones required by the C++
00297     // standard.
00298     static const char* const* const _S_categories;
00299 
00300     // Number of standard categories. For C++, these categories are
00301     // collate, ctype, monetary, numeric, time, and messages. These
00302     // directly correspond to ISO C99 macros LC_COLLATE, LC_CTYPE,
00303     // LC_MONETARY, LC_NUMERIC, and LC_TIME. In addition, POSIX (IEEE
00304     // 1003.1-2001) specifies LC_MESSAGES.
00305     // In addition to the standard categories, the underlying
00306     // operating system is allowed to define extra LC_*
00307     // macros. For GNU systems, the following are also valid:
00308     // LC_PAPER, LC_NAME, LC_ADDRESS, LC_TELEPHONE, LC_MEASUREMENT,
00309     // and LC_IDENTIFICATION.
00310     static const size_t _S_categories_size = 6 + _GLIBCXX_NUM_CATEGORIES;
00311 
00312 #ifdef __GTHREADS
00313     static __gthread_once_t _S_once;
00314 #endif
00315 
00316     explicit
00317     locale(_Impl*) throw();
00318 
00319     static void
00320     _S_initialize();
00321 
00322     static void
00323     _S_initialize_once();
00324 
00325     static category
00326     _S_normalize_category(category);
00327 
00328     void
00329     _M_coalesce(const locale& __base, const locale& __add, category __cat);
00330   };
00331 
00332 
00333   // 22.1.1.1.2  Class locale::facet
00343   class locale::facet
00344   {
00345   private:
00346     friend class locale;
00347     friend class locale::_Impl;
00348 
00349     mutable _Atomic_word        _M_refcount;
00350 
00351     // Contains data from the underlying "C" library for the classic locale.
00352     static __c_locale                   _S_c_locale;
00353 
00354     // String literal for the name of the classic locale.
00355     static const char           _S_c_name[2];
00356 
00357 #ifdef __GTHREADS
00358     static __gthread_once_t     _S_once;
00359 #endif
00360 
00361     static void
00362     _S_initialize_once();
00363 
00364   protected:
00374     explicit
00375     facet(size_t __refs = 0) throw() : _M_refcount(__refs ? 1 : 0)
00376     { }
00377 
00379     virtual
00380     ~facet();
00381 
00382     static void
00383     _S_create_c_locale(__c_locale& __cloc, const char* __s,
00384                __c_locale __old = 0);
00385 
00386     static __c_locale
00387     _S_clone_c_locale(__c_locale& __cloc);
00388 
00389     static void
00390     _S_destroy_c_locale(__c_locale& __cloc);
00391 
00392     // Returns data from the underlying "C" library data for the
00393     // classic locale.
00394     static __c_locale
00395     _S_get_c_locale();
00396 
00397     static const char*
00398     _S_get_c_name();
00399 
00400   private:
00401     inline void
00402     _M_add_reference() const throw()
00403     { __gnu_cxx::__atomic_add(&_M_refcount, 1); }
00404 
00405     inline void
00406     _M_remove_reference() const throw()
00407     {
00408       if (__gnu_cxx::__exchange_and_add(&_M_refcount, -1) == 1)
00409     {
00410       try
00411         { delete this; }
00412       catch (...)
00413         { }
00414     }
00415     }
00416 
00417     facet(const facet&);  // Not defined.
00418 
00419     facet&
00420     operator=(const facet&);  // Not defined.
00421   };
00422 
00423 
00424   // 22.1.1.1.3 Class locale::id
00434   class locale::id
00435   {
00436   private:
00437     friend class locale;
00438     friend class locale::_Impl;
00439 
00440     template<typename _Facet>
00441       friend const _Facet&
00442       use_facet(const locale&);
00443 
00444     template<typename _Facet>
00445       friend bool
00446       has_facet(const locale&) throw ();
00447 
00448     // NB: There is no accessor for _M_index because it may be used
00449     // before the constructor is run; the effect of calling a member
00450     // function (even an inline) would be undefined.
00451     mutable size_t      _M_index;
00452 
00453     // Last id number assigned.
00454     static _Atomic_word     _S_refcount;
00455 
00456     void
00457     operator=(const id&);  // Not defined.
00458 
00459     id(const id&);  // Not defined.
00460 
00461   public:
00462     // NB: This class is always a static data member, and thus can be
00463     // counted on to be zero-initialized.
00465     id() { }
00466 
00467     size_t
00468     _M_id() const;
00469   };
00470 
00471 
00472   // Implementation object for locale.
00473   class locale::_Impl
00474   {
00475   public:
00476     // Friends.
00477     friend class locale;
00478     friend class locale::facet;
00479 
00480     template<typename _Facet>
00481       friend bool
00482       has_facet(const locale&) throw();
00483 
00484     template<typename _Facet>
00485       friend const _Facet&
00486       use_facet(const locale&);
00487 
00488     template<typename _Cache>
00489       friend struct __use_cache;
00490 
00491   private:
00492     // Data Members.
00493     _Atomic_word            _M_refcount;
00494     const facet**           _M_facets;
00495     size_t              _M_facets_size;
00496     const facet**           _M_caches;
00497     char**              _M_names;
00498     static const locale::id* const  _S_id_ctype[];
00499     static const locale::id* const  _S_id_numeric[];
00500     static const locale::id* const  _S_id_collate[];
00501     static const locale::id* const  _S_id_time[];
00502     static const locale::id* const  _S_id_monetary[];
00503     static const locale::id* const  _S_id_messages[];
00504     static const locale::id* const* const _S_facet_categories[];
00505 
00506     inline void
00507     _M_add_reference() throw()
00508     { __gnu_cxx::__atomic_add(&_M_refcount, 1); }
00509 
00510     inline void
00511     _M_remove_reference() throw()
00512     {
00513       if (__gnu_cxx::__exchange_and_add(&_M_refcount, -1) == 1)
00514     {
00515       try
00516         { delete this; }
00517       catch(...)
00518         { }
00519     }
00520     }
00521 
00522     _Impl(const _Impl&, size_t);
00523     _Impl(const char*, size_t);
00524     _Impl(size_t) throw();
00525 
00526    ~_Impl() throw();
00527 
00528     _Impl(const _Impl&);  // Not defined.
00529 
00530     void
00531     operator=(const _Impl&);  // Not defined.
00532 
00533     inline bool
00534     _M_check_same_name()
00535     {
00536       bool __ret = true;
00537       for (size_t __i = 0; __ret && __i < _S_categories_size - 1; ++__i)
00538     __ret = std::strcmp(_M_names[__i], _M_names[__i + 1]) == 0;
00539       return __ret;
00540     }
00541 
00542     void
00543     _M_replace_categories(const _Impl*, category);
00544 
00545     void
00546     _M_replace_category(const _Impl*, const locale::id* const*);
00547 
00548     void
00549     _M_replace_facet(const _Impl*, const locale::id*);
00550 
00551     void
00552     _M_install_facet(const locale::id*, const facet*);
00553 
00554     template<typename _Facet>
00555       inline void
00556       _M_init_facet(_Facet* __facet)
00557       { _M_install_facet(&_Facet::id, __facet); }
00558 
00559     void
00560     _M_install_cache(const facet* __cache, size_t __index) throw()
00561     {
00562       __cache->_M_add_reference();
00563       _M_caches[__index] = __cache;
00564     }
00565   };
00566 
00567   template<typename _Facet>
00568     locale::locale(const locale& __other, _Facet* __f)
00569     {
00570       _M_impl = new _Impl(*__other._M_impl, 1);
00571 
00572       char* _M_tmp_names[_S_categories_size];
00573       size_t __i = 0;
00574       try
00575     {
00576       for (; __i < _S_categories_size; ++__i)
00577         {
00578           _M_tmp_names[__i] = new char[2];
00579           std::strcpy(_M_tmp_names[__i], "*");
00580         }
00581       _M_impl->_M_install_facet(&_Facet::id, __f);
00582     }
00583       catch(...)
00584     {
00585       _M_impl->_M_remove_reference();
00586       for (size_t __j = 0; __j < __i; ++__j)
00587         delete [] _M_tmp_names[__j];
00588       __throw_exception_again;
00589     }
00590 
00591       for (size_t __k = 0; __k < _S_categories_size; ++__k)
00592     {
00593       delete [] _M_impl->_M_names[__k];
00594       _M_impl->_M_names[__k] = _M_tmp_names[__k];
00595     }
00596     }
00597 } // namespace std
00598 
00599 #endif

Generated on Tue Jan 30 17:31:51 2007 for GNU C++ STL by doxygen 1.3.6