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