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

Generated on Wed Apr 27 18:35:13 2005 for libstdc++ source by  doxygen 1.4.2