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