char_traits.h

00001 /*
00002  * Copyright (c) 1997
00003  * Silicon Graphics Computer Systems, Inc.
00004  *
00005  * Permission to use, copy, modify, distribute and sell this software
00006  * and its documentation for any purpose is hereby granted without fee,
00007  * provided that the above copyright notice appear in all copies and
00008  * that both that copyright notice and this permission notice appear
00009  * in supporting documentation.  Silicon Graphics makes no
00010  * representations about the suitability of this software for any
00011  * purpose.  It is provided "as is" without express or implied warranty.
00012  */ 
00013 
00014 #ifndef __SGI_STL_CHAR_TRAITS_H
00015 #define __SGI_STL_CHAR_TRAITS_H
00016 
00017 #include <string.h>
00018 
00019 #ifndef UNDER_CE
00020 #include <wchar.h>
00021 
00022 #if defined(__STL_USE_NEW_IOSTREAMS) && !defined(__SGI_STL_IOSFWD)
00023 #include <iosfwd>
00024 #endif /* use new iostreams */
00025 
00026 #endif //UNDER_CE
00027 
00028 __STL_BEGIN_NAMESPACE
00029 
00030 // Class __char_traits_base.
00031 
00032 template <class _CharT, class _IntT> class __char_traits_base {
00033 public:
00034   typedef _CharT char_type;
00035   typedef _IntT int_type;
00036 #ifdef __STL_USE_NEW_IOSTREAMS
00037   typedef streamoff off_type;
00038   typedef streampos pos_type;
00039   typedef mbstate_t state_type;
00040 #endif /* __STL_USE_NEW_IOSTREAMS */
00041 
00042   static void assign(char_type& __c1, const char_type& __c2) { __c1 = __c2; }
00043   static bool eq(const _CharT& __c1, const _CharT& __c2) 
00044     { return __c1 == __c2; }
00045   static bool lt(const _CharT& __c1, const _CharT& __c2) 
00046     { return __c1 < __c2; }
00047 
00048   static int compare(const _CharT* __s1, const _CharT* __s2, size_t __n) {
00049     for (size_t __i = 0; __i < __n; ++__i)
00050       if (!eq(__s1[__i], __s2[__i]))
00051         return __s1[__i] < __s2[__i] ? -1 : 1;
00052     return 0;
00053   }
00054 
00055   static size_t length(const _CharT* __s) {
00056     const _CharT __nullchar = _CharT();
00057     size_t __i;
00058     for (__i = 0; !eq(__s[__i], __nullchar); ++__i)
00059       {}
00060     return __i;
00061   }
00062 
00063   static const _CharT* find(const _CharT* __s, size_t __n, const _CharT& __c)
00064   {
00065     for ( ; __n > 0 ; ++__s, --__n)
00066       if (eq(*__s, __c))
00067         return __s;
00068     return 0;
00069   }
00070 
00071   static _CharT* move(_CharT* __s1, const _CharT* __s2, size_t __n) {
00072     memmove(__s1, __s2, __n * sizeof(_CharT));
00073     return __s1;
00074   }
00075     
00076   static _CharT* copy(_CharT* __s1, const _CharT* __s2, size_t __n) {
00077     memcpy(__s1, __s2, __n * sizeof(_CharT));
00078     return __s1;
00079   } 
00080 
00081   static _CharT* assign(_CharT* __s, size_t __n, _CharT __c) {
00082     for (size_t __i = 0; __i < __n; ++__i)
00083       __s[__i] = __c;
00084     return __s;
00085   }
00086 
00087   static int_type not_eof(const int_type& __c) {
00088     return !eq_int_type(__c, eof()) ? __c : 0;
00089   }
00090 
00091   static char_type to_char_type(const int_type& __c) {
00092     return static_cast<char_type>(__c);
00093   }
00094 
00095   static int_type to_int_type(const char_type& __c) {
00096     return static_cast<int_type>(__c);
00097   }
00098 
00099   static bool eq_int_type(const int_type& __c1, const int_type& __c2) {
00100     return __c1 == __c2;
00101   }
00102 
00103   static int_type eof() {
00104     return static_cast<int_type>(-1);
00105   }
00106 };
00107 
00108 // Generic char_traits class.  Note that this class is provided only
00109 //  as a base for explicit specialization; it is unlikely to be useful
00110 //  as is for any particular user-defined type.  In particular, it 
00111 //  *will not work* for a non-POD type.
00112 
00113 template <class _CharT> class char_traits
00114   : public __char_traits_base<_CharT, _CharT>
00115 {};
00116 
00117 // Specialization for char.
00118 
00119 __STL_TEMPLATE_NULL class char_traits<char> 
00120   : public __char_traits_base<char, int>
00121 {
00122 public:
00123   static char_type to_char_type(const int_type& __c) {
00124     return static_cast<char_type>(static_cast<unsigned char>(__c));
00125   }
00126 
00127   static int_type to_int_type(const char_type& __c) {
00128     return static_cast<unsigned char>(__c);
00129   }
00130 
00131   static int compare(const char* __s1, const char* __s2, size_t __n) 
00132     { return memcmp(__s1, __s2, __n); }
00133   
00134   static size_t length(const char* __s) { return strlen(__s); }
00135 
00136   static void assign(char& __c1, const char& __c2) { __c1 = __c2; }
00137 
00138   static char* assign(char* __s, size_t __n, char __c)
00139     { memset(__s, __c, __n); return __s; }
00140 };
00141 
00142 // Specialization for wchar_t.
00143 
00144 __STL_TEMPLATE_NULL class char_traits<wchar_t>
00145   : public __char_traits_base<wchar_t, wint_t>
00146 {};
00147 
00148 
00149 __STL_END_NAMESPACE
00150 
00151 #endif /* __SGI_STL_CHAR_TRAITS_H */
00152 
00153 // Local Variables:
00154 // mode:C++
00155 // End:
00156 

Generated on Mon Jun 5 10:20:43 2006 for Intelligence.kdevelop by  doxygen 1.4.6