2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libstdc++-v3 / include / bits / codecvt.h
blob80f9cba0b3589a1d65f35e19856b0b2e046bf05e
1 // Locale support (codecvt) -*- C++ -*-
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.
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.
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.
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.
31 // ISO C++ 14882: 22.2.1.5 Template class codecvt
34 // Written by Benjamin Kosnik <bkoz@cygnus.com>
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.
41 #ifndef _CODECVT_H
42 #define _CODECVT_H 1
44 #pragma GCC system_header
46 // 22.2.1.5 Template class codecvt
47 class codecvt_base
49 public:
50 enum result
52 ok,
53 partial,
54 error,
55 noconv
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
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;
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
81 return this->do_out(__state, __from, __from_end, __from_next,
82 __to, __to_end, __to_next);
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); }
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
96 return this->do_in(__state, __from, __from_end, __from_next,
97 __to, __to_end, __to_next);
100 int
101 encoding() const throw()
102 { return this->do_encoding(); }
104 bool
105 always_noconv() const throw()
106 { return this->do_always_noconv(); }
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); }
113 int
114 max_length() const throw()
115 { return this->do_max_length(); }
117 protected:
118 explicit
119 __codecvt_abstract_base(size_t __refs = 0) : locale::facet(__refs) { }
121 virtual
122 ~__codecvt_abstract_base() { }
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;
130 virtual result
131 do_unshift(state_type& __state, extern_type* __to,
132 extern_type* __to_end, extern_type*& __to_next) const = 0;
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;
140 virtual int
141 do_encoding() const throw() = 0;
143 virtual bool
144 do_always_noconv() const throw() = 0;
146 virtual int
147 do_length(state_type&, const extern_type* __from,
148 const extern_type* __end, size_t __max) const = 0;
150 virtual int
151 do_max_length() const throw() = 0;
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>
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;
167 protected:
168 __c_locale _M_c_locale_codecvt;
170 public:
171 static locale::id id;
173 explicit
174 codecvt(size_t __refs = 0)
175 : __codecvt_abstract_base<_InternT, _ExternT, _StateT> (__refs) { }
177 explicit
178 codecvt(__c_locale __cloc, size_t __refs = 0);
180 protected:
181 virtual
182 ~codecvt() { }
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;
190 virtual result
191 do_unshift(state_type& __state, extern_type* __to,
192 extern_type* __to_end, extern_type*& __to_next) const;
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;
200 virtual int
201 do_encoding() const throw();
203 virtual bool
204 do_always_noconv() const throw();
206 virtual int
207 do_length(state_type&, const extern_type* __from,
208 const extern_type* __end, size_t __max) const;
210 virtual int
211 do_max_length() const throw();
214 template<typename _InternT, typename _ExternT, typename _StateT>
215 locale::id codecvt<_InternT, _ExternT, _StateT>::id;
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>
222 public:
223 // Types:
224 typedef char intern_type;
225 typedef char extern_type;
226 typedef mbstate_t state_type;
228 protected:
229 __c_locale _M_c_locale_codecvt;
231 public:
232 static locale::id id;
234 explicit
235 codecvt(size_t __refs = 0);
237 explicit
238 codecvt(__c_locale __cloc, size_t __refs = 0);
240 protected:
241 virtual
242 ~codecvt();
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;
250 virtual result
251 do_unshift(state_type& __state, extern_type* __to,
252 extern_type* __to_end, extern_type*& __to_next) const;
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;
260 virtual int
261 do_encoding() const throw();
263 virtual bool
264 do_always_noconv() const throw();
266 virtual int
267 do_length(state_type&, const extern_type* __from,
268 const extern_type* __end, size_t __max) const;
270 virtual int
271 do_max_length() const throw();
274 #ifdef _GLIBCXX_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>
280 public:
281 // Types:
282 typedef wchar_t intern_type;
283 typedef char extern_type;
284 typedef mbstate_t state_type;
286 protected:
287 __c_locale _M_c_locale_codecvt;
289 public:
290 static locale::id id;
292 explicit
293 codecvt(size_t __refs = 0);
295 explicit
296 codecvt(__c_locale __cloc, size_t __refs = 0);
298 protected:
299 virtual
300 ~codecvt();
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;
308 virtual result
309 do_unshift(state_type& __state,
310 extern_type* __to, extern_type* __to_end,
311 extern_type*& __to_next) const;
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;
320 virtual
321 int do_encoding() const throw();
323 virtual
324 bool do_always_noconv() const throw();
326 virtual
327 int do_length(state_type&, const extern_type* __from,
328 const extern_type* __end, size_t __max) const;
330 virtual int
331 do_max_length() const throw();
333 #endif //_GLIBCXX_USE_WCHAR_T
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>
339 public:
340 explicit
341 codecvt_byname(const char* __s, size_t __refs = 0)
342 : codecvt<_InternT, _ExternT, _StateT>(__refs)
344 if (std::strcmp(__s, "C") != 0 && std::strcmp(__s, "POSIX") != 0)
346 _S_destroy_c_locale(this->_M_c_locale_codecvt);
347 _S_create_c_locale(this->_M_c_locale_codecvt, __s);
351 protected:
352 virtual
353 ~codecvt_byname() { }
356 // Include host and configuration specific partial specializations
357 // with additional functionality, if possible.
358 #ifdef _GLIBCXX_USE_WCHAR_T
359 #include <bits/codecvt_specializations.h>
360 #endif
362 #endif // _CODECVT_H