OSDN Git Service

2003-02-13 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / bits / codecvt.h
1 // Locale support (codecvt) -*- C++ -*-
2
3 // Copyright (C) 2000, 2001, 2002, 2003 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.2.1.5 Template class codecvt
32 //
33
34 // Written by Benjamin Kosnik <bkoz@cygnus.com>
35
36 /** @file codecvt.h
37  *  This is an internal header file, included by other library headers.
38  *  You should not attempt to use it directly.
39  */
40
41 #ifndef _CPP_BITS_CODECVT_H
42 #define _CPP_BITS_CODECVT_H     1
43
44 #pragma GCC system_header
45
46   //  22.2.1.5  Template class codecvt
47   class codecvt_base
48   {
49   public:
50     enum result
51     {
52       ok,
53       partial,
54       error,
55       noconv
56     };
57   };
58
59   // Template class __codecvt_abstract_base
60   // NB: An abstract base class that fills in the public inlines, so
61   // that the specializations don't have to re-copy the public
62   // interface.
63   template<typename _InternT, typename _ExternT, typename _StateT>
64     class __codecvt_abstract_base 
65     : public locale::facet, public codecvt_base
66     {
67     public:
68       // Types:
69       typedef codecvt_base::result      result;
70       typedef _InternT                  intern_type;
71       typedef _ExternT                  extern_type;
72       typedef _StateT                   state_type;
73       
74       // 22.2.1.5.1 codecvt members
75       result
76       out(state_type& __state, const intern_type* __from, 
77           const intern_type* __from_end, const intern_type*& __from_next,
78           extern_type* __to, extern_type* __to_end, 
79           extern_type*& __to_next) const
80       { 
81         return this->do_out(__state, __from, __from_end, __from_next, 
82                             __to, __to_end, __to_next); 
83       }
84
85       result
86       unshift(state_type& __state, extern_type* __to, extern_type* __to_end,
87               extern_type*& __to_next) const
88       { return this->do_unshift(__state, __to,__to_end,__to_next); }
89
90       result
91       in(state_type& __state, const extern_type* __from, 
92          const extern_type* __from_end, const extern_type*& __from_next,
93          intern_type* __to, intern_type* __to_end, 
94          intern_type*& __to_next) const
95       { 
96         return this->do_in(__state, __from, __from_end, __from_next,
97                            __to, __to_end, __to_next); 
98       }
99
100       int 
101       encoding() const throw()
102       { return this->do_encoding(); }
103
104       bool 
105       always_noconv() const throw()
106       { return this->do_always_noconv(); }
107
108       int
109       length(state_type& __state, const extern_type* __from,
110              const extern_type* __end, size_t __max) const
111       { return this->do_length(__state, __from, __end, __max); }
112
113       int 
114       max_length() const throw()
115       { return this->do_max_length(); }
116
117     protected:
118       explicit 
119       __codecvt_abstract_base(size_t __refs = 0) : locale::facet(__refs) { }
120
121       virtual 
122       ~__codecvt_abstract_base() { }
123
124       virtual result
125       do_out(state_type& __state, const intern_type* __from, 
126              const intern_type* __from_end, const intern_type*& __from_next,
127              extern_type* __to, extern_type* __to_end,
128              extern_type*& __to_next) const = 0;
129
130       virtual result
131       do_unshift(state_type& __state, extern_type* __to, 
132                  extern_type* __to_end, extern_type*& __to_next) const = 0;
133       
134       virtual result
135       do_in(state_type& __state, const extern_type* __from, 
136             const extern_type* __from_end, const extern_type*& __from_next, 
137             intern_type* __to, intern_type* __to_end, 
138             intern_type*& __to_next) const = 0;
139       
140       virtual int 
141       do_encoding() const throw() = 0;
142
143       virtual bool 
144       do_always_noconv() const throw() = 0;
145
146       virtual int 
147       do_length(state_type&, const extern_type* __from, 
148                 const extern_type* __end, size_t __max) const = 0;
149
150       virtual int 
151       do_max_length() const throw() = 0;
152     };
153
154   // 22.2.1.5 Template class codecvt
155   // NB: Generic, mostly useless implementation.
156   template<typename _InternT, typename _ExternT, typename _StateT>
157     class codecvt 
158     : public __codecvt_abstract_base<_InternT, _ExternT, _StateT>
159     {
160     public:      
161       // Types:
162       typedef codecvt_base::result      result;
163       typedef _InternT                  intern_type;
164       typedef _ExternT                  extern_type;
165       typedef _StateT                   state_type;
166
167     protected:
168       __c_locale                        _M_c_locale_codecvt;
169
170     public:
171       static locale::id                 id;
172
173       explicit 
174       codecvt(size_t __refs = 0) 
175       : __codecvt_abstract_base<_InternT, _ExternT, _StateT> (__refs) { }
176
177       explicit 
178       codecvt(__c_locale __cloc, size_t __refs = 0);
179
180     protected:
181       virtual 
182       ~codecvt() { }
183
184       virtual result
185       do_out(state_type& __state, const intern_type* __from, 
186              const intern_type* __from_end, const intern_type*& __from_next,
187              extern_type* __to, extern_type* __to_end,
188              extern_type*& __to_next) const;
189
190       virtual result
191       do_unshift(state_type& __state, extern_type* __to, 
192                  extern_type* __to_end, extern_type*& __to_next) const;
193       
194       virtual result
195       do_in(state_type& __state, const extern_type* __from, 
196             const extern_type* __from_end, const extern_type*& __from_next, 
197             intern_type* __to, intern_type* __to_end, 
198             intern_type*& __to_next) const;
199       
200       virtual int 
201       do_encoding() const throw();
202
203       virtual bool 
204       do_always_noconv() const throw();
205
206       virtual int 
207       do_length(state_type&, const extern_type* __from, 
208                 const extern_type* __end, size_t __max) const;
209
210       virtual int 
211       do_max_length() const throw();
212     };
213
214   template<typename _InternT, typename _ExternT, typename _StateT>
215     locale::id codecvt<_InternT, _ExternT, _StateT>::id;
216
217   // codecvt<char, char, mbstate_t> required specialization
218   template<>
219     class codecvt<char, char, mbstate_t> 
220     : public __codecvt_abstract_base<char, char, mbstate_t>
221     {
222     public:      
223       // Types:
224       typedef char                      intern_type;
225       typedef char                      extern_type;
226       typedef mbstate_t                 state_type;
227
228     protected:
229       __c_locale                        _M_c_locale_codecvt;
230
231     public:
232       static locale::id id;
233
234       explicit 
235       codecvt(size_t __refs = 0);
236
237       explicit 
238       codecvt(__c_locale __cloc, size_t __refs = 0);
239
240     protected:
241       virtual 
242       ~codecvt();
243
244       virtual result
245       do_out(state_type& __state, const intern_type* __from, 
246              const intern_type* __from_end, const intern_type*& __from_next,
247              extern_type* __to, extern_type* __to_end,
248              extern_type*& __to_next) const;
249
250       virtual result
251       do_unshift(state_type& __state, extern_type* __to, 
252                  extern_type* __to_end, extern_type*& __to_next) const;
253
254       virtual result
255       do_in(state_type& __state, const extern_type* __from, 
256             const extern_type* __from_end, const extern_type*& __from_next,
257             intern_type* __to, intern_type* __to_end, 
258             intern_type*& __to_next) const;
259
260       virtual int 
261       do_encoding() const throw();
262
263       virtual bool 
264       do_always_noconv() const throw();
265
266       virtual int 
267       do_length(state_type&, const extern_type* __from, 
268                 const extern_type* __end, size_t __max) const;
269
270       virtual int 
271       do_max_length() const throw();
272   };
273
274 #ifdef _GLIBCPP_USE_WCHAR_T
275   // codecvt<wchar_t, char, mbstate_t> required specialization
276   template<>
277     class codecvt<wchar_t, char, mbstate_t> 
278     : public __codecvt_abstract_base<wchar_t, char, mbstate_t>
279     {
280     public:
281       // Types:
282       typedef wchar_t                   intern_type;
283       typedef char                      extern_type;
284       typedef mbstate_t                 state_type;
285
286     protected:
287       __c_locale                        _M_c_locale_codecvt;
288
289     public:
290       static locale::id                 id;
291
292       explicit 
293       codecvt(size_t __refs = 0);
294
295       explicit 
296       codecvt(__c_locale __cloc, size_t __refs = 0);
297
298     protected:
299       virtual 
300       ~codecvt();
301
302       virtual result
303       do_out(state_type& __state, const intern_type* __from, 
304              const intern_type* __from_end, const intern_type*& __from_next,
305              extern_type* __to, extern_type* __to_end,
306              extern_type*& __to_next) const;
307
308       virtual result
309       do_unshift(state_type& __state,
310                  extern_type* __to, extern_type* __to_end,
311                  extern_type*& __to_next) const;
312
313       virtual result
314       do_in(state_type& __state,
315              const extern_type* __from, const extern_type* __from_end,
316              const extern_type*& __from_next,
317              intern_type* __to, intern_type* __to_end,
318              intern_type*& __to_next) const;
319
320       virtual 
321       int do_encoding() const throw();
322
323       virtual 
324       bool do_always_noconv() const throw();
325
326       virtual 
327       int do_length(state_type&, const extern_type* __from,
328                     const extern_type* __end, size_t __max) const;
329
330       virtual int 
331       do_max_length() const throw();
332     };
333 #endif //_GLIBCPP_USE_WCHAR_T
334
335   // 22.2.1.6  Template class codecvt_byname
336   template<typename _InternT, typename _ExternT, typename _StateT>
337     class codecvt_byname : public codecvt<_InternT, _ExternT, _StateT>
338     {
339     public:
340       explicit 
341       codecvt_byname(const char* __s, size_t __refs = 0) 
342       : codecvt<_InternT, _ExternT, _StateT>(__refs)
343       { 
344         if (this->_M_c_locale_codecvt != this->_S_c_locale)
345           _S_destroy_c_locale(this->_M_c_locale_codecvt);
346         _S_create_c_locale(this->_M_c_locale_codecvt, __s); 
347       }
348
349     protected:
350       virtual 
351       ~codecvt_byname() { }
352     };
353
354   // Include host and configuration specific partial specializations
355   // with additional functionality, if possible.
356 #ifdef _GLIBCPP_USE_WCHAR_T
357   #include <bits/codecvt_specializations.h>
358 #endif
359
360 #endif // _CPP_BITS_CODECVT_H