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, 2010, 2011
00004 // Free Software Foundation, Inc.
00005 //
00006 // This file is part of the GNU ISO C++ Library.  This library is free
00007 // software; you can redistribute it and/or modify it under the
00008 // terms of the GNU General Public License as published by the
00009 // Free Software Foundation; either version 3, or (at your option)
00010 // any later version.
00011 
00012 // This library is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 
00017 // Under Section 7 of GPL version 3, you are granted additional
00018 // permissions described in the GCC Runtime Library Exception, version
00019 // 3.1, as published by the Free Software Foundation.
00020 
00021 // You should have received a copy of the GNU General Public License and
00022 // a copy of the GCC Runtime Library Exception along with this program;
00023 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00024 // <http://www.gnu.org/licenses/>.
00025 
00026 /** @file ext/pod_char_traits.h
00027  *  This file is a GNU extension to the Standard C++ Library.
00028  */
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 #pragma GCC system_header
00037 
00038 #include <string>
00039 
00040 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
00041 {
00042 _GLIBCXX_BEGIN_NAMESPACE_VERSION
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.
00048   /// @brief A POD class that serves as a character abstraction class.
00049   template<typename V, typename I, typename S = std::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 
00087 _GLIBCXX_END_NAMESPACE_VERSION
00088 } // namespace
00089 
00090 namespace std _GLIBCXX_VISIBILITY(default)
00091 {
00092 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00093 
00094   /// char_traits<__gnu_cxx::character> specialization.
00095   template<typename V, typename I, typename S>
00096     struct char_traits<__gnu_cxx::character<V, I, S> >
00097     {
00098       typedef __gnu_cxx::character<V, I, S> char_type;
00099       typedef typename char_type::int_type  int_type;
00100       typedef typename char_type::state_type    state_type;
00101       typedef fpos<state_type>          pos_type;
00102       typedef streamoff             off_type;
00103 
00104       static void
00105       assign(char_type& __c1, const char_type& __c2)
00106       { __c1 = __c2; }
00107 
00108       static bool
00109       eq(const char_type& __c1, const char_type& __c2)
00110       { return __c1 == __c2; }
00111 
00112       static bool
00113       lt(const char_type& __c1, const char_type& __c2)
00114       { return __c1 < __c2; }
00115 
00116       static int
00117       compare(const char_type* __s1, const char_type* __s2, size_t __n)
00118       {
00119     for (size_t __i = 0; __i < __n; ++__i)
00120       if (!eq(__s1[__i], __s2[__i]))
00121         return lt(__s1[__i], __s2[__i]) ? -1 : 1;
00122     return 0;
00123       }
00124 
00125       static size_t
00126       length(const char_type* __s)
00127       {
00128     const char_type* __p = __s;
00129     while (__p->value)
00130       ++__p;
00131     return (__p - __s);
00132       }
00133 
00134       static const char_type*
00135       find(const char_type* __s, size_t __n, const char_type& __a)
00136       {
00137     for (const char_type* __p = __s; size_t(__p - __s) < __n; ++__p)
00138       if (*__p == __a)
00139         return __p;
00140     return 0;
00141       }
00142 
00143       static char_type*
00144       move(char_type* __s1, const char_type* __s2, size_t __n)
00145       { 
00146     return static_cast<char_type*>
00147       (__builtin_memmove(__s1, __s2, __n * sizeof(char_type)));
00148       }
00149 
00150       static char_type*
00151       copy(char_type* __s1, const char_type* __s2, size_t __n)
00152       {
00153     std::copy(__s2, __s2 + __n, __s1);
00154     return __s1;
00155       }
00156 
00157       static char_type*
00158       assign(char_type* __s, size_t __n, char_type __a)
00159       {
00160     std::fill_n(__s, __n, __a);
00161         return __s;
00162       }
00163 
00164       static char_type
00165       to_char_type(const int_type& __i)
00166       { return char_type::template from(__i); }
00167 
00168       static int_type
00169       to_int_type(const char_type& __c)
00170       { return char_type::template to<int_type>(__c); }
00171 
00172       static bool
00173       eq_int_type(const int_type& __c1, const int_type& __c2)
00174       { return __c1 == __c2; }
00175 
00176       static int_type
00177       eof() 
00178       {
00179     int_type __r = { -1 };
00180     return __r;
00181       }
00182 
00183       static int_type
00184       not_eof(const int_type& __c)
00185       { return eq_int_type(__c, eof()) ? int_type() : __c; }
00186     };
00187 
00188 _GLIBCXX_END_NAMESPACE_VERSION
00189 } // namespace
00190 
00191 #endif