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, 2005 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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
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 
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   // POD character abstraction.
00045   // NB: The char_type parameter is a subset of int_type, as to allow
00046   // int_type to properly hold the full range of char_type values as
00047   // well as EOF.
00049   template<typename V, typename I, typename S = mbstate_t>
00050     struct character
00051     {
00052       typedef V             value_type;
00053       typedef I             int_type;
00054       typedef S             state_type;
00055       typedef character<V, I, S>    char_type;
00056 
00057       value_type    value;
00058 
00059       template<typename V2>
00060         static char_type
00061         from(const V2& v)
00062         {
00063       char_type ret = { static_cast<value_type>(v) };
00064       return ret;
00065     }
00066 
00067       template<typename V2>
00068         static V2
00069         to(const char_type& c)
00070         {
00071       V2 ret = { static_cast<V2>(c.value) };
00072       return ret;
00073     }
00074 
00075     };
00076 
00077   template<typename V, typename I, typename S>
00078     inline bool
00079     operator==(const character<V, I, S>& lhs, const character<V, I, S>& rhs)
00080     { return lhs.value == rhs.value; }
00081 
00082   template<typename V, typename I, typename S>
00083     inline bool
00084     operator<(const character<V, I, S>& lhs, const character<V, I, S>& rhs)
00085     { return lhs.value < rhs.value; }
00086 } // namespace __gnu_cxx
00087 
00088 namespace std
00089 {
00091   template<typename V, typename I, typename S>
00092     struct char_traits<__gnu_cxx::character<V, I, S> >
00093     {
00094       typedef __gnu_cxx::character<V, I, S> char_type;
00095       typedef typename char_type::int_type  int_type;
00096       typedef typename char_type::state_type    state_type;
00097       typedef fpos<state_type>          pos_type;
00098       typedef streamoff             off_type;
00099 
00100       static void
00101       assign(char_type& __c1, const char_type& __c2)
00102       { __c1 = __c2; }
00103 
00104       static bool
00105       eq(const char_type& __c1, const char_type& __c2)
00106       { return __c1 == __c2; }
00107 
00108       static bool
00109       lt(const char_type& __c1, const char_type& __c2)
00110       { return __c1 < __c2; }
00111 
00112       static int
00113       compare(const char_type* __s1, const char_type* __s2, size_t __n)
00114       {
00115     for (size_t __i = 0; __i < __n; ++__i)
00116       if (!eq(__s1[__i], __s2[__i]))
00117         return lt(__s1[__i], __s2[__i]) ? -1 : 1;
00118     return 0;
00119       }
00120 
00121       static size_t
00122       length(const char_type* __s)
00123       {
00124     const char_type* __p = __s;
00125     while (__p->value)
00126       ++__p;
00127     return (__p - __s);
00128       }
00129 
00130       static const char_type*
00131       find(const char_type* __s, size_t __n, const char_type& __a)
00132       {
00133     for (const char_type* __p = __s; size_t(__p - __s) < __n; ++__p)
00134       if (*__p == __a)
00135         return __p;
00136     return 0;
00137       }
00138 
00139       static char_type*
00140       move(char_type* __s1, const char_type* __s2, size_t __n)
00141       { 
00142     return static_cast<char_type*>(std::memmove(__s1, __s2, 
00143                             __n * sizeof(char_type))); 
00144       }
00145 
00146       static char_type*
00147       copy(char_type* __s1, const char_type* __s2, size_t __n)
00148       {
00149     std::copy(__s2, __s2 + __n, __s1);
00150     return __s1;
00151       }
00152 
00153       static char_type*
00154       assign(char_type* __s, size_t __n, char_type __a)
00155       {
00156     std::fill_n(__s, __n, __a);
00157         return __s;
00158       }
00159 
00160       static char_type
00161       to_char_type(const int_type& __i)
00162       { return char_type::template from(__i); }
00163 
00164       static int_type
00165       to_int_type(const char_type& __c)
00166       { return char_type::template to<int_type>(__c); }
00167 
00168       static bool
00169       eq_int_type(const int_type& __c1, const int_type& __c2)
00170       { return __c1 == __c2; }
00171 
00172       static int_type
00173       eof() 
00174       {
00175     int_type __r = { -1 };
00176     return __r;
00177       }
00178 
00179       static int_type
00180       not_eof(const int_type& __c)
00181       { return eq_int_type(__c, eof()) ? int_type() : __c; }
00182     };
00183 }
00184 
00185 #endif

Generated on Tue Feb 2 16:56:21 2010 for GNU C++ STL by  doxygen 1.4.7