OSDN Git Service

2001-08-07 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 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 // Warning: this file is not meant for user inclusion.  Use <locale>.
35
36 // Written by Benjamin Kosnik <bkoz@cygnus.com>
37
38 #ifndef _CPP_BITS_CODECVT_H
39 #define _CPP_BITS_CODECVT_H     1
40
41 #pragma GCC system_header
42
43   //  22.2.1.5  Template class codecvt
44   class codecvt_base
45   {
46   public:
47     enum result
48     {
49       ok,
50       partial,
51       error,
52       noconv
53     };
54   };
55
56   // Template class __codecvt_abstract_base
57   // NB: An abstract base class that fills in the public inlines, so
58   // that the specializations don't have to re-copy the public
59   // interface.
60   template<typename _InternT, typename _ExternT, typename _StateT>
61     class __codecvt_abstract_base 
62     : public locale::facet, public codecvt_base
63     {
64     public:
65       // Types:
66       typedef codecvt_base::result                      result;
67       typedef _InternT                                  intern_type;
68       typedef _ExternT                                  extern_type;
69       typedef _StateT                                   state_type;
70       
71       // 22.2.1.5.1 codecvt members
72       result
73       out(state_type& __state, const intern_type* __from, 
74           const intern_type* __from_end, const intern_type*& __from_next,
75           extern_type* __to, extern_type* __to_end, 
76           extern_type*& __to_next) const
77       { 
78         return this->do_out(__state, __from, __from_end, __from_next, 
79                             __to, __to_end, __to_next); 
80       }
81
82       result
83       unshift(state_type& __state, extern_type* __to, extern_type* __to_end,
84               extern_type*& __to_next) const
85       { return this->do_unshift(__state, __to,__to_end,__to_next); }
86
87       result
88       in(state_type& __state, const extern_type* __from, 
89          const extern_type* __from_end, const extern_type*& __from_next,
90          intern_type* __to, intern_type* __to_end, 
91          intern_type*& __to_next) const
92       { 
93         return this->do_in(__state, __from, __from_end, __from_next,
94                            __to, __to_end, __to_next); 
95       }
96
97       int 
98       encoding() const throw()
99       { return this->do_encoding(); }
100
101       bool 
102       always_noconv() const throw()
103       { return this->do_always_noconv(); }
104
105       int
106       length(const state_type& __state, const extern_type* __from,
107              const extern_type* __end, size_t __max) const
108       { return this->do_length(__state, __from, __end, __max); }
109
110       int 
111       max_length() const throw()
112       { return this->do_max_length(); }
113
114     protected:
115       explicit 
116       __codecvt_abstract_base(size_t __refs = 0) : locale::facet(__refs) { }
117
118       virtual 
119       ~__codecvt_abstract_base() { }
120
121       virtual result
122       do_out(state_type& __state, const intern_type* __from, 
123              const intern_type* __from_end, const intern_type*& __from_next,
124              extern_type* __to, extern_type* __to_end,
125              extern_type*& __to_next) const = 0;
126
127       virtual result
128       do_unshift(state_type& __state, extern_type* __to, 
129                  extern_type* __to_end, extern_type*& __to_next) const = 0;
130       
131       virtual result
132       do_in(state_type& __state, const extern_type* __from, 
133             const extern_type* __from_end, const extern_type*& __from_next, 
134             intern_type* __to, intern_type* __to_end, 
135             intern_type*& __to_next) const = 0;
136       
137       virtual int 
138       do_encoding() const throw() = 0;
139
140       virtual bool 
141       do_always_noconv() const throw() = 0;
142
143       virtual int 
144       do_length(const state_type&, const extern_type* __from, 
145                 const extern_type* __end, size_t __max) const = 0;
146
147       virtual int 
148       do_max_length() const throw() = 0;
149     };
150
151   // 22.2.1.5 Template class codecvt
152   // NB: Generic, mostly useless implementation.
153   template<typename _InternT, typename _ExternT, typename _StateT>
154     class codecvt 
155     : public __codecvt_abstract_base<_InternT, _ExternT, _StateT>
156     {
157     public:      
158       // Types:
159       typedef codecvt_base::result                      result;
160       typedef _InternT intern_type;
161       typedef _ExternT extern_type;
162       typedef _StateT  state_type;
163
164       // Data Members:
165       static locale::id id;
166
167       explicit 
168       codecvt(size_t __refs = 0) 
169       : __codecvt_abstract_base<_InternT,_ExternT,_StateT> (__refs) { }
170
171     protected:
172       virtual 
173       ~codecvt() { }
174     };
175
176   template<typename _InternT, typename _ExternT, typename _StateT>
177     locale::id codecvt<_InternT, _ExternT, _StateT>::id;
178
179   // codecvt<char, char, mbstate_t> required specialization
180   template<>
181     class codecvt<char, char, mbstate_t> 
182     : public __codecvt_abstract_base<char, char, mbstate_t>
183     {
184     public:      
185       // Types:
186       typedef char      intern_type;
187       typedef char      extern_type;
188       typedef mbstate_t state_type;
189
190       // Data Members:
191       static locale::id id;
192
193       explicit 
194       codecvt(size_t __refs = 0);
195
196     protected:
197       virtual 
198       ~codecvt();
199
200       virtual result
201       do_out(state_type& __state, const intern_type* __from, 
202              const intern_type* __from_end, const intern_type*& __from_next,
203              extern_type* __to, extern_type* __to_end,
204              extern_type*& __to_next) const;
205
206       virtual result
207       do_unshift(state_type& __state, extern_type* __to, 
208                  extern_type* __to_end, extern_type*& __to_next) const;
209
210       virtual result
211       do_in(state_type& __state, const extern_type* __from, 
212             const extern_type* __from_end, const extern_type*& __from_next,
213             intern_type* __to, intern_type* __to_end, 
214             intern_type*& __to_next) const;
215
216       virtual int 
217       do_encoding() const throw();
218
219       virtual bool 
220       do_always_noconv() const throw();
221
222       virtual int 
223       do_length(const state_type&, const extern_type* __from, 
224                 const extern_type* __end, size_t __max) const;
225
226       virtual int 
227       do_max_length() const throw();
228   };
229
230 #ifdef _GLIBCPP_USE_WCHAR_T
231   // codecvt<wchar_t, char, mbstate_t> required specialization
232   template<>
233     class codecvt<wchar_t, char, mbstate_t> 
234     : public __codecvt_abstract_base<wchar_t, char, mbstate_t>
235     {
236     public:
237       // Types:
238       typedef wchar_t   intern_type;
239       typedef char      extern_type;
240       typedef mbstate_t state_type;
241
242       // Data Members:
243       static locale::id id;
244
245       explicit 
246       codecvt(size_t __refs = 0);
247
248     protected:
249       virtual 
250       ~codecvt();
251
252       virtual result
253       do_out(state_type& __state, const intern_type* __from, 
254              const intern_type* __from_end, const intern_type*& __from_next,
255              extern_type* __to, extern_type* __to_end,
256              extern_type*& __to_next) const;
257
258       virtual result
259       do_unshift(state_type& __state,
260                  extern_type* __to, extern_type* __to_end,
261                  extern_type*& __to_next) const;
262
263       virtual result
264       do_in(state_type& __state,
265              const extern_type* __from, const extern_type* __from_end,
266              const extern_type*& __from_next,
267              intern_type* __to, intern_type* __to_end,
268              intern_type*& __to_next) const;
269
270       virtual 
271       int do_encoding() const throw();
272
273       virtual 
274       bool do_always_noconv() const throw();
275
276       virtual 
277       int do_length(const state_type&, const extern_type* __from,
278                     const extern_type* __end, size_t __max) const;
279
280       virtual int 
281       do_max_length() const throw();
282     };
283 #endif //_GLIBCPP_USE_WCHAR_T
284
285   // 22.2.1.6  Template class codecvt_byname
286   template<typename _InternT, typename _ExternT, typename _StateT>
287     class codecvt_byname : public codecvt<_InternT, _ExternT, _StateT>
288     {
289     public:
290       explicit 
291       codecvt_byname(const char*, size_t __refs = 0) 
292       : codecvt<_InternT, _ExternT, _StateT>(__refs) { }
293     protected:
294       virtual 
295       ~codecvt_byname() { }
296     };
297
298   // Include host and configuration specific partial specializations
299   // with additional functionality, if possible.
300 #ifdef _GLIBCPP_USE_WCHAR_T
301   #include <bits/codecvt_specializations.h>
302 #endif
303
304 #endif // _CPP_BITS_CODECVT_H
305
306
307
308