libstdc++
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, 2007, 2009 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /** @file ext/pod_char_traits.h
00026  *  This file is a GNU extension to the Standard C++ Library.
00027  */
00028 
00029 // Gabriel Dos Reis <gdr@integrable-solutions.net>
00030 // Benjamin Kosnik <bkoz@redhat.com>
00031 
00032 #ifndef _POD_CHAR_TRAITS_H
00033 #define _POD_CHAR_TRAITS_H 1
00034 
00035 #include <string>
00036 
00037 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
00038 {
00039 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00040 
00041   // POD character abstraction.
00042   // NB: The char_type parameter is a subset of int_type, as to allow
00043   // int_type to properly hold the full range of char_type values as
00044   // well as EOF.
00045   /// @brief A POD class that serves as a character abstraction class.
00046   template<typename V, typename I, typename S = std::mbstate_t>
00047     struct character
00048     {
00049       typedef V             value_type;
00050       typedef I             int_type;
00051       typedef S             state_type;
00052       typedef character<V, I, S>    char_type;
00053 
00054       value_type    value;
00055 
00056       template<typename V2>
00057         static char_type
00058         from(const V2& v)
00059         {
00060       char_type ret = { static_cast<value_type>(v) };
00061       return ret;
00062     }
00063 
00064       template<typename V2>
00065         static V2
00066         to(const char_type& c)
00067         {
00068       V2 ret = { static_cast<V2>(c.value) };
00069       return ret;
00070     }
00071 
00072     };
00073 
00074   template<typename V, typename I, typename S>
00075     inline bool
00076     operator==(const character<V, I, S>& lhs, const character<V, I, S>& rhs)
00077     { return lhs.value == rhs.value; }
00078 
00079   template<typename V, typename I, typename S>
00080     inline bool
00081     operator<(const character<V, I, S>& lhs, const character<V, I, S>& rhs)
00082     { return lhs.value < rhs.value; }
00083 
00084 _GLIBCXX_END_NAMESPACE_VERSION
00085 } // namespace
00086 
00087 namespace std _GLIBCXX_VISIBILITY(default)
00088 {
00089 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00090 
00091   /// char_traits<__gnu_cxx::character> specialization.
00092   template<typename V, typename I, typename S>
00093     struct char_traits<__gnu_cxx::character<V, I, S> >
00094     {
00095       typedef __gnu_cxx::character<V, I, S> char_type;
00096       typedef typename char_type::int_type  int_type;
00097       typedef typename char_type::state_type    state_type;
00098       typedef fpos<state_type>          pos_type;
00099       typedef streamoff             off_type;
00100 
00101       static void
00102       assign(char_type& __c1, const char_type& __c2)
00103       { __c1 = __c2; }
00104 
00105       static bool
00106       eq(const char_type& __c1, const char_type& __c2)
00107       { return __c1 == __c2; }
00108 
00109       static bool
00110       lt(const char_type& __c1, const char_type& __c2)
00111       { return __c1 < __c2; }
00112 
00113       static int
00114       compare(const char_type* __s1, const char_type* __s2, size_t __n)
00115       {
00116     for (size_t __i = 0; __i < __n; ++__i)
00117       if (!eq(__s1[__i], __s2[__i]))
00118         return lt(__s1[__i], __s2[__i]) ? -1 : 1;
00119     return 0;
00120       }
00121 
00122       static size_t
00123       length(const char_type* __s)
00124       {
00125     const char_type* __p = __s;
00126     while (__p->value)
00127       ++__p;
00128     return (__p - __s);
00129       }
00130 
00131       static const char_type*
00132       find(const char_type* __s, size_t __n, const char_type& __a)
00133       {
00134     for (const char_type* __p = __s; size_t(__p - __s) < __n; ++__p)
00135       if (*__p == __a)
00136         return __p;
00137     return 0;
00138       }
00139 
00140       static char_type*
00141       move(char_type* __s1, const char_type* __s2, size_t __n)
00142       { 
00143     return static_cast<char_type*>
00144       (__builtin_memmove(__s1, __s2, __n * sizeof(char_type)));
00145       }
00146 
00147       static char_type*
00148       copy(char_type* __s1, const char_type* __s2, size_t __n)
00149       {
00150     std::copy(__s2, __s2 + __n, __s1);
00151     return __s1;
00152       }
00153 
00154       static char_type*
00155       assign(char_type* __s, size_t __n, char_type __a)
00156       {
00157     std::fill_n(__s, __n, __a);
00158         return __s;
00159       }
00160 
00161       static char_type
00162       to_char_type(const int_type& __i)
00163       { return char_type::template from(__i); }
00164 
00165       static int_type
00166       to_int_type(const char_type& __c)
00167       { return char_type::template to<int_type>(__c); }
00168 
00169       static bool
00170       eq_int_type(const int_type& __c1, const int_type& __c2)
00171       { return __c1 == __c2; }
00172 
00173       static int_type
00174       eof() 
00175       {
00176     int_type __r = { -1 };
00177     return __r;
00178       }
00179 
00180       static int_type
00181       not_eof(const int_type& __c)
00182       { return eq_int_type(__c, eof()) ? int_type() : __c; }
00183     };
00184 
00185 _GLIBCXX_END_NAMESPACE_VERSION
00186 } // namespace
00187 
00188 #endif