OSDN Git Service

2001-09-19 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / bits / localefwd.h
1 // Locale support -*- C++ -*-
2
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001 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 2, 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 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING.  If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
20
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction.  Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License.  This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29
30 //
31 // ISO C++ 14882: 22.1  Locales
32 //
33
34 #ifndef _CPP_BITS_LOCCORE_H
35 #define _CPP_BITS_LOCCORE_H     1
36
37 #pragma GCC system_header
38
39 #include <bits/c++config.h>
40 #include <bits/c++locale.h>     // Defines __c_locale, config-specific includes
41 #include <bits/std_climits.h>   // For CHAR_BIT
42 #include <bits/std_string.h>    // For string
43 #include <bits/std_cctype.h>    // For isspace, etc.
44 #include <bits/functexcept.h>
45
46 namespace std
47 {
48   // NB: Don't instantiate required wchar_t facets if no wchar_t support.
49 #ifdef _GLIBCPP_USE_WCHAR_T
50 # define  _GLIBCPP_NUM_FACETS 28
51 #else
52 # define  _GLIBCPP_NUM_FACETS 14
53 #endif
54
55   // 22.1.1 Locale
56   template<typename _Tp, typename _Alloc> 
57     class vector;
58   class locale;
59
60   // 22.1.3 Convenience interfaces
61   template<typename _CharT> 
62     inline bool 
63     isspace(_CharT, const locale&);
64
65   template<typename _CharT> 
66     inline bool 
67     isprint(_CharT, const locale&);
68
69   template<typename _CharT> 
70     inline bool 
71     iscntrl(_CharT, const locale&);
72
73   template<typename _CharT> 
74     inline bool 
75     isupper(_CharT, const locale&);
76
77   template<typename _CharT> 
78     inline bool 
79     islower(_CharT, const locale&);
80
81   template<typename _CharT> 
82     inline bool 
83     isalpha(_CharT, const locale&);
84
85   template<typename _CharT> 
86     inline bool 
87     isdigit(_CharT, const locale&);
88
89   template<typename _CharT> 
90     inline bool 
91     ispunct(_CharT, const locale&);
92
93   template<typename _CharT> 
94     inline bool 
95     isxdigit(_CharT, const locale&);
96
97   template<typename _CharT> 
98     inline bool 
99     isalnum(_CharT, const locale&);
100
101   template<typename _CharT> 
102     inline bool 
103     isgraph(_CharT, const locale&);
104
105   template<typename _CharT> 
106     inline _CharT 
107     toupper(_CharT, const locale&);
108
109   template<typename _CharT> 
110     inline _CharT 
111     tolower(_CharT, const locale&);
112
113
114   // 22.2.1 and 22.2.1.3 ctype
115   class ctype_base;
116   template<typename _CharT> 
117     class ctype;
118   template<> class ctype<char>;
119 #ifdef _GLIBCPP_USE_WCHAR_T
120   template<> class ctype<wchar_t>;
121 #endif
122   template<typename _CharT> 
123     class ctype_byname;
124   // NB: Specialized for char and wchar_t in locale_facets.h.
125
126   class codecvt_base;
127   class __enc_traits;
128   template<typename _InternT, typename _ExternT, typename _StateT>
129     class codecvt;
130   template<> class codecvt<char, char, mbstate_t>;
131 #ifdef _GLIBCPP_USE_WCHAR_T
132   template<> class codecvt<wchar_t, char, mbstate_t>;
133 #endif
134   template<typename _InternT, typename _ExternT, typename _StateT>
135     class codecvt_byname;
136
137   // 22.2.2 and 22.2.3 numeric
138   template<typename _CharT, typename _InIter = istreambuf_iterator<_CharT> >
139     class num_get;
140   template<typename _CharT, typename _OutIter = ostreambuf_iterator<_CharT> >
141     class num_put;
142   template<typename _CharT> class numpunct;
143   template<typename _CharT> class numpunct_byname;
144
145   // 22.2.4 collation
146   template<typename _CharT> 
147     class collate;
148   template<typename _CharT> class 
149     collate_byname;
150
151   // 22.2.5 date and time
152   class time_base;
153   template<typename _CharT, typename _InIter =  istreambuf_iterator<_CharT> >
154     class time_get;
155   template<typename _CharT, typename _InIter =  istreambuf_iterator<_CharT> >
156     class time_get_byname;
157   template<typename _CharT, typename _OutIter = ostreambuf_iterator<_CharT> >
158     class time_put;
159   template<typename _CharT, typename _OutIter = ostreambuf_iterator<_CharT> >
160     class time_put_byname;
161
162   // 22.2.6 money
163   class money_base;
164   template<typename _CharT, typename _InIter =  istreambuf_iterator<_CharT> >
165     class money_get;
166   template<typename _CharT, typename _OutIter = ostreambuf_iterator<_CharT> >
167     class money_put;
168   template<typename _CharT, bool _Intl = false> 
169     class moneypunct;
170   template<typename _CharT, bool _Intl = false> 
171     class moneypunct_byname;
172
173   // 22.2.7 message retrieval
174   class messages_base;
175   template<typename _CharT> 
176     class messages;
177   template<typename _CharT> 
178     class messages_byname;
179
180   // 22.1.1 Class locale
181   class locale
182   {
183   public:
184     // Types:
185     typedef unsigned int        category;
186
187     // Forward decls and friends:
188     class facet;
189     class id;
190     class _Impl;
191
192     friend class facet;
193     friend class _Impl;
194
195     template<typename _Facet>
196       friend const _Facet& 
197       use_facet(const locale&);
198     
199     template<typename _Facet>
200       friend bool 
201       has_facet(const locale&) throw();
202  
203     // Category values:
204     // NB: Order must match _S_facet_categories definition in locale.cc
205     static const category none          = 0;
206     static const category ctype         = 1L << 0;
207     static const category numeric       = 1L << 1;
208     static const category collate       = 1L << 2;
209     static const category time          = 1L << 3;
210     static const category monetary      = 1L << 4;
211     static const category messages      = 1L << 5;
212     static const category all           = (collate | ctype | monetary |
213                                            numeric | time  | messages);
214
215     // Construct/copy/destroy:
216     locale() throw();
217
218     locale(const locale& __other) throw();
219
220     explicit  
221     locale(const char* __std_name);
222
223     locale(const locale& __base, const char* __s, category __cat);
224
225     locale(const locale& __base, const locale& __add, category __cat);
226
227     template<typename _Facet>
228       locale(const locale& __other, _Facet* __f);
229
230     ~locale() throw();
231
232     const locale&  
233     operator=(const locale& __other) throw();
234
235     template<typename _Facet>
236       locale  
237       combine(const locale& __other);
238
239     // Locale operations:
240     string 
241     name() const;
242
243     bool 
244     operator==(const locale& __other) const throw ();
245
246     inline bool  
247     operator!=(const locale& __other) const throw ()
248     { return !(this->operator==(__other));  }
249
250     template<typename _Char, typename _Traits, typename _Alloc>
251       bool  
252       operator()(const basic_string<_Char, _Traits, _Alloc>& __s1,
253                  const basic_string<_Char, _Traits, _Alloc>& __s2) const;
254
255     // Global locale objects:
256     static locale 
257     global(const locale&);
258
259     static const locale& 
260     classic();
261
262   private:
263     // The (shared) implementation
264     _Impl*              _M_impl;  
265
266     // The "C" reference locale
267     static _Impl*       _S_classic; 
268
269     // Current global reference locale
270     static _Impl*       _S_global;  
271
272     static const size_t _S_num_categories = 6;
273     static const size_t _S_num_facets = _GLIBCPP_NUM_FACETS;
274
275     explicit 
276     locale(_Impl*) throw();
277
278     static inline void  
279     _S_initialize()
280     { if (!_S_classic) classic();  }
281
282     static category  
283     _S_normalize_category(category);
284
285     void
286     _M_coalesce(const locale& __base, const locale& __add, category __cat);
287   };
288
289
290   // locale implementation object
291   class locale::_Impl
292   {
293   public:
294     // Types.
295     typedef vector<facet*, allocator<facet*> >  __vec_facet;
296
297     // Friends.
298     friend class locale;
299     friend class locale::facet;
300
301     template<typename _Facet>
302       friend const _Facet&  
303       use_facet(const locale&);
304
305     template<typename _Facet>
306       friend bool  
307       has_facet(const locale&) throw();
308
309   private:
310     // Data Members.
311     size_t                              _M_references;
312     __vec_facet*                        _M_facets;
313     string                              _M_names[_S_num_categories];
314     static const locale::id* const      _S_id_ctype[];
315     static const locale::id* const      _S_id_numeric[];
316     static const locale::id* const      _S_id_collate[];
317     static const locale::id* const      _S_id_time[];
318     static const locale::id* const      _S_id_monetary[];
319     static const locale::id* const      _S_id_messages[];
320     static const locale::id* const* const _S_facet_categories[];
321
322     inline void 
323     _M_add_reference() throw()
324     { ++_M_references; }  // XXX MT
325
326     inline void 
327     _M_remove_reference() throw()
328     {
329       if (--_M_references == 0)  // XXX MT
330         {
331           try 
332             { delete this; } 
333           catch(...) 
334             { }
335         }
336     }
337
338     _Impl(const _Impl&, size_t);
339     _Impl(string __name, size_t);
340    ~_Impl() throw();
341
342     bool
343     _M_check_same_name()
344     {
345       bool __ret = true;
346       for (size_t i = 0; i < _S_num_categories - 1; ++i)
347         __ret &= _M_names[i] == _M_names[i + 1];
348       return __ret;
349     }
350     void 
351     _M_replace_categories(const _Impl*, category);
352
353     void 
354     _M_replace_category(const _Impl*, const locale::id* const*);
355
356     void 
357     _M_replace_facet(const _Impl*, const locale::id*);
358
359     void 
360     _M_install_facet(const locale::id*, facet*);
361
362     template<typename _Facet>
363       inline void 
364       _M_init_facet(_Facet* __facet)
365       { _M_install_facet(&_Facet::id, __facet);  }
366   };
367
368   template<typename _Facet>
369     locale::locale(const locale& __other, _Facet* __f)
370     {
371       _M_impl = new _Impl(*__other._M_impl, 1);
372       _M_impl->_M_install_facet(&_Facet::id, __f);
373       for (size_t __i = 0; __i < _S_num_categories; ++__i)
374         _M_impl->_M_names[__i] = "*";
375     }
376
377   // 22.1.1.1.2  Class locale::facet
378   class locale::facet
379   {
380     friend class locale;
381     friend class locale::_Impl;
382     friend class __enc_traits;
383
384   protected:
385     explicit 
386     facet(size_t __refs = 0) throw();
387
388     virtual 
389     ~facet() { };
390
391     static void
392     _S_create_c_locale(__c_locale& __cloc, const char* __s);
393
394     static __c_locale
395     _S_clone_c_locale(__c_locale& __cloc);
396
397     static void
398     _S_destroy_c_locale(__c_locale& __cloc);
399
400   private:
401     size_t _M_references;
402
403     void 
404     _M_add_reference() throw();
405
406     void 
407     _M_remove_reference() throw();
408
409     facet(const facet&);  // not defined
410
411     void 
412     operator=(const facet&);  // not defined
413   };
414
415
416   // 22.1.1.1.3 Class locale::id
417   class locale::id
418   {
419   private:
420     friend class locale;
421     friend class locale::_Impl;
422     template<typename _Facet>
423       friend const _Facet&  
424       use_facet(const locale&);
425     template<typename _Facet>
426       friend bool           
427       has_facet(const locale&) throw ();
428
429     // NB: There is no accessor for _M_index because it may be used
430     // before the constructor is run; the effect of calling a member
431     // function (even an inline) would be undefined.
432     mutable size_t      _M_index;
433
434     // Last id number assigned
435     static size_t       _S_highwater;   
436
437     void 
438     operator=(const id&);  // not defined
439
440     id(const id&);  // not defined
441
442   public:
443     // NB: This class is always a static data member, and thus can be
444     // counted on to be zero-initialized.
445     // XXX id() : _M_index(0) { }
446     id() { }
447   };
448
449   template<typename _Facet>
450     const _Facet&
451     use_facet(const locale& __loc);
452
453   template<typename _Facet>
454     bool
455     has_facet(const locale& __loc) throw();
456 } // namespace std
457
458 #endif