1// Locale support -*- C++ -*- 2 3// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 4// 2006, 2007, 2008, 2009, 2010, 2011 5// Free Software Foundation, Inc. 6// 7// This file is part of the GNU ISO C++ Library. This library is free 8// software; you can redistribute it and/or modify it under the 9// terms of the GNU General Public License as published by the 10// Free Software Foundation; either version 3, or (at your option) 11// any later version. 12 13// This library is distributed in the hope that it will be useful, 14// but WITHOUT ANY WARRANTY; without even the implied warranty of 15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16// GNU General Public License for more details. 17 18// Under Section 7 of GPL version 3, you are granted additional 19// permissions described in the GCC Runtime Library Exception, version 20// 3.1, as published by the Free Software Foundation. 21 22// You should have received a copy of the GNU General Public License and 23// a copy of the GCC Runtime Library Exception along with this program; 24// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 25// <http://www.gnu.org/licenses/>. 26 27/** @file bits/locale_classes.h 28 * This is an internal header file, included by other library headers. 29 * Do not attempt to use it directly. @headername{locale} 30 */ 31 32// 33// ISO C++ 14882: 22.1 Locales 34// 35 36#ifndef _LOCALE_CLASSES_H 37#define _LOCALE_CLASSES_H 1 38 39#pragma GCC system_header 40 41#include <bits/localefwd.h> 42#include <string> 43#include <ext/atomicity.h> 44 45namespace std _GLIBCXX_VISIBILITY(default) 46{ 47_GLIBCXX_BEGIN_NAMESPACE_VERSION 48 49 // 22.1.1 Class locale 50 /** 51 * @brief Container class for localization functionality. 52 * @ingroup locales 53 * 54 * The locale class is first a class wrapper for C library locales. It is 55 * also an extensible container for user-defined localization. A locale is 56 * a collection of facets that implement various localization features such 57 * as money, time, and number printing. 58 * 59 * Constructing C++ locales does not change the C library locale. 60 * 61 * This library supports efficient construction and copying of locales 62 * through a reference counting implementation of the locale class. 63 */ 64 class locale 65 { 66 public: 67 // Types: 68 /// Definition of locale::category. 69 typedef int category; 70 71 // Forward decls and friends: 72 class facet; 73 class id; 74 class _Impl; 75 76 friend class facet; 77 friend class _Impl; 78 79 template<typename _Facet> 80 friend bool 81 has_facet(const locale&) throw(); 82 83 template<typename _Facet> 84 friend const _Facet& 85 use_facet(const locale&); 86 87 template<typename _Cache> 88 friend struct __use_cache; 89 90 //@{ 91 /** 92 * @brief Category values. 93 * 94 * The standard category values are none, ctype, numeric, collate, time, 95 * monetary, and messages. They form a bitmask that supports union and 96 * intersection. The category all is the union of these values. 97 * 98 * NB: Order must match _S_facet_categories definition in locale.cc 99 */ 100 static const category none = 0; 101 static const category ctype = 1L << 0; 102 static const category numeric = 1L << 1; 103 static const category collate = 1L << 2; 104 static const category time = 1L << 3; 105 static const category monetary = 1L << 4; 106 static const category messages = 1L << 5; 107 static const category all = (ctype | numeric | collate | 108 time | monetary | messages); 109 //@} 110 111 // Construct/copy/destroy: 112 113 /** 114 * @brief Default constructor. 115 * 116 * Constructs a copy of the global locale. If no locale has been 117 * explicitly set, this is the C locale. 118 */ 119 locale() throw(); 120 121 /** 122 * @brief Copy constructor. 123 * 124 * Constructs a copy of @a other. 125 * 126 * @param other The locale to copy. 127 */ 128 locale(const locale& __other) throw(); 129 130 /** 131 * @brief Named locale constructor. 132 * 133 * Constructs a copy of the named C library locale. 134 * 135 * @param s Name of the locale to construct. 136 * @throw std::runtime_error if s is null or an undefined locale. 137 */ 138 explicit 139 locale(const char* __s); 140 141 /** 142 * @brief Construct locale with facets from another locale. 143 * 144 * Constructs a copy of the locale @a base. The facets specified by @a 145 * cat are replaced with those from the locale named by @a s. If base is 146 * named, this locale instance will also be named. 147 * 148 * @param base The locale to copy. 149 * @param s Name of the locale to use facets from. 150 * @param cat Set of categories defining the facets to use from s. 151 * @throw std::runtime_error if s is null or an undefined locale. 152 */ 153 locale(const locale& __base, const char* __s, category __cat); 154 155 /** 156 * @brief Construct locale with facets from another locale. 157 * 158 * Constructs a copy of the locale @a base. The facets specified by @a 159 * cat are replaced with those from the locale @a add. If @a base and @a 160 * add are named, this locale instance will also be named. 161 * 162 * @param base The locale to copy. 163 * @param add The locale to use facets from. 164 * @param cat Set of categories defining the facets to use from add. 165 */ 166 locale(const locale& __base, const locale& __add, category __cat); 167 168 /** 169 * @brief Construct locale with another facet. 170 * 171 * Constructs a copy of the locale @a other. The facet @f is added to 172 * @other, replacing an existing facet of type Facet if there is one. If 173 * @f is null, this locale is a copy of @a other. 174 * 175 * @param other The locale to copy. 176 * @param f The facet to add in. 177 */ 178 template<typename _Facet> 179 locale(const locale& __other, _Facet* __f); 180 181 /// Locale destructor. 182 ~locale() throw(); 183 184 /** 185 * @brief Assignment operator. 186 * 187 * Set this locale to be a copy of @a other. 188 * 189 * @param other The locale to copy. 190 * @return A reference to this locale. 191 */ 192 const locale& 193 operator=(const locale& __other) throw(); 194 195 /** 196 * @brief Construct locale with another facet. 197 * 198 * Constructs and returns a new copy of this locale. Adds or replaces an 199 * existing facet of type Facet from the locale @a other into the new 200 * locale. 201 * 202 * @param Facet The facet type to copy from other 203 * @param other The locale to copy from. 204 * @return Newly constructed locale. 205 * @throw std::runtime_error if other has no facet of type Facet. 206 */ 207 template<typename _Facet> 208 locale 209 combine(const locale& __other) const; 210 211 // Locale operations: 212 /** 213 * @brief Return locale name. 214 * @return Locale name or "*" if unnamed. 215 */ 216 string 217 name() const; 218 219 /** 220 * @brief Locale equality. 221 * 222 * @param other The locale to compare against. 223 * @return True if other and this refer to the same locale instance, are 224 * copies, or have the same name. False otherwise. 225 */ 226 bool 227 operator==(const locale& __other) const throw(); 228 229 /** 230 * @brief Locale inequality. 231 * 232 * @param other The locale to compare against. 233 * @return ! (*this == other) 234 */ 235 bool 236 operator!=(const locale& __other) const throw() 237 { return !(this->operator==(__other)); } 238 239 /** 240 * @brief Compare two strings according to collate. 241 * 242 * Template operator to compare two strings using the compare function of 243 * the collate facet in this locale. One use is to provide the locale to 244 * the sort function. For example, a vector v of strings could be sorted 245 * according to locale loc by doing: 246 * @code 247 * std::sort(v.begin(), v.end(), loc); 248 * @endcode 249 * 250 * @param s1 First string to compare. 251 * @param s2 Second string to compare. 252 * @return True if collate<Char> facet compares s1 < s2, else false. 253 */ 254 template<typename _Char, typename _Traits, typename _Alloc> 255 bool 256 operator()(const basic_string<_Char, _Traits, _Alloc>& __s1, 257 const basic_string<_Char, _Traits, _Alloc>& __s2) const; 258 259 // Global locale objects: 260 /** 261 * @brief Set global locale 262 * 263 * This function sets the global locale to the argument and returns a 264 * copy of the previous global locale. If the argument has a name, it 265 * will also call std::setlocale(LC_ALL, loc.name()). 266 * 267 * @param locale The new locale to make global. 268 * @return Copy of the old global locale. 269 */ 270 static locale 271 global(const locale&); 272 273 /** 274 * @brief Return reference to the C locale. 275 */ 276 static const locale& 277 classic(); 278 279 private: 280 // The (shared) implementation 281 _Impl* _M_impl; 282 283 // The "C" reference locale 284 static _Impl* _S_classic; 285 286 // Current global locale 287 static _Impl* _S_global; 288 289 // Names of underlying locale categories. 290 // NB: locale::global() has to know how to modify all the 291 // underlying categories, not just the ones required by the C++ 292 // standard. 293 static const char* const* const _S_categories; 294 295 // Number of standard categories. For C++, these categories are 296 // collate, ctype, monetary, numeric, time, and messages. These 297 // directly correspond to ISO C99 macros LC_COLLATE, LC_CTYPE, 298 // LC_MONETARY, LC_NUMERIC, and LC_TIME. In addition, POSIX (IEEE 299 // 1003.1-2001) specifies LC_MESSAGES. 300 // In addition to the standard categories, the underlying 301 // operating system is allowed to define extra LC_* 302 // macros. For GNU systems, the following are also valid: 303 // LC_PAPER, LC_NAME, LC_ADDRESS, LC_TELEPHONE, LC_MEASUREMENT, 304 // and LC_IDENTIFICATION. 305 enum { _S_categories_size = 6 + _GLIBCXX_NUM_CATEGORIES }; 306 307#ifdef __GTHREADS 308 static __gthread_once_t _S_once; 309#endif 310 311 explicit 312 locale(_Impl*) throw(); 313 314 static void 315 _S_initialize(); 316 317 static void 318 _S_initialize_once() throw(); 319 320 static category 321 _S_normalize_category(category); 322 323 void 324 _M_coalesce(const locale& __base, const locale& __add, category __cat); 325 }; 326 327 328 // 22.1.1.1.2 Class locale::facet 329 /** 330 * @brief Localization functionality base class. 331 * @ingroup locales 332 * 333 * The facet class is the base class for a localization feature, such as 334 * money, time, and number printing. It provides common support for facets 335 * and reference management. 336 * 337 * Facets may not be copied or assigned. 338 */ 339 class locale::facet 340 { 341 private: 342 friend class locale; 343 friend class locale::_Impl; 344 345 mutable _Atomic_word _M_refcount; 346 347 // Contains data from the underlying "C" library for the classic locale. 348 static __c_locale _S_c_locale; 349 350 // String literal for the name of the classic locale. 351 static const char _S_c_name[2]; 352 353#ifdef __GTHREADS 354 static __gthread_once_t _S_once; 355#endif 356 357 static void 358 _S_initialize_once(); 359 360 protected: 361 /** 362 * @brief Facet constructor. 363 * 364 * This is the constructor provided by the standard. If refs is 0, the 365 * facet is destroyed when the last referencing locale is destroyed. 366 * Otherwise the facet will never be destroyed. 367 * 368 * @param refs The initial value for reference count. 369 */ 370 explicit 371 facet(size_t __refs = 0) throw() : _M_refcount(__refs ? 1 : 0) 372 { } 373 374 /// Facet destructor. 375 virtual 376 ~facet(); 377 378 static void 379 _S_create_c_locale(__c_locale& __cloc, const char* __s, 380 __c_locale __old = 0); 381 382 static __c_locale 383 _S_clone_c_locale(__c_locale& __cloc) throw(); 384 385 static void 386 _S_destroy_c_locale(__c_locale& __cloc); 387 388 static __c_locale 389 _S_lc_ctype_c_locale(__c_locale __cloc, const char* __s); 390 391 // Returns data from the underlying "C" library data for the 392 // classic locale. 393 static __c_locale 394 _S_get_c_locale(); 395 396 _GLIBCXX_CONST static const char* 397 _S_get_c_name() throw(); 398 399 private: 400 void 401 _M_add_reference() const throw() 402 { __gnu_cxx::__atomic_add_dispatch(&_M_refcount, 1); } 403 404 void 405 _M_remove_reference() const throw() 406 { 407 // Be race-detector-friendly. For more info see bits/c++config. 408 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_refcount); 409 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_refcount, -1) == 1) 410 { 411 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_refcount); 412 __try 413 { delete this; } 414 __catch(...) 415 { } 416 } 417 } 418 419 facet(const facet&); // Not defined. 420 421 facet& 422 operator=(const facet&); // Not defined. 423 }; 424 425 426 // 22.1.1.1.3 Class locale::id 427 /** 428 * @brief Facet ID class. 429 * @ingroup locales 430 * 431 * The ID class provides facets with an index used to identify them. 432 * Every facet class must define a public static member locale::id, or be 433 * derived from a facet that provides this member, otherwise the facet 434 * cannot be used in a locale. The locale::id ensures that each class 435 * type gets a unique identifier. 436 */ 437 class locale::id 438 { 439 private: 440 friend class locale; 441 friend class locale::_Impl; 442 443 template<typename _Facet> 444 friend const _Facet& 445 use_facet(const locale&); 446 447 template<typename _Facet> 448 friend bool 449 has_facet(const locale&) throw(); 450 451 // NB: There is no accessor for _M_index because it may be used 452 // before the constructor is run; the effect of calling a member 453 // function (even an inline) would be undefined. 454 mutable size_t _M_index; 455 456 // Last id number assigned. 457 static _Atomic_word _S_refcount; 458 459 void 460 operator=(const id&); // Not defined. 461 462 id(const id&); // Not defined. 463 464 public: 465 // NB: This class is always a static data member, and thus can be 466 // counted on to be zero-initialized. 467 /// Constructor. 468 id() { } 469 470 size_t 471 _M_id() const throw(); 472 }; 473 474 475 // Implementation object for locale. 476 class locale::_Impl 477 { 478 public: 479 // Friends. 480 friend class locale; 481 friend class locale::facet; 482 483 template<typename _Facet> 484 friend bool 485 has_facet(const locale&) throw(); 486 487 template<typename _Facet> 488 friend const _Facet& 489 use_facet(const locale&); 490 491 template<typename _Cache> 492 friend struct __use_cache; 493 494 private: 495 // Data Members. 496 _Atomic_word _M_refcount; 497 const facet** _M_facets; 498 size_t _M_facets_size; 499 const facet** _M_caches; 500 char** _M_names; 501 static const locale::id* const _S_id_ctype[]; 502 static const locale::id* const _S_id_numeric[]; 503 static const locale::id* const _S_id_collate[]; 504 static const locale::id* const _S_id_time[]; 505 static const locale::id* const _S_id_monetary[]; 506 static const locale::id* const _S_id_messages[]; 507 static const locale::id* const* const _S_facet_categories[]; 508 509 void 510 _M_add_reference() throw() 511 { __gnu_cxx::__atomic_add_dispatch(&_M_refcount, 1); } 512 513 void 514 _M_remove_reference() throw() 515 { 516 // Be race-detector-friendly. For more info see bits/c++config. 517 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_refcount); 518 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_refcount, -1) == 1) 519 { 520 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_refcount); 521 __try 522 { delete this; } 523 __catch(...) 524 { } 525 } 526 } 527 528 _Impl(const _Impl&, size_t); 529 _Impl(const char*, size_t); 530 _Impl(size_t) throw(); 531 532 ~_Impl() throw(); 533 534 _Impl(const _Impl&); // Not defined. 535 536 void 537 operator=(const _Impl&); // Not defined. 538 539 bool 540 _M_check_same_name() 541 { 542 bool __ret = true; 543 if (_M_names[1]) 544 // We must actually compare all the _M_names: can be all equal! 545 for (size_t __i = 0; __ret && __i < _S_categories_size - 1; ++__i) 546 __ret = __builtin_strcmp(_M_names[__i], _M_names[__i + 1]) == 0; 547 return __ret; 548 } 549 550 void 551 _M_replace_categories(const _Impl*, category); 552 553 void 554 _M_replace_category(const _Impl*, const locale::id* const*); 555 556 void 557 _M_replace_facet(const _Impl*, const locale::id*); 558 559 void 560 _M_install_facet(const locale::id*, const facet*); 561 562 template<typename _Facet> 563 void 564 _M_init_facet(_Facet* __facet) 565 { _M_install_facet(&_Facet::id, __facet); } 566 567 void 568 _M_install_cache(const facet*, size_t); 569 }; 570 571 572 /** 573 * @brief Test for the presence of a facet. 574 * 575 * has_facet tests the locale argument for the presence of the facet type 576 * provided as the template parameter. Facets derived from the facet 577 * parameter will also return true. 578 * 579 * @param Facet The facet type to test the presence of. 580 * @param locale The locale to test. 581 * @return true if locale contains a facet of type Facet, else false. 582 */ 583 template<typename _Facet> 584 bool 585 has_facet(const locale& __loc) throw(); 586 587 /** 588 * @brief Return a facet. 589 * 590 * use_facet looks for and returns a reference to a facet of type Facet 591 * where Facet is the template parameter. If has_facet(locale) is true, 592 * there is a suitable facet to return. It throws std::bad_cast if the 593 * locale doesn't contain a facet of type Facet. 594 * 595 * @param Facet The facet type to access. 596 * @param locale The locale to use. 597 * @return Reference to facet of type Facet. 598 * @throw std::bad_cast if locale doesn't contain a facet of type Facet. 599 */ 600 template<typename _Facet> 601 const _Facet& 602 use_facet(const locale& __loc); 603 604 605 /** 606 * @brief Facet for localized string comparison. 607 * 608 * This facet encapsulates the code to compare strings in a localized 609 * manner. 610 * 611 * The collate template uses protected virtual functions to provide 612 * the actual results. The public accessors forward the call to 613 * the virtual functions. These virtual functions are hooks for 614 * developers to implement the behavior they require from the 615 * collate facet. 616 */ 617 template<typename _CharT> 618 class collate : public locale::facet 619 { 620 public: 621 // Types: 622 //@{ 623 /// Public typedefs 624 typedef _CharT char_type; 625 typedef basic_string<_CharT> string_type; 626 //@} 627 628 protected: 629 // Underlying "C" library locale information saved from 630 // initialization, needed by collate_byname as well. 631 __c_locale _M_c_locale_collate; 632 633 public: 634 /// Numpunct facet id. 635 static locale::id id; 636 637 /** 638 * @brief Constructor performs initialization. 639 * 640 * This is the constructor provided by the standard. 641 * 642 * @param refs Passed to the base facet class. 643 */ 644 explicit 645 collate(size_t __refs = 0) 646 : facet(__refs), _M_c_locale_collate(_S_get_c_locale()) 647 { } 648 649 /** 650 * @brief Internal constructor. Not for general use. 651 * 652 * This is a constructor for use by the library itself to set up new 653 * locales. 654 * 655 * @param cloc The C locale. 656 * @param refs Passed to the base facet class. 657 */ 658 explicit 659 collate(__c_locale __cloc, size_t __refs = 0) 660 : facet(__refs), _M_c_locale_collate(_S_clone_c_locale(__cloc)) 661 { } 662 663 /** 664 * @brief Compare two strings. 665 * 666 * This function compares two strings and returns the result by calling 667 * collate::do_compare(). 668 * 669 * @param lo1 Start of string 1. 670 * @param hi1 End of string 1. 671 * @param lo2 Start of string 2. 672 * @param hi2 End of string 2. 673 * @return 1 if string1 > string2, -1 if string1 < string2, else 0. 674 */ 675 int 676 compare(const _CharT* __lo1, const _CharT* __hi1, 677 const _CharT* __lo2, const _CharT* __hi2) const 678 { return this->do_compare(__lo1, __hi1, __lo2, __hi2); } 679 680 /** 681 * @brief Transform string to comparable form. 682 * 683 * This function is a wrapper for strxfrm functionality. It takes the 684 * input string and returns a modified string that can be directly 685 * compared to other transformed strings. In the C locale, this 686 * function just returns a copy of the input string. In some other 687 * locales, it may replace two chars with one, change a char for 688 * another, etc. It does so by returning collate::do_transform(). 689 * 690 * @param lo Start of string. 691 * @param hi End of string. 692 * @return Transformed string_type. 693 */ 694 string_type 695 transform(const _CharT* __lo, const _CharT* __hi) const 696 { return this->do_transform(__lo, __hi); } 697 698 /** 699 * @brief Return hash of a string. 700 * 701 * This function computes and returns a hash on the input string. It 702 * does so by returning collate::do_hash(). 703 * 704 * @param lo Start of string. 705 * @param hi End of string. 706 * @return Hash value. 707 */ 708 long 709 hash(const _CharT* __lo, const _CharT* __hi) const 710 { return this->do_hash(__lo, __hi); } 711 712 // Used to abstract out _CharT bits in virtual member functions, below. 713 int 714 _M_compare(const _CharT*, const _CharT*) const throw(); 715 716 size_t 717 _M_transform(_CharT*, const _CharT*, size_t) const throw(); 718 719 protected: 720 /// Destructor. 721 virtual 722 ~collate() 723 { _S_destroy_c_locale(_M_c_locale_collate); } 724 725 /** 726 * @brief Compare two strings. 727 * 728 * This function is a hook for derived classes to change the value 729 * returned. @see compare(). 730 * 731 * @param lo1 Start of string 1. 732 * @param hi1 End of string 1. 733 * @param lo2 Start of string 2. 734 * @param hi2 End of string 2. 735 * @return 1 if string1 > string2, -1 if string1 < string2, else 0. 736 */ 737 virtual int 738 do_compare(const _CharT* __lo1, const _CharT* __hi1, 739 const _CharT* __lo2, const _CharT* __hi2) const; 740 741 /** 742 * @brief Transform string to comparable form. 743 * 744 * This function is a hook for derived classes to change the value 745 * returned. 746 * 747 * @param lo1 Start of string 1. 748 * @param hi1 End of string 1. 749 * @param lo2 Start of string 2. 750 * @param hi2 End of string 2. 751 * @return 1 if string1 > string2, -1 if string1 < string2, else 0. 752 */ 753 virtual string_type 754 do_transform(const _CharT* __lo, const _CharT* __hi) const; 755 756 /** 757 * @brief Return hash of a string. 758 * 759 * This function computes and returns a hash on the input string. This 760 * function is a hook for derived classes to change the value returned. 761 * 762 * @param lo Start of string. 763 * @param hi End of string. 764 * @return Hash value. 765 */ 766 virtual long 767 do_hash(const _CharT* __lo, const _CharT* __hi) const; 768 }; 769 770 template<typename _CharT> 771 locale::id collate<_CharT>::id; 772 773 // Specializations. 774 template<> 775 int 776 collate<char>::_M_compare(const char*, const char*) const throw(); 777 778 template<> 779 size_t 780 collate<char>::_M_transform(char*, const char*, size_t) const throw(); 781 782#ifdef _GLIBCXX_USE_WCHAR_T 783 template<> 784 int 785 collate<wchar_t>::_M_compare(const wchar_t*, const wchar_t*) const throw(); 786 787 template<> 788 size_t 789 collate<wchar_t>::_M_transform(wchar_t*, const wchar_t*, size_t) const throw(); 790#endif 791 792 /// class collate_byname [22.2.4.2]. 793 template<typename _CharT> 794 class collate_byname : public collate<_CharT> 795 { 796 public: 797 //@{ 798 /// Public typedefs 799 typedef _CharT char_type; 800 typedef basic_string<_CharT> string_type; 801 //@} 802 803 explicit 804 collate_byname(const char* __s, size_t __refs = 0) 805 : collate<_CharT>(__refs) 806 { 807 if (__builtin_strcmp(__s, "C") != 0 808 && __builtin_strcmp(__s, "POSIX") != 0) 809 { 810 this->_S_destroy_c_locale(this->_M_c_locale_collate); 811 this->_S_create_c_locale(this->_M_c_locale_collate, __s); 812 } 813 } 814 815 protected: 816 virtual 817 ~collate_byname() { } 818 }; 819 820_GLIBCXX_END_NAMESPACE_VERSION 821} // namespace 822 823# include <bits/locale_classes.tcc> 824 825#endif 826