pod_char_traits.h

Go to the documentation of this file.
00001 // POD character, std::char_traits specialization -*- C++ -*-
00002 
00003 // Copyright (C) 2002, 2003 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 // Gabriel Dos Reis <gdr@integrable-solutions.net>
00031 // Benjamin Kosnik <bkoz@redhat.com>
00032 
00033 #ifndef _POD_CHAR_TRAITS_H
00034 #define _POD_CHAR_TRAITS_H 1
00035 
00036 #include <string>
00037 
00038 namespace __gnu_cxx
00039 {
00040   template<typename V, typename I, typename S = mbstate_t>
00041     struct character
00042     {
00043       typedef V     value_type;
00044       typedef I     int_type;
00045       typedef S     state_type;
00046       value_type    value;
00047     };
00048 
00049   template<typename V, typename I>
00050     inline bool
00051     operator==(const character<V, I>& lhs, const character<V, I>& rhs)
00052     { return lhs.value == rhs.value; }
00053 
00054   template<typename V, typename I>
00055     inline bool
00056     operator<(const character<V, I>& lhs, const character<V, I>& rhs)
00057     { return lhs.value < rhs.value; }
00058 } // namespace __gnu_cxx
00059 
00060 namespace std
00061 {
00062   // Provide std::char_traits specialization.
00063   template<typename V, typename I, typename S>
00064     struct char_traits<__gnu_cxx::character<V, I, S> >
00065     {
00066       typedef __gnu_cxx::character<V, I, S> char_type;
00067 
00068       // NB: This type should be bigger than char_type, so as to
00069       // properly hold EOF values in addition to the full range of
00070       // char_type values.
00071       // Also, assumes
00072       // int_type(value_type) is valid.
00073       // int_type(-1) is possible.
00074       typedef typename char_type::int_type  int_type;
00075       typedef typename char_type::state_type    state_type;
00076       typedef fpos<state_type>          pos_type;
00077       typedef streamoff             off_type;
00078 
00079       static void
00080       assign(char_type& __c1, const char_type& __c2)
00081       { __c1 = __c2; }
00082 
00083       static bool
00084       eq(const char_type& __c1, const char_type& __c2)
00085       { return __c1 == __c2; }
00086 
00087       static bool
00088       lt(const char_type& __c1, const char_type& __c2)
00089       { return __c1 < __c2; }
00090 
00091       static int
00092       compare(const char_type* __s1, const char_type* __s2, size_t __n)
00093       {
00094     for (size_t __i = 0; __i < __n; ++__i)
00095       if (!eq(__s1[__i], __s2[__i]))
00096         return lt(__s1[__i], __s2[__i]) ? -1 : 1;
00097     return 0;
00098       }
00099 
00100       static size_t
00101       length(const char_type* __s)
00102       {
00103     const char_type* __p = __s;
00104     while (__p->value)
00105       ++__p;
00106     return (__p - __s);
00107       }
00108 
00109       static const char_type*
00110       find(const char_type* __s, size_t __n, const char_type& __a)
00111       {
00112     for (const char_type* __p = __s; size_t(__p - __s) < __n; ++__p)
00113       if (*__p == __a)
00114         return __p;
00115     return 0;
00116       }
00117 
00118       static char_type*
00119       move(char_type* __s1, const char_type* __s2, size_t __n)
00120       { return (char_type*) memmove(__s1, __s2, __n * sizeof(char_type)); }
00121 
00122       static char_type*
00123       copy(char_type* __s1, const char_type* __s2, size_t __n)
00124       { return (char_type*) memcpy(__s1, __s2, __n * sizeof(char_type)); }
00125 
00126       static char_type*
00127       assign(char_type* __s, size_t __n, char_type __a)
00128       {
00129     for (char_type* __p = __s; __p < __s + __n; ++__p)
00130       assign(*__p, __a);
00131         return __s;
00132       }
00133 
00134       static char_type
00135       to_char_type(const int_type& __c)
00136       {
00137     char_type __r = { __c };
00138     return __r;
00139       }
00140 
00141       static int_type
00142       to_int_type(const char_type& __c)
00143       { return int_type(__c.value); }
00144 
00145       static bool
00146       eq_int_type(const int_type& __c1, const int_type& __c2)
00147       { return __c1 == __c2; }
00148 
00149       static int_type
00150       eof() { return static_cast<int_type>(-1); }
00151 
00152       static int_type
00153       not_eof(const int_type& __c)
00154       { return eq_int_type(__c, eof()) ? int_type(0) : __c; }
00155     };
00156 }
00157 
00158 #endif

Generated on Tue Jan 30 17:31:52 2007 for GNU C++ STL by doxygen 1.3.6