STLdoc
STLdocumentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
locale_classes.h
Go to the documentation of this file.
1 // Locale support -*- C++ -*-
2 
3 // Copyright (C) 1997-2013 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10 
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19 
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24 
30 //
31 // ISO C++ 14882: 22.1 Locales
32 //
33 
34 #ifndef _LOCALE_CLASSES_H
35 #define _LOCALE_CLASSES_H 1
36 
37 #pragma GCC system_header
38 
39 #include <bits/localefwd.h>
40 #include <string>
41 #include <ext/atomicity.h>
42 
43 namespace std _GLIBCXX_VISIBILITY(default)
44 {
45 _GLIBCXX_BEGIN_NAMESPACE_VERSION
46 
47  // 22.1.1 Class locale
62  class locale
63  {
64  public:
65  // Types:
67  typedef int category;
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 
77  template<typename _Facet>
78  friend bool
79  has_facet(const locale&) throw();
80 
81  template<typename _Facet>
82  friend const _Facet&
83  use_facet(const locale&);
84 
85  template<typename _Cache>
86  friend struct __use_cache;
87 
89 
98  static const category none = 0;
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);
108 
109  // Construct/copy/destroy:
110 
117  locale() throw();
118 
126  locale(const locale& __other) throw();
127 
136  explicit
137  locale(const char* __s);
138 
151  locale(const locale& __base, const char* __s, category __cat);
152 
164  locale(const locale& __base, const locale& __add, category __cat);
165 
177  template<typename _Facet>
178  locale(const locale& __other, _Facet* __f);
179 
181  ~locale() throw();
182 
191  const locale&
192  operator=(const locale& __other) throw();
193 
206  template<typename _Facet>
207  locale
208  combine(const locale& __other) const;
209 
210  // Locale operations:
215  string
216  name() const;
217 
225  bool
226  operator==(const locale& __other) const throw();
227 
234  bool
235  operator!=(const locale& __other) const throw()
236  { return !(this->operator==(__other)); }
237 
253  template<typename _Char, typename _Traits, typename _Alloc>
254  bool
255  operator()(const basic_string<_Char, _Traits, _Alloc>& __s1,
256  const basic_string<_Char, _Traits, _Alloc>& __s2) const;
257 
258  // Global locale objects:
269  static locale
270  global(const locale& __loc);
271 
275  static const locale&
276  classic();
277 
278  private:
279  // The (shared) implementation
280  _Impl* _M_impl;
281 
282  // The "C" reference locale
283  static _Impl* _S_classic;
284 
285  // Current global locale
286  static _Impl* _S_global;
287 
288  // Names of underlying locale categories.
289  // NB: locale::global() has to know how to modify all the
290  // underlying categories, not just the ones required by the C++
291  // standard.
292  static const char* const* const _S_categories;
293 
294  // Number of standard categories. For C++, these categories are
295  // collate, ctype, monetary, numeric, time, and messages. These
296  // directly correspond to ISO C99 macros LC_COLLATE, LC_CTYPE,
297  // LC_MONETARY, LC_NUMERIC, and LC_TIME. In addition, POSIX (IEEE
298  // 1003.1-2001) specifies LC_MESSAGES.
299  // In addition to the standard categories, the underlying
300  // operating system is allowed to define extra LC_*
301  // macros. For GNU systems, the following are also valid:
302  // LC_PAPER, LC_NAME, LC_ADDRESS, LC_TELEPHONE, LC_MEASUREMENT,
303  // and LC_IDENTIFICATION.
304  enum { _S_categories_size = 6 + _GLIBCXX_NUM_CATEGORIES };
305 
306 #ifdef __GTHREADS
307  static __gthread_once_t _S_once;
308 #endif
309 
310  explicit
311  locale(_Impl*) throw();
312 
313  static void
314  _S_initialize();
315 
316  static void
317  _S_initialize_once() throw();
318 
319  static category
320  _S_normalize_category(category);
321 
322  void
323  _M_coalesce(const locale& __base, const locale& __add, category __cat);
324  };
325 
326 
327  // 22.1.1.1.2 Class locale::facet
338  class locale::facet
339  {
340  private:
341  friend class locale;
342  friend class locale::_Impl;
343 
344  mutable _Atomic_word _M_refcount;
345 
346  // Contains data from the underlying "C" library for the classic locale.
347  static __c_locale _S_c_locale;
348 
349  // String literal for the name of the classic locale.
350  static const char _S_c_name[2];
351 
352 #ifdef __GTHREADS
353  static __gthread_once_t _S_once;
354 #endif
355 
356  static void
357  _S_initialize_once();
358 
359  protected:
369  explicit
370  facet(size_t __refs = 0) throw() : _M_refcount(__refs ? 1 : 0)
371  { }
372 
374  virtual
375  ~facet();
376 
377  static void
378  _S_create_c_locale(__c_locale& __cloc, const char* __s,
379  __c_locale __old = 0);
380 
381  static __c_locale
382  _S_clone_c_locale(__c_locale& __cloc) throw();
383 
384  static void
385  _S_destroy_c_locale(__c_locale& __cloc);
386 
387  static __c_locale
388  _S_lc_ctype_c_locale(__c_locale __cloc, const char* __s);
389 
390  // Returns data from the underlying "C" library data for the
391  // classic locale.
392  static __c_locale
393  _S_get_c_locale();
394 
395  _GLIBCXX_CONST static const char*
396  _S_get_c_name() throw();
397 
398  private:
399  void
400  _M_add_reference() const throw()
401  { __gnu_cxx::__atomic_add_dispatch(&_M_refcount, 1); }
402 
403  void
404  _M_remove_reference() const throw()
405  {
406  // Be race-detector-friendly. For more info see bits/c++config.
407  _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_refcount);
408  if (__gnu_cxx::__exchange_and_add_dispatch(&_M_refcount, -1) == 1)
409  {
410  _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_refcount);
411  __try
412  { delete this; }
413  __catch(...)
414  { }
415  }
416  }
417 
418  facet(const facet&); // Not defined.
419 
420  facet&
421  operator=(const facet&); // Not defined.
422  };
423 
424 
425  // 22.1.1.1.3 Class locale::id
436  class locale::id
437  {
438  private:
439  friend class locale;
440  friend class locale::_Impl;
441 
442  template<typename _Facet>
443  friend const _Facet&
444  use_facet(const locale&);
445 
446  template<typename _Facet>
447  friend bool
448  has_facet(const locale&) throw();
449 
450  // NB: There is no accessor for _M_index because it may be used
451  // before the constructor is run; the effect of calling a member
452  // function (even an inline) would be undefined.
453  mutable size_t _M_index;
454 
455  // Last id number assigned.
456  static _Atomic_word _S_refcount;
457 
458  void
459  operator=(const id&); // Not defined.
460 
461  id(const id&); // Not defined.
462 
463  public:
464  // NB: This class is always a static data member, and thus can be
465  // counted on to be zero-initialized.
467  id() { }
468 
469  size_t
470  _M_id() const throw();
471  };
472 
473 
474  // Implementation object for locale.
475  class locale::_Impl
476  {
477  public:
478  // Friends.
479  friend class locale;
480  friend class locale::facet;
481 
482  template<typename _Facet>
483  friend bool
484  has_facet(const locale&) throw();
485 
486  template<typename _Facet>
487  friend const _Facet&
488  use_facet(const locale&);
489 
490  template<typename _Cache>
491  friend struct __use_cache;
492 
493  private:
494  // Data Members.
495  _Atomic_word _M_refcount;
496  const facet** _M_facets;
497  size_t _M_facets_size;
498  const facet** _M_caches;
499  char** _M_names;
500  static const locale::id* const _S_id_ctype[];
501  static const locale::id* const _S_id_numeric[];
502  static const locale::id* const _S_id_collate[];
503  static const locale::id* const _S_id_time[];
504  static const locale::id* const _S_id_monetary[];
505  static const locale::id* const _S_id_messages[];
506  static const locale::id* const* const _S_facet_categories[];
507 
508  void
509  _M_add_reference() throw()
510  { __gnu_cxx::__atomic_add_dispatch(&_M_refcount, 1); }
511 
512  void
513  _M_remove_reference() throw()
514  {
515  // Be race-detector-friendly. For more info see bits/c++config.
516  _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_refcount);
517  if (__gnu_cxx::__exchange_and_add_dispatch(&_M_refcount, -1) == 1)
518  {
519  _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_refcount);
520  __try
521  { delete this; }
522  __catch(...)
523  { }
524  }
525  }
526 
527  _Impl(const _Impl&, size_t);
528  _Impl(const char*, size_t);
529  _Impl(size_t) throw();
530 
531  ~_Impl() throw();
532 
533  _Impl(const _Impl&); // Not defined.
534 
535  void
536  operator=(const _Impl&); // Not defined.
537 
538  bool
539  _M_check_same_name()
540  {
541  bool __ret = true;
542  if (_M_names[1])
543  // We must actually compare all the _M_names: can be all equal!
544  for (size_t __i = 0; __ret && __i < _S_categories_size - 1; ++__i)
545  __ret = __builtin_strcmp(_M_names[__i], _M_names[__i + 1]) == 0;
546  return __ret;
547  }
548 
549  void
550  _M_replace_categories(const _Impl*, category);
551 
552  void
553  _M_replace_category(const _Impl*, const locale::id* const*);
554 
555  void
556  _M_replace_facet(const _Impl*, const locale::id*);
557 
558  void
559  _M_install_facet(const locale::id*, const facet*);
560 
561  template<typename _Facet>
562  void
563  _M_init_facet(_Facet* __facet)
564  { _M_install_facet(&_Facet::id, __facet); }
565 
566  void
567  _M_install_cache(const facet*, size_t);
568  };
569 
570 
583  template<typename _CharT>
584  class collate : public locale::facet
585  {
586  public:
587  // Types:
589  typedef _CharT char_type;
591  typedef basic_string<_CharT> string_type;
593 
594  protected:
595  // Underlying "C" library locale information saved from
596  // initialization, needed by collate_byname as well.
597  __c_locale _M_c_locale_collate;
598 
599  public:
601  static locale::id id;
602 
610  explicit
611  collate(size_t __refs = 0)
612  : facet(__refs), _M_c_locale_collate(_S_get_c_locale())
613  { }
614 
624  explicit
625  collate(__c_locale __cloc, size_t __refs = 0)
626  : facet(__refs), _M_c_locale_collate(_S_clone_c_locale(__cloc))
627  { }
628 
641  int
642  compare(const _CharT* __lo1, const _CharT* __hi1,
643  const _CharT* __lo2, const _CharT* __hi2) const
644  { return this->do_compare(__lo1, __hi1, __lo2, __hi2); }
645 
660  string_type
661  transform(const _CharT* __lo, const _CharT* __hi) const
662  { return this->do_transform(__lo, __hi); }
663 
674  long
675  hash(const _CharT* __lo, const _CharT* __hi) const
676  { return this->do_hash(__lo, __hi); }
677 
678  // Used to abstract out _CharT bits in virtual member functions, below.
679  int
680  _M_compare(const _CharT*, const _CharT*) const throw();
681 
682  size_t
683  _M_transform(_CharT*, const _CharT*, size_t) const throw();
684 
685  protected:
687  virtual
688  ~collate()
689  { _S_destroy_c_locale(_M_c_locale_collate); }
690 
703  virtual int
704  do_compare(const _CharT* __lo1, const _CharT* __hi1,
705  const _CharT* __lo2, const _CharT* __hi2) const;
706 
717  virtual string_type
718  do_transform(const _CharT* __lo, const _CharT* __hi) const;
719 
730  virtual long
731  do_hash(const _CharT* __lo, const _CharT* __hi) const;
732  };
733 
734  template<typename _CharT>
736 
737  // Specializations.
738  template<>
739  int
740  collate<char>::_M_compare(const char*, const char*) const throw();
741 
742  template<>
743  size_t
744  collate<char>::_M_transform(char*, const char*, size_t) const throw();
745 
746 #ifdef _GLIBCXX_USE_WCHAR_T
747  template<>
748  int
749  collate<wchar_t>::_M_compare(const wchar_t*, const wchar_t*) const throw();
750 
751  template<>
752  size_t
753  collate<wchar_t>::_M_transform(wchar_t*, const wchar_t*, size_t) const throw();
754 #endif
755 
757  template<typename _CharT>
758  class collate_byname : public collate<_CharT>
759  {
760  public:
762  typedef _CharT char_type;
764  typedef basic_string<_CharT> string_type;
766 
767  explicit
768  collate_byname(const char* __s, size_t __refs = 0)
769  : collate<_CharT>(__refs)
770  {
771  if (__builtin_strcmp(__s, "C") != 0
772  && __builtin_strcmp(__s, "POSIX") != 0)
773  {
774  this->_S_destroy_c_locale(this->_M_c_locale_collate);
775  this->_S_create_c_locale(this->_M_c_locale_collate, __s);
776  }
777  }
778 
779  protected:
780  virtual
781  ~collate_byname() { }
782  };
783 
784 _GLIBCXX_END_NAMESPACE_VERSION
785 } // namespace
786 
787 # include <bits/locale_classes.tcc>
788 
789 #endif
_Siter_base< _Iterator >::iterator_type __base(_Iterator __it)
Definition: functions.h:446
bool operator==(const exception_ptr &, const exception_ptr &) _GLIBCXX_USE_NOEXCEPT __attribute__((__pure__))
#define __try
Definition: exception_defines.h:35
namespace std _GLIBCXX_VISIBILITY(default)
Definition: auto_ptr.h:36
Definition: objc.h:84
#define __catch(X)
Definition: exception_defines.h:36
struct objc_object * id
Definition: basic_string.h:45