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