locale_classes.h

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 00035 /** @file localefwd.h 00036 * This is an internal header file, included by other library headers. 00037 * You should not attempt to use it directly. 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 <bits/atomicity.h> 00049 #include <bits/gthr.h> 00050 00051 namespace std 00052 { 00053 // 22.1.1 Class locale 00054 /** 00055 * @brief Container class for localization functionality. 00056 * 00057 * The locale class is first a class wrapper for C library locales. It is 00058 * also an extensible container for user-defined localization. A locale is 00059 * a collection of facets that implement various localization features such 00060 * as money, time, and number printing. 00061 * 00062 * Constructing C++ locales does not change the C library locale. 00063 * 00064 * This library supports efficient construction and copying of locales 00065 * through a reference counting implementation of the locale class. 00066 */ 00067 class locale 00068 { 00069 public: 00070 // Types: 00071 /// Definition of locale::category. 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 00093 //@{ 00094 /** 00095 * @brief Category values. 00096 * 00097 * The standard category values are none, ctype, numeric, collate, time, 00098 * monetary, and messages. They form a bitmask that supports union and 00099 * intersection. The category all is the union of these values. 00100 * 00101 * @if maint 00102 * NB: Order must match _S_facet_categories definition in locale.cc 00103 * @endif 00104 */ 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); 00114 //@} 00115 00116 // Construct/copy/destroy: 00117 00118 /** 00119 * @brief Default constructor. 00120 * 00121 * Constructs a copy of the global locale. If no locale has been 00122 * explicitly set, this is the "C" locale. 00123 */ 00124 locale() throw(); 00125 00126 /** 00127 * @brief Copy constructor. 00128 * 00129 * Constructs a copy of @a other. 00130 * 00131 * @param other The locale to copy. 00132 */ 00133 locale(const locale& __other) throw(); 00134 00135 /** 00136 * @brief Named locale constructor. 00137 * 00138 * Constructs a copy of the named C library locale. 00139 * 00140 * @param s Name of the locale to construct. 00141 * @throw std::runtime_error if s is null or an undefined locale. 00142 */ 00143 explicit 00144 locale(const char* __s); 00145 00146 /** 00147 * @brief Construct locale with facets from another locale. 00148 * 00149 * Constructs a copy of the locale @a base. The facets specified by @a 00150 * cat are replaced with those from the locale named by @a s. If base is 00151 * named, this locale instance will also be named. 00152 * 00153 * @param base The locale to copy. 00154 * @param s Name of the locale to use facets from. 00155 * @param cat Set of categories defining the facets to use from s. 00156 * @throw std::runtime_error if s is null or an undefined locale. 00157 */ 00158 locale(const locale& __base, const char* __s, category __cat); 00159 00160 /** 00161 * @brief Construct locale with facets from another locale. 00162 * 00163 * Constructs a copy of the locale @a base. The facets specified by @a 00164 * cat are replaced with those from the locale @a add. If @a base and @a 00165 * add are named, this locale instance will also be named. 00166 * 00167 * @param base The locale to copy. 00168 * @param add The locale to use facets from. 00169 * @param cat Set of categories defining the facets to use from add. 00170 */ 00171 locale(const locale& __base, const locale& __add, category __cat); 00172 00173 /** 00174 * @brief Construct locale with another facet. 00175 * 00176 * Constructs a copy of the locale @a other. The facet @f is added to 00177 * @other, replacing an existing facet of type Facet if there is one. If 00178 * @f is null, this locale is a copy of @a other. 00179 * 00180 * @param other The locale to copy. 00181 * @param f The facet to add in. 00182 */ 00183 template<typename _Facet> 00184 locale(const locale& __other, _Facet* __f); 00185 00186 /// Locale destructor. 00187 ~locale() throw(); 00188 00189 /** 00190 * @brief Assignment operator. 00191 * 00192 * Set this locale to be a copy of @a other. 00193 * 00194 * @param other The locale to copy. 00195 * @return A reference to this locale. 00196 */ 00197 const locale& 00198 operator=(const locale& __other) throw(); 00199 00200 /** 00201 * @brief Construct locale with another facet. 00202 * 00203 * Constructs and returns a new copy of this locale. Adds or replaces an 00204 * existing facet of type Facet from the locale @a other into the new 00205 * locale. 00206 * 00207 * @param Facet The facet type to copy from other 00208 * @param other The locale to copy from. 00209 * @return Newly constructed locale. 00210 * @throw std::runtime_error if other has no facet of type Facet. 00211 */ 00212 template<typename _Facet> 00213 locale 00214 combine(const locale& __other) const; 00215 00216 // Locale operations: 00217 /** 00218 * @brief Return locale name. 00219 * @return Locale name or "*" if unnamed. 00220 */ 00221 string 00222 name() const; 00223 00224 /** 00225 * @brief Locale equality. 00226 * 00227 * @param other The locale to compare against. 00228 * @return True if other and this refer to the same locale instance, are 00229 * copies, or have the same name. False otherwise. 00230 */ 00231 bool 00232 operator==(const locale& __other) const throw (); 00233 00234 /** 00235 * @brief Locale inequality. 00236 * 00237 * @param other The locale to compare against. 00238 * @return ! (*this == other) 00239 */ 00240 inline bool 00241 operator!=(const locale& __other) const throw () 00242 { return !(this->operator==(__other)); } 00243 00244 /** 00245 * @brief Compare two strings according to collate. 00246 * 00247 * Template operator to compare two strings using the compare function of 00248 * the collate facet in this locale. One use is to provide the locale to 00249 * the sort function. For example, a vector v of strings could be sorted 00250 * according to locale loc by doing: 00251 * @code 00252 * std::sort(v.begin(), v.end(), loc); 00253 * @endcode 00254 * 00255 * @param s1 First string to compare. 00256 * @param s2 Second string to compare. 00257 * @return True if collate<Char> facet compares s1 < s2, else false. 00258 */ 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: 00265 /** 00266 * @brief Set global locale 00267 * 00268 * This function sets the global locale to the argument and returns a 00269 * copy of the previous global locale. If the argument has a name, it 00270 * will also call std::setlocale(LC_ALL, loc.name()). 00271 * 00272 * @param locale The new locale to make global. 00273 * @return Copy of the old global locale. 00274 */ 00275 static locale 00276 global(const locale&); 00277 00278 /** 00279 * @brief Return reference to the "C" locale. 00280 */ 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 00334 /** 00335 * @brief Localization functionality base class. 00336 * 00337 * The facet class is the base class for a localization feature, such as 00338 * money, time, and number printing. It provides common support for facets 00339 * and reference management. 00340 * 00341 * Facets may not be copied or assigned. 00342 */ 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: 00365 /** 00366 * @brief Facet constructor. 00367 * 00368 * This is the constructor provided by the standard. If refs is 0, the 00369 * facet is destroyed when the last referencing locale is destroyed. 00370 * Otherwise the facet will never be destroyed. 00371 * 00372 * @param refs The initial value for reference count. 00373 */ 00374 explicit 00375 facet(size_t __refs = 0) throw() : _M_refcount(__refs ? 1 : 0) 00376 { } 00377 00378 /// Facet destructor. 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 00425 /** 00426 * @brief Facet ID class. 00427 * 00428 * The ID class provides facets with an index used to identify them. 00429 * Every facet class must define a public static member locale::id, or be 00430 * derived from a facet that provides this member, otherwise the facet 00431 * cannot be used in a locale. The locale::id ensures that each class 00432 * type gets a unique identifier. 00433 */ 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. 00464 /// Constructor. 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 Wed Sep 8 10:19:35 2004 for libstdc++-v3 Source by doxygen 1.3.8