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