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