# codecvt.h

Go to the documentation of this file.

    00001 // Locale support (codecvt) -*- C++ -*-
    00002
    00003 // Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
    00004 //
    00005 // This file is part of the GNU ISO C++ Library.  This library is free
    00006 // software; you can redistribute it and/or modify it under the
    00007 // terms of the GNU General Public License as published by the
    00008 // Free Software Foundation; either version 2, or (at your option)
    00009 // any later version.
    00010
    00011 // This library is distributed in the hope that it will be useful,
    00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00014 // GNU General Public License for more details.
    00015
    00016 // You should have received a copy of the GNU General Public License along
    00017 // with this library; see the file COPYING.  If not, write to the Free
    00018 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
    00019 // USA.
    00020
    00021 // As a special exception, you may use this file as part of a free software
    00022 // library without restriction.  Specifically, if other files instantiate
    00023 // templates or use macros or inline functions from this file, or you compile
    00024 // this file and link it with other files to produce an executable, this
    00025 // file does not by itself cause the resulting executable to be covered by
    00026 // the GNU General Public License.  This exception does not however
    00027 // invalidate any other reasons why the executable file might be covered by
    00028 // the GNU General Public License.
    00029
    00030 //
    00031 // ISO C++ 14882: 22.2.1.5 Template class codecvt
    00032 //
    00033
    00034 // Written by Benjamin Kosnik <bkoz@cygnus.com>
    00035
    00041 #ifndef _CPP_BITS_CODECVT_H
    00042 #define _CPP_BITS_CODECVT_H 1
    00043
    00044 #pragma GCC system_header
    00045
    00046   //  22.2.1.5  Template class codecvt
    00047   class codecvt_base
    00048   {
    00049   public:
    00050     enum result
    00051     {
    00052       ok,
    00053       partial,
    00054       error,
    00055       noconv
    00056     };
    00057   };
    00058
    00059   // Template class __codecvt_abstract_base
    00060   // NB: An abstract base class that fills in the public inlines, so
    00061   // that the specializations don't have to re-copy the public
    00062   // interface.
    00063   template<typename _InternT, typename _ExternT, typename _StateT>
    00064     class __codecvt_abstract_base
    00065     : public locale::facet, public codecvt_base
    00066     {
    00067     public:
    00068       // Types:
    00069       typedef codecvt_base::result  result;
    00070       typedef _InternT          intern_type;
    00071       typedef _ExternT          extern_type;
    00072       typedef _StateT           state_type;
    00073
    00074       // 22.2.1.5.1 codecvt members
    00075       result
    00076       out(state_type& __state, const intern_type* __from,
    00077       const intern_type* __from_end, const intern_type*& __from_next,
    00078       extern_type* __to, extern_type* __to_end,
    00079       extern_type*& __to_next) const
    00080       {
    00081     return this->do_out(__state, __from, __from_end, __from_next,
    00082                 __to, __to_end, __to_next);
    00083       }
    00084
    00085       result
    00086       unshift(state_type& __state, extern_type* __to, extern_type* __to_end,
    00087           extern_type*& __to_next) const
    00088       { return this->do_unshift(__state, __to,__to_end,__to_next); }
    00089
    00090       result
    00091       in(state_type& __state, const extern_type* __from,
    00092      const extern_type* __from_end, const extern_type*& __from_next,
    00093      intern_type* __to, intern_type* __to_end,
    00094      intern_type*& __to_next) const
    00095       {
    00096     return this->do_in(__state, __from, __from_end, __from_next,
    00097                __to, __to_end, __to_next);
    00098       }
    00099
    00100       int
    00101       encoding() const throw()
    00102       { return this->do_encoding(); }
    00103
    00104       bool
    00105       always_noconv() const throw()
    00106       { return this->do_always_noconv(); }
    00107
    00108       int
    00109       length(const state_type& __state, const extern_type* __from,
    00110          const extern_type* __end, size_t __max) const
    00111       { return this->do_length(__state, __from, __end, __max); }
    00112
    00113       int
    00114       max_length() const throw()
    00115       { return this->do_max_length(); }
    00116
    00117     protected:
    00118       explicit
    00119       __codecvt_abstract_base(size_t __refs = 0) : locale::facet(__refs) { }
    00120
    00121       virtual
    00122       ~__codecvt_abstract_base() { }
    00123
    00124       virtual result
    00125       do_out(state_type& __state, const intern_type* __from,
    00126          const intern_type* __from_end, const intern_type*& __from_next,
    00127          extern_type* __to, extern_type* __to_end,
    00128          extern_type*& __to_next) const = 0;
    00129
    00130       virtual result
    00131       do_unshift(state_type& __state, extern_type* __to,
    00132          extern_type* __to_end, extern_type*& __to_next) const = 0;
    00133
    00134       virtual result
    00135       do_in(state_type& __state, const extern_type* __from,
    00136         const extern_type* __from_end, const extern_type*& __from_next,
    00137         intern_type* __to, intern_type* __to_end,
    00138         intern_type*& __to_next) const = 0;
    00139
    00140       virtual int
    00141       do_encoding() const throw() = 0;
    00142
    00143       virtual bool
    00144       do_always_noconv() const throw() = 0;
    00145
    00146       virtual int
    00147       do_length(const state_type&, const extern_type* __from,
    00148         const extern_type* __end, size_t __max) const = 0;
    00149
    00150       virtual int
    00151       do_max_length() const throw() = 0;
    00152     };
    00153
    00154   // 22.2.1.5 Template class codecvt
    00155   // NB: Generic, mostly useless implementation.
    00156   template<typename _InternT, typename _ExternT, typename _StateT>
    00157     class codecvt
    00158     : public __codecvt_abstract_base<_InternT, _ExternT, _StateT>
    00159     {
    00160     public:
    00161       // Types:
    00162       typedef codecvt_base::result  result;
    00163       typedef _InternT          intern_type;
    00164       typedef _ExternT          extern_type;
    00165       typedef _StateT           state_type;
    00166
    00167     protected:
    00168       __c_locale            _M_c_locale_codecvt;
    00169
    00170     public:
    00171       static locale::id         id;
    00172
    00173       explicit
    00174       codecvt(size_t __refs = 0)
    00175       : __codecvt_abstract_base<_InternT, _ExternT, _StateT> (__refs) { }
    00176
    00177       explicit
    00178       codecvt(__c_locale __cloc, size_t __refs = 0);
    00179
    00180     protected:
    00181       virtual
    00182       ~codecvt() { }
    00183
    00184       virtual result
    00185       do_out(state_type& __state, const intern_type* __from,
    00186          const intern_type* __from_end, const intern_type*& __from_next,
    00187          extern_type* __to, extern_type* __to_end,
    00188          extern_type*& __to_next) const;
    00189
    00190       virtual result
    00191       do_unshift(state_type& __state, extern_type* __to,
    00192          extern_type* __to_end, extern_type*& __to_next) const;
    00193
    00194       virtual result
    00195       do_in(state_type& __state, const extern_type* __from,
    00196         const extern_type* __from_end, const extern_type*& __from_next,
    00197         intern_type* __to, intern_type* __to_end,
    00198         intern_type*& __to_next) const;
    00199
    00200       virtual int
    00201       do_encoding() const throw();
    00202
    00203       virtual bool
    00204       do_always_noconv() const throw();
    00205
    00206       virtual int
    00207       do_length(const state_type&, const extern_type* __from,
    00208         const extern_type* __end, size_t __max) const;
    00209
    00210       virtual int
    00211       do_max_length() const throw();
    00212     };
    00213
    00214   template<typename _InternT, typename _ExternT, typename _StateT>
    00215     locale::id codecvt<_InternT, _ExternT, _StateT>::id;
    00216
    00217   // codecvt<char, char, mbstate_t> required specialization
    00218   template<>
    00219     class codecvt<char, char, mbstate_t>
    00220     : public __codecvt_abstract_base<char, char, mbstate_t>
    00221     {
    00222     public:
    00223       // Types:
    00224       typedef char          intern_type;
    00225       typedef char          extern_type;
    00226       typedef mbstate_t         state_type;
    00227
    00228     protected:
    00229       __c_locale            _M_c_locale_codecvt;
    00230
    00231     public:
    00232       static locale::id id;
    00233
    00234       explicit
    00235       codecvt(size_t __refs = 0);
    00236
    00237       explicit
    00238       codecvt(__c_locale __cloc, size_t __refs = 0);
    00239
    00240     protected:
    00241       virtual
    00242       ~codecvt();
    00243
    00244       virtual result
    00245       do_out(state_type& __state, const intern_type* __from,
    00246          const intern_type* __from_end, const intern_type*& __from_next,
    00247          extern_type* __to, extern_type* __to_end,
    00248          extern_type*& __to_next) const;
    00249
    00250       virtual result
    00251       do_unshift(state_type& __state, extern_type* __to,
    00252          extern_type* __to_end, extern_type*& __to_next) const;
    00253
    00254       virtual result
    00255       do_in(state_type& __state, const extern_type* __from,
    00256         const extern_type* __from_end, const extern_type*& __from_next,
    00257         intern_type* __to, intern_type* __to_end,
    00258         intern_type*& __to_next) const;
    00259
    00260       virtual int
    00261       do_encoding() const throw();
    00262
    00263       virtual bool
    00264       do_always_noconv() const throw();
    00265
    00266       virtual int
    00267       do_length(const state_type&, const extern_type* __from,
    00268         const extern_type* __end, size_t __max) const;
    00269
    00270       virtual int
    00271       do_max_length() const throw();
    00272   };
    00273
    00274 #ifdef _GLIBCPP_USE_WCHAR_T
    00275   // codecvt<wchar_t, char, mbstate_t> required specialization
    00276   template<>
    00277     class codecvt<wchar_t, char, mbstate_t>
    00278     : public __codecvt_abstract_base<wchar_t, char, mbstate_t>
    00279     {
    00280     public:
    00281       // Types:
    00282       typedef wchar_t           intern_type;
    00283       typedef char          extern_type;
    00284       typedef mbstate_t         state_type;
    00285
    00286     protected:
    00287       __c_locale            _M_c_locale_codecvt;
    00288
    00289     public:
    00290       static locale::id         id;
    00291
    00292       explicit
    00293       codecvt(size_t __refs = 0);
    00294
    00295       explicit
    00296       codecvt(__c_locale __cloc, size_t __refs = 0);
    00297
    00298     protected:
    00299       virtual
    00300       ~codecvt();
    00301
    00302       virtual result
    00303       do_out(state_type& __state, const intern_type* __from,
    00304          const intern_type* __from_end, const intern_type*& __from_next,
    00305          extern_type* __to, extern_type* __to_end,
    00306          extern_type*& __to_next) const;
    00307
    00308       virtual result
    00309       do_unshift(state_type& __state,
    00310          extern_type* __to, extern_type* __to_end,
    00311          extern_type*& __to_next) const;
    00312
    00313       virtual result
    00314       do_in(state_type& __state,
    00315          const extern_type* __from, const extern_type* __from_end,
    00316          const extern_type*& __from_next,
    00317          intern_type* __to, intern_type* __to_end,
    00318          intern_type*& __to_next) const;
    00319
    00320       virtual
    00321       int do_encoding() const throw();
    00322
    00323       virtual
    00324       bool do_always_noconv() const throw();
    00325
    00326       virtual
    00327       int do_length(const state_type&, const extern_type* __from,
    00328             const extern_type* __end, size_t __max) const;
    00329
    00330       virtual int
    00331       do_max_length() const throw();
    00332     };
    00333 #endif //_GLIBCPP_USE_WCHAR_T
    00334
    00335   // 22.2.1.6  Template class codecvt_byname
    00336   template<typename _InternT, typename _ExternT, typename _StateT>
    00337     class codecvt_byname : public codecvt<_InternT, _ExternT, _StateT>
    00338     {
    00339     public:
    00340       explicit
    00341       codecvt_byname(const char* __s, size_t __refs = 0)
    00342       : codecvt<_InternT, _ExternT, _StateT>(__refs)
    00343       {
    00344     if (_M_c_locale_codecvt != _S_c_locale)
    00345       _S_destroy_c_locale(_M_c_locale_codecvt);
    00346     _S_create_c_locale(_M_c_locale_codecvt, __s);
    00347       }
    00348
    00349     protected:
    00350       virtual
    00351       ~codecvt_byname() { }
    00352     };
    00353
    00354   // Include host and configuration specific partial specializations
    00355   // with additional functionality, if possible.
    00356 #ifdef _GLIBCPP_USE_WCHAR_T
    00357   #include <bits/codecvt_specializations.h>
    00358 #endif
    00359
    00360 #endif // _CPP_BITS_CODECVT_H