1 // Locale support -*- C++ -*-
2
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
10 // any later version.
11
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 // USA.
21
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
30
31 /** @file locale_classes.h
32 * This is an internal header file, included by other library headers.
33 * You should not attempt to use it directly.
34 */
35
36 //
37 // ISO C++ 14882: 22.1 Locales
38 //
39
40 #ifndef _LOCALE_CLASSES_H
41 #define _LOCALE_CLASSES_H 1
42
43 #pragma GCC system_header
44
45 #include <bits/localefwd.h>
46 #include <cstring> // For strcmp.
47 #include <string>
48 #include <ext/atomicity.h>
49
_GLIBCXX_BEGIN_NAMESPACE(std)50 _GLIBCXX_BEGIN_NAMESPACE(std)
51
52 // 22.1.1 Class locale
53 /**
54 * @brief Container class for localization functionality.
55 *
56 * The locale class is first a class wrapper for C library locales. It is
57 * also an extensible container for user-defined localization. A locale is
58 * a collection of facets that implement various localization features such
59 * as money, time, and number printing.
60 *
61 * Constructing C++ locales does not change the C library locale.
62 *
63 * This library supports efficient construction and copying of locales
64 * through a reference counting implementation of the locale class.
65 */
66 class locale
67 {
68 public:
69 // Types:
70 /// Definition of locale::category.
71 typedef int category;
72
73 // Forward decls and friends:
74 class facet;
75 class id;
76 class _Impl;
77
78 friend class facet;
79 friend class _Impl;
80
81 template<typename _Facet>
82 friend bool
83 has_facet(const locale&) throw();
84
85 template<typename _Facet>
86 friend const _Facet&
87 use_facet(const locale&);
88
89 template<typename _Cache>
90 friend struct __use_cache;
91
92 //@{
93 /**
94 * @brief Category values.
95 *
96 * The standard category values are none, ctype, numeric, collate, time,
97 * monetary, and messages. They form a bitmask that supports union and
98 * intersection. The category all is the union of these values.
99 *
100 * @if maint
101 * NB: Order must match _S_facet_categories definition in locale.cc
102 * @endif
103 */
104 static const category none = 0;
105 static const category ctype = 1L << 0;
106 static const category numeric = 1L << 1;
107 static const category collate = 1L << 2;
108 static const category time = 1L << 3;
109 static const category monetary = 1L << 4;
110 static const category messages = 1L << 5;
111 static const category all = (ctype | numeric | collate |
112 time | monetary | messages);
113 //@}
114
115 // Construct/copy/destroy:
116
117 /**
118 * @brief Default constructor.
119 *
120 * Constructs a copy of the global locale. If no locale has been
121 * explicitly set, this is the "C" locale.
122 */
123 locale() throw();
124
125 /**
126 * @brief Copy constructor.
127 *
128 * Constructs a copy of @a other.
129 *
130 * @param other The locale to copy.
131 */
132 locale(const locale& __other) throw();
133
134 /**
135 * @brief Named locale constructor.
136 *
137 * Constructs a copy of the named C library locale.
138 *
139 * @param s Name of the locale to construct.
140 * @throw std::runtime_error if s is null or an undefined locale.
141 */
142 explicit
143 locale(const char* __s);
144
145 /**
146 * @brief Construct locale with facets from another locale.
147 *
148 * Constructs a copy of the locale @a base. The facets specified by @a
149 * cat are replaced with those from the locale named by @a s. If base is
150 * named, this locale instance will also be named.
151 *
152 * @param base The locale to copy.
153 * @param s Name of the locale to use facets from.
154 * @param cat Set of categories defining the facets to use from s.
155 * @throw std::runtime_error if s is null or an undefined locale.
156 */
157 locale(const locale& __base, const char* __s, category __cat);
158
159 /**
160 * @brief Construct locale with facets from another locale.
161 *
162 * Constructs a copy of the locale @a base. The facets specified by @a
163 * cat are replaced with those from the locale @a add. If @a base and @a
164 * add are named, this locale instance will also be named.
165 *
166 * @param base The locale to copy.
167 * @param add The locale to use facets from.
168 * @param cat Set of categories defining the facets to use from add.
169 */
170 locale(const locale& __base, const locale& __add, category __cat);
171
172 /**
173 * @brief Construct locale with another facet.
174 *
175 * Constructs a copy of the locale @a other. The facet @f is added to
176 * @other, replacing an existing facet of type Facet if there is one. If
177 * @f is null, this locale is a copy of @a other.
178 *
179 * @param other The locale to copy.
180 * @param f The facet to add in.
181 */
182 template<typename _Facet>
183 locale(const locale& __other, _Facet* __f);
184
185 /// Locale destructor.
186 ~locale() throw();
187
188 /**
189 * @brief Assignment operator.
190 *
191 * Set this locale to be a copy of @a other.
192 *
193 * @param other The locale to copy.
194 * @return A reference to this locale.
195 */
196 const locale&
197 operator=(const locale& __other) throw();
198
199 /**
200 * @brief Construct locale with another facet.
201 *
202 * Constructs and returns a new copy of this locale. Adds or replaces an
203 * existing facet of type Facet from the locale @a other into the new
204 * locale.
205 *
206 * @param Facet The facet type to copy from other
207 * @param other The locale to copy from.
208 * @return Newly constructed locale.
209 * @throw std::runtime_error if other has no facet of type Facet.
210 */
211 template<typename _Facet>
212 locale
213 combine(const locale& __other) const;
214
215 // Locale operations:
216 /**
217 * @brief Return locale name.
218 * @return Locale name or "*" if unnamed.
219 */
220 string
221 name() const;
222
223 /**
224 * @brief Locale equality.
225 *
226 * @param other The locale to compare against.
227 * @return True if other and this refer to the same locale instance, are
228 * copies, or have the same name. False otherwise.
229 */
230 bool
231 operator==(const locale& __other) const throw ();
232
233 /**
234 * @brief Locale inequality.
235 *
236 * @param other The locale to compare against.
237 * @return ! (*this == other)
238 */
239 inline bool
240 operator!=(const locale& __other) const throw ()
241 { return !(this->operator==(__other)); }
242
243 /**
244 * @brief Compare two strings according to collate.
245 *
246 * Template operator to compare two strings using the compare function of
247 * the collate facet in this locale. One use is to provide the locale to
248 * the sort function. For example, a vector v of strings could be sorted
249 * according to locale loc by doing:
250 * @code
251 * std::sort(v.begin(), v.end(), loc);
252 * @endcode
253 *
254 * @param s1 First string to compare.
255 * @param s2 Second string to compare.
256 * @return True if collate<Char> facet compares s1 < s2, else false.
257 */
258 template<typename _Char, typename _Traits, typename _Alloc>
259 bool
260 operator()(const basic_string<_Char, _Traits, _Alloc>& __s1,
261 const basic_string<_Char, _Traits, _Alloc>& __s2) const;
262
263 // Global locale objects:
264 /**
265 * @brief Set global locale
266 *
267 * This function sets the global locale to the argument and returns a
268 * copy of the previous global locale. If the argument has a name, it
269 * will also call std::setlocale(LC_ALL, loc.name()).
270 *
271 * @param locale The new locale to make global.
272 * @return Copy of the old global locale.
273 */
274 static locale
275 global(const locale&);
276
277 /**
278 * @brief Return reference to the "C" locale.
279 */
280 static const locale&
281 classic();
282
283 private:
284 // The (shared) implementation
285 _Impl* _M_impl;
286
287 // The "C" reference locale
288 static _Impl* _S_classic;
289
290 // Current global locale
291 static _Impl* _S_global;
292
293 // Names of underlying locale categories.
294 // NB: locale::global() has to know how to modify all the
295 // underlying categories, not just the ones required by the C++
296 // standard.
297 static const char* const* const _S_categories;
298
299 // Number of standard categories. For C++, these categories are
300 // collate, ctype, monetary, numeric, time, and messages. These
301 // directly correspond to ISO C99 macros LC_COLLATE, LC_CTYPE,
302 // LC_MONETARY, LC_NUMERIC, and LC_TIME. In addition, POSIX (IEEE
303 // 1003.1-2001) specifies LC_MESSAGES.
304 // In addition to the standard categories, the underlying
305 // operating system is allowed to define extra LC_*
306 // macros. For GNU systems, the following are also valid:
307 // LC_PAPER, LC_NAME, LC_ADDRESS, LC_TELEPHONE, LC_MEASUREMENT,
308 // and LC_IDENTIFICATION.
309 enum { _S_categories_size = 6 + _GLIBCXX_NUM_CATEGORIES };
310
311 #ifdef __GTHREADS
312 static __gthread_once_t _S_once;
313 #endif
314
315 explicit
316 locale(_Impl*) throw();
317
318 static void
319 _S_initialize();
320
321 static void
322 _S_initialize_once();
323
324 static category
325 _S_normalize_category(category);
326
327 void
328 _M_coalesce(const locale& __base, const locale& __add, category __cat);
329 };
330
331
332 // 22.1.1.1.2 Class locale::facet
333 /**
334 * @brief Localization functionality base class.
335 *
336 * The facet class is the base class for a localization feature, such as
337 * money, time, and number printing. It provides common support for facets
338 * and reference management.
339 *
340 * Facets may not be copied or assigned.
341 */
342 class locale::facet
343 {
344 private:
345 friend class locale;
346 friend class locale::_Impl;
347
348 mutable _Atomic_word _M_refcount;
349
350 // Contains data from the underlying "C" library for the classic locale.
351 static __c_locale _S_c_locale;
352
353 // String literal for the name of the classic locale.
354 static const char _S_c_name[2];
355
356 #ifdef __GTHREADS
357 static __gthread_once_t _S_once;
358 #endif
359
360 static void
361 _S_initialize_once();
362
363 protected:
364 /**
365 * @brief Facet constructor.
366 *
367 * This is the constructor provided by the standard. If refs is 0, the
368 * facet is destroyed when the last referencing locale is destroyed.
369 * Otherwise the facet will never be destroyed.
370 *
371 * @param refs The initial value for reference count.
372 */
373 explicit
throw()374 facet(size_t __refs = 0) throw() : _M_refcount(__refs ? 1 : 0)
375 { }
376
377 /// Facet destructor.
378 virtual
379 ~facet();
380
381 static void
382 _S_create_c_locale(__c_locale& __cloc, const char* __s,
383 __c_locale __old = 0);
384
385 static __c_locale
386 _S_clone_c_locale(__c_locale& __cloc);
387
388 static void
389 _S_destroy_c_locale(__c_locale& __cloc);
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 static const char*
397 _S_get_c_name();
398
399 private:
400 inline void
_M_add_reference()401 _M_add_reference() const throw()
402 { __gnu_cxx::__atomic_add_dispatch(&_M_refcount, 1); }
403
404 inline void
_M_remove_reference()405 _M_remove_reference() const throw()
406 {
407 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_refcount, -1) == 1)
408 {
409 try
410 { delete this; }
411 catch (...)
412 { }
413 }
414 }
415
416 facet(const facet&); // Not defined.
417
418 facet&
419 operator=(const facet&); // Not defined.
420 };
421
422
423 // 22.1.1.1.3 Class locale::id
424 /**
425 * @brief Facet ID class.
426 *
427 * The ID class provides facets with an index used to identify them.
428 * Every facet class must define a public static member locale::id, or be
429 * derived from a facet that provides this member, otherwise the facet
430 * cannot be used in a locale. The locale::id ensures that each class
431 * type gets a unique identifier.
432 */
433 class locale::id
434 {
435 private:
436 friend class locale;
437 friend class locale::_Impl;
438
439 template<typename _Facet>
440 friend const _Facet&
441 use_facet(const locale&);
442
443 template<typename _Facet>
444 friend bool
445 has_facet(const locale&) throw ();
446
447 // NB: There is no accessor for _M_index because it may be used
448 // before the constructor is run; the effect of calling a member
449 // function (even an inline) would be undefined.
450 mutable size_t _M_index;
451
452 // Last id number assigned.
453 static _Atomic_word _S_refcount;
454
455 void
456 operator=(const id&); // Not defined.
457
458 id(const id&); // Not defined.
459
460 public:
461 // NB: This class is always a static data member, and thus can be
462 // counted on to be zero-initialized.
463 /// Constructor.
id()464 id() { }
465
466 size_t
467 _M_id() const;
468 };
469
470
471 // Implementation object for locale.
472 class locale::_Impl
473 {
474 public:
475 // Friends.
476 friend class locale;
477 friend class locale::facet;
478
479 template<typename _Facet>
480 friend bool
481 has_facet(const locale&) throw();
482
483 template<typename _Facet>
484 friend const _Facet&
485 use_facet(const locale&);
486
487 template<typename _Cache>
488 friend struct __use_cache;
489
490 private:
491 // Data Members.
492 _Atomic_word _M_refcount;
493 const facet** _M_facets;
494 size_t _M_facets_size;
495 const facet** _M_caches;
496 char** _M_names;
497 static const locale::id* const _S_id_ctype[];
498 static const locale::id* const _S_id_numeric[];
499 static const locale::id* const _S_id_collate[];
500 static const locale::id* const _S_id_time[];
501 static const locale::id* const _S_id_monetary[];
502 static const locale::id* const _S_id_messages[];
503 static const locale::id* const* const _S_facet_categories[];
504
505 inline void
_M_add_reference()506 _M_add_reference() throw()
507 { __gnu_cxx::__atomic_add_dispatch(&_M_refcount, 1); }
508
509 inline void
_M_remove_reference()510 _M_remove_reference() throw()
511 {
512 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_refcount, -1) == 1)
513 {
514 try
515 { delete this; }
516 catch(...)
517 { }
518 }
519 }
520
521 _Impl(const _Impl&, size_t);
522 _Impl(const char*, size_t);
523 _Impl(size_t) throw();
524
525 ~_Impl() throw();
526
527 _Impl(const _Impl&); // Not defined.
528
529 void
530 operator=(const _Impl&); // Not defined.
531
532 inline bool
_M_check_same_name()533 _M_check_same_name()
534 {
535 bool __ret = true;
536 if (_M_names[1])
537 // We must actually compare all the _M_names: can be all equal!
538 for (size_t __i = 0; __ret && __i < _S_categories_size - 1; ++__i)
539 __ret = std::strcmp(_M_names[__i], _M_names[__i + 1]) == 0;
540 return __ret;
541 }
542
543 void
544 _M_replace_categories(const _Impl*, category);
545
546 void
547 _M_replace_category(const _Impl*, const locale::id* const*);
548
549 void
550 _M_replace_facet(const _Impl*, const locale::id*);
551
552 void
553 _M_install_facet(const locale::id*, const facet*);
554
555 template<typename _Facet>
556 inline void
_M_init_facet(_Facet * __facet)557 _M_init_facet(_Facet* __facet)
558 { _M_install_facet(&_Facet::id, __facet); }
559
560 void
561 _M_install_cache(const facet*, size_t);
562 };
563
564 template<typename _Facet>
locale(const locale & __other,_Facet * __f)565 locale::locale(const locale& __other, _Facet* __f)
566 {
567 _M_impl = new _Impl(*__other._M_impl, 1);
568
569 try
570 { _M_impl->_M_install_facet(&_Facet::id, __f); }
571 catch(...)
572 {
573 _M_impl->_M_remove_reference();
574 __throw_exception_again;
575 }
576 delete [] _M_impl->_M_names[0];
577 _M_impl->_M_names[0] = 0; // Unnamed.
578 }
579
580 _GLIBCXX_END_NAMESPACE
581
582 #endif
583