1117397Skan// Locale support -*- C++ -*- 2117397Skan 3169691Skan// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 4117397Skan// Free Software Foundation, Inc. 5117397Skan// 6117397Skan// This file is part of the GNU ISO C++ Library. This library is free 7117397Skan// software; you can redistribute it and/or modify it under the 8117397Skan// terms of the GNU General Public License as published by the 9117397Skan// Free Software Foundation; either version 2, or (at your option) 10117397Skan// any later version. 11117397Skan 12117397Skan// This library is distributed in the hope that it will be useful, 13117397Skan// but WITHOUT ANY WARRANTY; without even the implied warranty of 14117397Skan// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15117397Skan// GNU General Public License for more details. 16117397Skan 17117397Skan// You should have received a copy of the GNU General Public License along 18117397Skan// with this library; see the file COPYING. If not, write to the Free 19169691Skan// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 20117397Skan// USA. 21117397Skan 22117397Skan// As a special exception, you may use this file as part of a free software 23117397Skan// library without restriction. Specifically, if other files instantiate 24117397Skan// templates or use macros or inline functions from this file, or you compile 25117397Skan// this file and link it with other files to produce an executable, this 26117397Skan// file does not by itself cause the resulting executable to be covered by 27117397Skan// the GNU General Public License. This exception does not however 28117397Skan// invalidate any other reasons why the executable file might be covered by 29117397Skan// the GNU General Public License. 30117397Skan 31169691Skan/** @file locale_classes.h 32169691Skan * This is an internal header file, included by other library headers. 33169691Skan * You should not attempt to use it directly. 34169691Skan */ 35169691Skan 36117397Skan// 37117397Skan// ISO C++ 14882: 22.1 Locales 38117397Skan// 39117397Skan 40132720Skan#ifndef _LOCALE_CLASSES_H 41132720Skan#define _LOCALE_CLASSES_H 1 42117397Skan 43117397Skan#pragma GCC system_header 44117397Skan 45117397Skan#include <bits/localefwd.h> 46117397Skan#include <cstring> // For strcmp. 47117397Skan#include <string> 48169691Skan#include <ext/atomicity.h> 49117397Skan 50169691Skan_GLIBCXX_BEGIN_NAMESPACE(std) 51169691Skan 52117397Skan // 22.1.1 Class locale 53132720Skan /** 54132720Skan * @brief Container class for localization functionality. 55132720Skan * 56132720Skan * The locale class is first a class wrapper for C library locales. It is 57132720Skan * also an extensible container for user-defined localization. A locale is 58132720Skan * a collection of facets that implement various localization features such 59132720Skan * as money, time, and number printing. 60132720Skan * 61132720Skan * Constructing C++ locales does not change the C library locale. 62132720Skan * 63132720Skan * This library supports efficient construction and copying of locales 64132720Skan * through a reference counting implementation of the locale class. 65132720Skan */ 66117397Skan class locale 67117397Skan { 68117397Skan public: 69117397Skan // Types: 70132720Skan /// Definition of locale::category. 71132720Skan typedef int category; 72117397Skan 73117397Skan // Forward decls and friends: 74117397Skan class facet; 75117397Skan class id; 76117397Skan class _Impl; 77117397Skan 78117397Skan friend class facet; 79117397Skan friend class _Impl; 80117397Skan 81117397Skan template<typename _Facet> 82132720Skan friend bool 83117397Skan has_facet(const locale&) throw(); 84132720Skan 85117397Skan template<typename _Facet> 86132720Skan friend const _Facet& 87132720Skan use_facet(const locale&); 88117397Skan 89132720Skan template<typename _Cache> 90132720Skan friend struct __use_cache; 91132720Skan 92132720Skan //@{ 93132720Skan /** 94132720Skan * @brief Category values. 95132720Skan * 96132720Skan * The standard category values are none, ctype, numeric, collate, time, 97132720Skan * monetary, and messages. They form a bitmask that supports union and 98132720Skan * intersection. The category all is the union of these values. 99132720Skan * 100132720Skan * @if maint 101132720Skan * NB: Order must match _S_facet_categories definition in locale.cc 102132720Skan * @endif 103132720Skan */ 104117397Skan static const category none = 0; 105132720Skan static const category ctype = 1L << 0; 106132720Skan static const category numeric = 1L << 1; 107132720Skan static const category collate = 1L << 2; 108132720Skan static const category time = 1L << 3; 109132720Skan static const category monetary = 1L << 4; 110132720Skan static const category messages = 1L << 5; 111132720Skan static const category all = (ctype | numeric | collate | 112132720Skan time | monetary | messages); 113132720Skan //@} 114117397Skan 115117397Skan // Construct/copy/destroy: 116132720Skan 117132720Skan /** 118132720Skan * @brief Default constructor. 119132720Skan * 120132720Skan * Constructs a copy of the global locale. If no locale has been 121132720Skan * explicitly set, this is the "C" locale. 122132720Skan */ 123117397Skan locale() throw(); 124117397Skan 125132720Skan /** 126132720Skan * @brief Copy constructor. 127132720Skan * 128132720Skan * Constructs a copy of @a other. 129132720Skan * 130132720Skan * @param other The locale to copy. 131132720Skan */ 132117397Skan locale(const locale& __other) throw(); 133117397Skan 134132720Skan /** 135132720Skan * @brief Named locale constructor. 136132720Skan * 137132720Skan * Constructs a copy of the named C library locale. 138132720Skan * 139132720Skan * @param s Name of the locale to construct. 140132720Skan * @throw std::runtime_error if s is null or an undefined locale. 141132720Skan */ 142132720Skan explicit 143117397Skan locale(const char* __s); 144117397Skan 145132720Skan /** 146132720Skan * @brief Construct locale with facets from another locale. 147132720Skan * 148132720Skan * Constructs a copy of the locale @a base. The facets specified by @a 149132720Skan * cat are replaced with those from the locale named by @a s. If base is 150132720Skan * named, this locale instance will also be named. 151132720Skan * 152132720Skan * @param base The locale to copy. 153132720Skan * @param s Name of the locale to use facets from. 154132720Skan * @param cat Set of categories defining the facets to use from s. 155132720Skan * @throw std::runtime_error if s is null or an undefined locale. 156132720Skan */ 157117397Skan locale(const locale& __base, const char* __s, category __cat); 158117397Skan 159132720Skan /** 160132720Skan * @brief Construct locale with facets from another locale. 161132720Skan * 162132720Skan * Constructs a copy of the locale @a base. The facets specified by @a 163132720Skan * cat are replaced with those from the locale @a add. If @a base and @a 164132720Skan * add are named, this locale instance will also be named. 165132720Skan * 166132720Skan * @param base The locale to copy. 167132720Skan * @param add The locale to use facets from. 168132720Skan * @param cat Set of categories defining the facets to use from add. 169132720Skan */ 170117397Skan locale(const locale& __base, const locale& __add, category __cat); 171117397Skan 172132720Skan /** 173132720Skan * @brief Construct locale with another facet. 174132720Skan * 175132720Skan * Constructs a copy of the locale @a other. The facet @f is added to 176132720Skan * @other, replacing an existing facet of type Facet if there is one. If 177132720Skan * @f is null, this locale is a copy of @a other. 178132720Skan * 179132720Skan * @param other The locale to copy. 180132720Skan * @param f The facet to add in. 181132720Skan */ 182117397Skan template<typename _Facet> 183117397Skan locale(const locale& __other, _Facet* __f); 184117397Skan 185132720Skan /// Locale destructor. 186117397Skan ~locale() throw(); 187117397Skan 188132720Skan /** 189132720Skan * @brief Assignment operator. 190132720Skan * 191132720Skan * Set this locale to be a copy of @a other. 192132720Skan * 193132720Skan * @param other The locale to copy. 194132720Skan * @return A reference to this locale. 195132720Skan */ 196132720Skan const locale& 197117397Skan operator=(const locale& __other) throw(); 198117397Skan 199132720Skan /** 200132720Skan * @brief Construct locale with another facet. 201132720Skan * 202132720Skan * Constructs and returns a new copy of this locale. Adds or replaces an 203132720Skan * existing facet of type Facet from the locale @a other into the new 204132720Skan * locale. 205132720Skan * 206132720Skan * @param Facet The facet type to copy from other 207132720Skan * @param other The locale to copy from. 208132720Skan * @return Newly constructed locale. 209132720Skan * @throw std::runtime_error if other has no facet of type Facet. 210132720Skan */ 211117397Skan template<typename _Facet> 212132720Skan locale 213117397Skan combine(const locale& __other) const; 214117397Skan 215117397Skan // Locale operations: 216132720Skan /** 217132720Skan * @brief Return locale name. 218132720Skan * @return Locale name or "*" if unnamed. 219132720Skan */ 220132720Skan string 221117397Skan name() const; 222117397Skan 223132720Skan /** 224132720Skan * @brief Locale equality. 225132720Skan * 226132720Skan * @param other The locale to compare against. 227132720Skan * @return True if other and this refer to the same locale instance, are 228132720Skan * copies, or have the same name. False otherwise. 229132720Skan */ 230132720Skan bool 231117397Skan operator==(const locale& __other) const throw (); 232117397Skan 233132720Skan /** 234132720Skan * @brief Locale inequality. 235132720Skan * 236132720Skan * @param other The locale to compare against. 237132720Skan * @return ! (*this == other) 238132720Skan */ 239132720Skan inline bool 240117397Skan operator!=(const locale& __other) const throw () 241117397Skan { return !(this->operator==(__other)); } 242117397Skan 243132720Skan /** 244132720Skan * @brief Compare two strings according to collate. 245132720Skan * 246132720Skan * Template operator to compare two strings using the compare function of 247132720Skan * the collate facet in this locale. One use is to provide the locale to 248132720Skan * the sort function. For example, a vector v of strings could be sorted 249132720Skan * according to locale loc by doing: 250132720Skan * @code 251132720Skan * std::sort(v.begin(), v.end(), loc); 252132720Skan * @endcode 253132720Skan * 254132720Skan * @param s1 First string to compare. 255132720Skan * @param s2 Second string to compare. 256132720Skan * @return True if collate<Char> facet compares s1 < s2, else false. 257132720Skan */ 258117397Skan template<typename _Char, typename _Traits, typename _Alloc> 259132720Skan bool 260117397Skan operator()(const basic_string<_Char, _Traits, _Alloc>& __s1, 261117397Skan const basic_string<_Char, _Traits, _Alloc>& __s2) const; 262117397Skan 263117397Skan // Global locale objects: 264132720Skan /** 265132720Skan * @brief Set global locale 266132720Skan * 267132720Skan * This function sets the global locale to the argument and returns a 268132720Skan * copy of the previous global locale. If the argument has a name, it 269132720Skan * will also call std::setlocale(LC_ALL, loc.name()). 270132720Skan * 271132720Skan * @param locale The new locale to make global. 272132720Skan * @return Copy of the old global locale. 273132720Skan */ 274132720Skan static locale 275117397Skan global(const locale&); 276117397Skan 277132720Skan /** 278132720Skan * @brief Return reference to the "C" locale. 279132720Skan */ 280132720Skan static const locale& 281117397Skan classic(); 282117397Skan 283117397Skan private: 284117397Skan // The (shared) implementation 285132720Skan _Impl* _M_impl; 286117397Skan 287117397Skan // The "C" reference locale 288132720Skan static _Impl* _S_classic; 289117397Skan 290117397Skan // Current global locale 291132720Skan static _Impl* _S_global; 292117397Skan 293132720Skan // Names of underlying locale categories. 294132720Skan // NB: locale::global() has to know how to modify all the 295132720Skan // underlying categories, not just the ones required by the C++ 296132720Skan // standard. 297132720Skan static const char* const* const _S_categories; 298132720Skan 299117397Skan // Number of standard categories. For C++, these categories are 300117397Skan // collate, ctype, monetary, numeric, time, and messages. These 301117397Skan // directly correspond to ISO C99 macros LC_COLLATE, LC_CTYPE, 302117397Skan // LC_MONETARY, LC_NUMERIC, and LC_TIME. In addition, POSIX (IEEE 303117397Skan // 1003.1-2001) specifies LC_MESSAGES. 304117397Skan // In addition to the standard categories, the underlying 305117397Skan // operating system is allowed to define extra LC_* 306117397Skan // macros. For GNU systems, the following are also valid: 307117397Skan // LC_PAPER, LC_NAME, LC_ADDRESS, LC_TELEPHONE, LC_MEASUREMENT, 308117397Skan // and LC_IDENTIFICATION. 309169691Skan enum { _S_categories_size = 6 + _GLIBCXX_NUM_CATEGORIES }; 310117397Skan 311132720Skan#ifdef __GTHREADS 312132720Skan static __gthread_once_t _S_once; 313132720Skan#endif 314117397Skan 315132720Skan explicit 316117397Skan locale(_Impl*) throw(); 317117397Skan 318132720Skan static void 319132720Skan _S_initialize(); 320117397Skan 321132720Skan static void 322132720Skan _S_initialize_once(); 323132720Skan 324132720Skan static category 325117397Skan _S_normalize_category(category); 326117397Skan 327117397Skan void 328117397Skan _M_coalesce(const locale& __base, const locale& __add, category __cat); 329117397Skan }; 330117397Skan 331117397Skan 332132720Skan // 22.1.1.1.2 Class locale::facet 333132720Skan /** 334132720Skan * @brief Localization functionality base class. 335132720Skan * 336132720Skan * The facet class is the base class for a localization feature, such as 337132720Skan * money, time, and number printing. It provides common support for facets 338132720Skan * and reference management. 339132720Skan * 340132720Skan * Facets may not be copied or assigned. 341132720Skan */ 342132720Skan class locale::facet 343132720Skan { 344132720Skan private: 345132720Skan friend class locale; 346132720Skan friend class locale::_Impl; 347132720Skan 348132720Skan mutable _Atomic_word _M_refcount; 349132720Skan 350132720Skan // Contains data from the underlying "C" library for the classic locale. 351132720Skan static __c_locale _S_c_locale; 352132720Skan 353132720Skan // String literal for the name of the classic locale. 354132720Skan static const char _S_c_name[2]; 355132720Skan 356132720Skan#ifdef __GTHREADS 357132720Skan static __gthread_once_t _S_once; 358132720Skan#endif 359132720Skan 360132720Skan static void 361132720Skan _S_initialize_once(); 362132720Skan 363132720Skan protected: 364132720Skan /** 365132720Skan * @brief Facet constructor. 366132720Skan * 367132720Skan * This is the constructor provided by the standard. If refs is 0, the 368132720Skan * facet is destroyed when the last referencing locale is destroyed. 369132720Skan * Otherwise the facet will never be destroyed. 370132720Skan * 371132720Skan * @param refs The initial value for reference count. 372132720Skan */ 373132720Skan explicit 374132720Skan facet(size_t __refs = 0) throw() : _M_refcount(__refs ? 1 : 0) 375132720Skan { } 376132720Skan 377132720Skan /// Facet destructor. 378132720Skan virtual 379132720Skan ~facet(); 380132720Skan 381132720Skan static void 382132720Skan _S_create_c_locale(__c_locale& __cloc, const char* __s, 383132720Skan __c_locale __old = 0); 384132720Skan 385132720Skan static __c_locale 386132720Skan _S_clone_c_locale(__c_locale& __cloc); 387132720Skan 388132720Skan static void 389132720Skan _S_destroy_c_locale(__c_locale& __cloc); 390132720Skan 391132720Skan // Returns data from the underlying "C" library data for the 392132720Skan // classic locale. 393132720Skan static __c_locale 394132720Skan _S_get_c_locale(); 395132720Skan 396132720Skan static const char* 397132720Skan _S_get_c_name(); 398132720Skan 399132720Skan private: 400132720Skan inline void 401132720Skan _M_add_reference() const throw() 402169691Skan { __gnu_cxx::__atomic_add_dispatch(&_M_refcount, 1); } 403132720Skan 404132720Skan inline void 405132720Skan _M_remove_reference() const throw() 406132720Skan { 407169691Skan if (__gnu_cxx::__exchange_and_add_dispatch(&_M_refcount, -1) == 1) 408132720Skan { 409132720Skan try 410132720Skan { delete this; } 411132720Skan catch (...) 412132720Skan { } 413132720Skan } 414132720Skan } 415132720Skan 416132720Skan facet(const facet&); // Not defined. 417132720Skan 418132720Skan facet& 419132720Skan operator=(const facet&); // Not defined. 420132720Skan }; 421132720Skan 422132720Skan 423132720Skan // 22.1.1.1.3 Class locale::id 424132720Skan /** 425132720Skan * @brief Facet ID class. 426132720Skan * 427132720Skan * The ID class provides facets with an index used to identify them. 428132720Skan * Every facet class must define a public static member locale::id, or be 429132720Skan * derived from a facet that provides this member, otherwise the facet 430132720Skan * cannot be used in a locale. The locale::id ensures that each class 431132720Skan * type gets a unique identifier. 432132720Skan */ 433132720Skan class locale::id 434132720Skan { 435132720Skan private: 436132720Skan friend class locale; 437132720Skan friend class locale::_Impl; 438132720Skan 439132720Skan template<typename _Facet> 440132720Skan friend const _Facet& 441132720Skan use_facet(const locale&); 442132720Skan 443132720Skan template<typename _Facet> 444132720Skan friend bool 445132720Skan has_facet(const locale&) throw (); 446132720Skan 447132720Skan // NB: There is no accessor for _M_index because it may be used 448132720Skan // before the constructor is run; the effect of calling a member 449132720Skan // function (even an inline) would be undefined. 450132720Skan mutable size_t _M_index; 451132720Skan 452132720Skan // Last id number assigned. 453132720Skan static _Atomic_word _S_refcount; 454132720Skan 455132720Skan void 456132720Skan operator=(const id&); // Not defined. 457132720Skan 458132720Skan id(const id&); // Not defined. 459132720Skan 460132720Skan public: 461132720Skan // NB: This class is always a static data member, and thus can be 462132720Skan // counted on to be zero-initialized. 463132720Skan /// Constructor. 464132720Skan id() { } 465132720Skan 466132720Skan size_t 467132720Skan _M_id() const; 468132720Skan }; 469132720Skan 470132720Skan 471132720Skan // Implementation object for locale. 472117397Skan class locale::_Impl 473117397Skan { 474117397Skan public: 475117397Skan // Friends. 476117397Skan friend class locale; 477117397Skan friend class locale::facet; 478117397Skan 479117397Skan template<typename _Facet> 480132720Skan friend bool 481117397Skan has_facet(const locale&) throw(); 482117397Skan 483117397Skan template<typename _Facet> 484132720Skan friend const _Facet& 485132720Skan use_facet(const locale&); 486117397Skan 487132720Skan template<typename _Cache> 488132720Skan friend struct __use_cache; 489132720Skan 490117397Skan private: 491117397Skan // Data Members. 492132720Skan _Atomic_word _M_refcount; 493132720Skan const facet** _M_facets; 494132720Skan size_t _M_facets_size; 495132720Skan const facet** _M_caches; 496132720Skan char** _M_names; 497132720Skan static const locale::id* const _S_id_ctype[]; 498132720Skan static const locale::id* const _S_id_numeric[]; 499132720Skan static const locale::id* const _S_id_collate[]; 500132720Skan static const locale::id* const _S_id_time[]; 501132720Skan static const locale::id* const _S_id_monetary[]; 502132720Skan static const locale::id* const _S_id_messages[]; 503117397Skan static const locale::id* const* const _S_facet_categories[]; 504117397Skan 505132720Skan inline void 506117397Skan _M_add_reference() throw() 507169691Skan { __gnu_cxx::__atomic_add_dispatch(&_M_refcount, 1); } 508117397Skan 509132720Skan inline void 510117397Skan _M_remove_reference() throw() 511117397Skan { 512169691Skan if (__gnu_cxx::__exchange_and_add_dispatch(&_M_refcount, -1) == 1) 513117397Skan { 514132720Skan try 515132720Skan { delete this; } 516132720Skan catch(...) 517117397Skan { } 518117397Skan } 519117397Skan } 520117397Skan 521117397Skan _Impl(const _Impl&, size_t); 522117397Skan _Impl(const char*, size_t); 523132720Skan _Impl(size_t) throw(); 524117397Skan 525117397Skan ~_Impl() throw(); 526117397Skan 527117397Skan _Impl(const _Impl&); // Not defined. 528117397Skan 529132720Skan void 530117397Skan operator=(const _Impl&); // Not defined. 531117397Skan 532117397Skan inline bool 533117397Skan _M_check_same_name() 534117397Skan { 535117397Skan bool __ret = true; 536169691Skan if (_M_names[1]) 537169691Skan // We must actually compare all the _M_names: can be all equal! 538169691Skan for (size_t __i = 0; __ret && __i < _S_categories_size - 1; ++__i) 539169691Skan __ret = std::strcmp(_M_names[__i], _M_names[__i + 1]) == 0; 540117397Skan return __ret; 541117397Skan } 542117397Skan 543132720Skan void 544117397Skan _M_replace_categories(const _Impl*, category); 545117397Skan 546132720Skan void 547117397Skan _M_replace_category(const _Impl*, const locale::id* const*); 548117397Skan 549132720Skan void 550117397Skan _M_replace_facet(const _Impl*, const locale::id*); 551117397Skan 552132720Skan void 553132720Skan _M_install_facet(const locale::id*, const facet*); 554117397Skan 555117397Skan template<typename _Facet> 556132720Skan inline void 557117397Skan _M_init_facet(_Facet* __facet) 558132720Skan { _M_install_facet(&_Facet::id, __facet); } 559117397Skan 560117397Skan void 561169691Skan _M_install_cache(const facet*, size_t); 562117397Skan }; 563117397Skan 564117397Skan template<typename _Facet> 565117397Skan locale::locale(const locale& __other, _Facet* __f) 566117397Skan { 567117397Skan _M_impl = new _Impl(*__other._M_impl, 1); 568132720Skan 569132720Skan try 570169691Skan { _M_impl->_M_install_facet(&_Facet::id, __f); } 571132720Skan catch(...) 572132720Skan { 573132720Skan _M_impl->_M_remove_reference(); 574132720Skan __throw_exception_again; 575132720Skan } 576169691Skan delete [] _M_impl->_M_names[0]; 577169691Skan _M_impl->_M_names[0] = 0; // Unnamed. 578117397Skan } 579117397Skan 580169691Skan_GLIBCXX_END_NAMESPACE 581169691Skan 582117397Skan#endif 583