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