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