char_traits.h

Go to the documentation of this file.
00001 // Character Traits for use by standard string and iostream -*- C++ -*-
00002 
00003 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002
00004 // Free Software Foundation, Inc.
00005 //
00006 // This file is part of the GNU ISO C++ Library.  This library is free
00007 // software; you can redistribute it and/or modify it under the
00008 // terms of the GNU General Public License as published by the
00009 // Free Software Foundation; either version 2, or (at your option)
00010 // any later version.
00011 
00012 // This library is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 
00017 // You should have received a copy of the GNU General Public License along
00018 // with this library; see the file COPYING.  If not, write to the Free
00019 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
00020 // USA.
00021 
00022 // As a special exception, you may use this file as part of a free software
00023 // library without restriction.  Specifically, if other files instantiate
00024 // templates or use macros or inline functions from this file, or you compile
00025 // this file and link it with other files to produce an executable, this
00026 // file does not by itself cause the resulting executable to be covered by
00027 // the GNU General Public License.  This exception does not however
00028 // invalidate any other reasons why the executable file might be covered by
00029 // the GNU General Public License.
00030 
00031 //
00032 // ISO C++ 14882: 21  Strings library
00033 //
00034 
00035 /** @file char_traits.h
00036  *  This is an internal header file, included by other library headers.
00037  *  You should not attempt to use it directly.
00038  */
00039 
00040 #ifndef _CPP_BITS_CHAR_TRAITS_H
00041 #define _CPP_BITS_CHAR_TRAITS_H 1
00042 
00043 #pragma GCC system_header
00044 
00045 #include <cstring>            // For memmove, memset, memchr
00046 #include <bits/fpos.h>        // For streampos
00047 
00048 namespace std 
00049 {
00050   // 21.1
00051   /**
00052    *  @brief  Basis for explicit traits specializations.
00053    *
00054    *  @note  For any given actual character type, this definition is
00055    *  probably wrong.
00056    *
00057    *  See http://gcc.gnu.org/onlinedocs/libstdc++/21_strings/howto.html#5
00058    *  for advice on how to make use of this class for "unusual" character
00059    *  types.
00060   */
00061   template<class _CharT>
00062     struct char_traits
00063     {
00064       typedef _CharT        char_type;
00065       // Unsigned as wint_t is unsigned.
00066       typedef unsigned long     int_type;
00067       typedef streampos     pos_type;
00068       typedef streamoff     off_type;
00069       typedef mbstate_t     state_type;
00070       
00071       static void 
00072       assign(char_type& __c1, const char_type& __c2);
00073 
00074       static bool 
00075       eq(const char_type& __c1, const char_type& __c2);
00076 
00077       static bool 
00078       lt(const char_type& __c1, const char_type& __c2);
00079 
00080       static int 
00081       compare(const char_type* __s1, const char_type* __s2, size_t __n);
00082 
00083       static size_t
00084       length(const char_type* __s);
00085 
00086       static const char_type* 
00087       find(const char_type* __s, size_t __n, const char_type& __a);
00088 
00089       static char_type* 
00090       move(char_type* __s1, const char_type* __s2, size_t __n);
00091 
00092       static char_type* 
00093       copy(char_type* __s1, const char_type* __s2, size_t __n);
00094 
00095       static char_type* 
00096       assign(char_type* __s, size_t __n, char_type __a);
00097 
00098       static char_type 
00099       to_char_type(const int_type& __c);
00100 
00101       static int_type 
00102       to_int_type(const char_type& __c);
00103 
00104       static bool 
00105       eq_int_type(const int_type& __c1, const int_type& __c2);
00106 
00107       static int_type 
00108       eof(); 
00109 
00110       static int_type 
00111       not_eof(const int_type& __c);
00112     };
00113 
00114 
00115   /// 21.1.3.1  char_traits specializations
00116   template<>
00117     struct char_traits<char>
00118     {
00119       typedef char      char_type;
00120       typedef int           int_type;
00121       typedef streampos     pos_type;
00122       typedef streamoff     off_type;
00123       typedef mbstate_t     state_type;
00124 
00125       static void 
00126       assign(char_type& __c1, const char_type& __c2)
00127       { __c1 = __c2; }
00128 
00129       static bool 
00130       eq(const char_type& __c1, const char_type& __c2)
00131       { return __c1 == __c2; }
00132 
00133       static bool 
00134       lt(const char_type& __c1, const char_type& __c2)
00135       { return __c1 < __c2; }
00136 
00137       static int 
00138       compare(const char_type* __s1, const char_type* __s2, size_t __n)
00139       { return memcmp(__s1, __s2, __n); }
00140 
00141       static size_t
00142       length(const char_type* __s)
00143       { return strlen(__s); }
00144 
00145       static const char_type* 
00146       find(const char_type* __s, size_t __n, const char_type& __a)
00147       { return static_cast<const char_type*>(memchr(__s, __a, __n)); }
00148 
00149       static char_type* 
00150       move(char_type* __s1, const char_type* __s2, size_t __n)
00151       { return static_cast<char_type*>(memmove(__s1, __s2, __n)); }
00152 
00153       static char_type* 
00154       copy(char_type* __s1, const char_type* __s2, size_t __n)
00155       {  return static_cast<char_type*>(memcpy(__s1, __s2, __n)); }
00156 
00157       static char_type* 
00158       assign(char_type* __s, size_t __n, char_type __a)
00159       { return static_cast<char_type*>(memset(__s, __a, __n)); }
00160 
00161       static char_type 
00162       to_char_type(const int_type& __c)
00163       { return static_cast<char_type>(__c); }
00164 
00165       // To keep both the byte 0xff and the eof symbol 0xffffffff
00166       // from ending up as 0xffffffff.
00167       static int_type 
00168       to_int_type(const char_type& __c)
00169       { return static_cast<int_type>(static_cast<unsigned char>(__c)); }
00170 
00171       static bool 
00172       eq_int_type(const int_type& __c1, const int_type& __c2)
00173       { return __c1 == __c2; }
00174 
00175       static int_type 
00176       eof() { return static_cast<int_type>(EOF); }
00177 
00178       static int_type 
00179       not_eof(const int_type& __c)
00180       { return (__c == eof()) ? 0 : __c; }
00181   };
00182 
00183 
00184 #ifdef _GLIBCPP_USE_WCHAR_T
00185   /// 21.1.3.2  char_traits specializations
00186   template<>
00187     struct char_traits<wchar_t>
00188     {
00189       typedef wchar_t       char_type;
00190       typedef wint_t        int_type;
00191       typedef streamoff     off_type;
00192       typedef wstreampos    pos_type;
00193       typedef mbstate_t     state_type;
00194       
00195       static void 
00196       assign(char_type& __c1, const char_type& __c2)
00197       { __c1 = __c2; }
00198 
00199       static bool 
00200       eq(const char_type& __c1, const char_type& __c2)
00201       { return __c1 == __c2; }
00202 
00203       static bool 
00204       lt(const char_type& __c1, const char_type& __c2)
00205       { return __c1 < __c2; }
00206 
00207       static int 
00208       compare(const char_type* __s1, const char_type* __s2, size_t __n)
00209       { return wmemcmp(__s1, __s2, __n); }
00210 
00211       static size_t
00212       length(const char_type* __s)
00213       { return wcslen(__s); }
00214 
00215       static const char_type* 
00216       find(const char_type* __s, size_t __n, const char_type& __a)
00217       { return wmemchr(__s, __a, __n); }
00218 
00219       static char_type* 
00220       move(char_type* __s1, const char_type* __s2, int_type __n)
00221       { return wmemmove(__s1, __s2, __n); }
00222 
00223       static char_type* 
00224       copy(char_type* __s1, const char_type* __s2, size_t __n)
00225       { return wmemcpy(__s1, __s2, __n); }
00226 
00227       static char_type* 
00228       assign(char_type* __s, size_t __n, char_type __a)
00229       { return wmemset(__s, __a, __n); }
00230 
00231       static char_type 
00232       to_char_type(const int_type& __c) { return char_type(__c); }
00233 
00234       static int_type 
00235       to_int_type(const char_type& __c) { return int_type(__c); }
00236 
00237       static bool 
00238       eq_int_type(const int_type& __c1, const int_type& __c2)
00239       { return __c1 == __c2; }
00240 
00241       static int_type 
00242       eof() { return static_cast<int_type>(WEOF); }
00243 
00244       static int_type 
00245       not_eof(const int_type& __c)
00246       { return eq_int_type(__c, eof()) ? 0 : __c; }
00247   };
00248 #endif //_GLIBCPP_USE_WCHAR_T
00249 
00250   template<typename _CharT, typename _Traits>
00251     struct _Char_traits_match
00252     {
00253       _CharT _M_c;
00254       _Char_traits_match(_CharT const& __c) : _M_c(__c) { }
00255 
00256       bool 
00257       operator()(_CharT const& __a) { return _Traits::eq(_M_c, __a); }
00258     };
00259 } // namespace std
00260 
00261 #endif

Generated on Thu Feb 10 23:22:53 2005 for libstdc++-v3 Source by  doxygen 1.4.0