Merge from vendor branch GCC:
[dragonfly.git] / contrib / gcc-4.1 / libstdc++-v3 / include / ext / pod_char_traits.h
1 // POD character, std::char_traits specialization -*- C++ -*-
2
3 // Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library.  This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING.  If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 // USA.
20
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction.  Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License.  This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29
30 /** @file ext/pod_char_traits.h
31  *  This file is a GNU extension to the Standard C++ Library.
32  */
33
34 // Gabriel Dos Reis <gdr@integrable-solutions.net>
35 // Benjamin Kosnik <bkoz@redhat.com>
36
37 #ifndef _POD_CHAR_TRAITS_H
38 #define _POD_CHAR_TRAITS_H 1
39
40 #include <string>
41
42 namespace __gnu_cxx
43 {
44   // POD character abstraction.
45   // NB: The char_type parameter is a subset of int_type, as to allow
46   // int_type to properly hold the full range of char_type values as
47   // well as EOF.
48   /// @brief A POD class that serves as a character abstraction class.
49   template<typename V, typename I, typename S = mbstate_t>
50     struct character
51     {
52       typedef V                         value_type;
53       typedef I                         int_type;
54       typedef S                         state_type;
55       typedef character<V, I, S>        char_type;
56
57       value_type        value;
58
59       template<typename V2>
60         static char_type
61         from(const V2& v)
62         {
63           char_type ret = { static_cast<value_type>(v) };
64           return ret;
65         }
66
67       template<typename V2>
68         static V2
69         to(const char_type& c)
70         {
71           V2 ret = { static_cast<V2>(c.value) };
72           return ret;
73         }
74
75     };
76
77   template<typename V, typename I, typename S>
78     inline bool
79     operator==(const character<V, I, S>& lhs, const character<V, I, S>& rhs)
80     { return lhs.value == rhs.value; }
81
82   template<typename V, typename I, typename S>
83     inline bool
84     operator<(const character<V, I, S>& lhs, const character<V, I, S>& rhs)
85     { return lhs.value < rhs.value; }
86 } // namespace __gnu_cxx
87
88 namespace std
89 {
90   /// char_traits<__gnu_cxx::character> specialization.
91   template<typename V, typename I, typename S>
92     struct char_traits<__gnu_cxx::character<V, I, S> >
93     {
94       typedef __gnu_cxx::character<V, I, S>     char_type;
95       typedef typename char_type::int_type      int_type;
96       typedef typename char_type::state_type    state_type;
97       typedef fpos<state_type>                  pos_type;
98       typedef streamoff                         off_type;
99
100       static void
101       assign(char_type& __c1, const char_type& __c2)
102       { __c1 = __c2; }
103
104       static bool
105       eq(const char_type& __c1, const char_type& __c2)
106       { return __c1 == __c2; }
107
108       static bool
109       lt(const char_type& __c1, const char_type& __c2)
110       { return __c1 < __c2; }
111
112       static int
113       compare(const char_type* __s1, const char_type* __s2, size_t __n)
114       {
115         for (size_t __i = 0; __i < __n; ++__i)
116           if (!eq(__s1[__i], __s2[__i]))
117             return lt(__s1[__i], __s2[__i]) ? -1 : 1;
118         return 0;
119       }
120
121       static size_t
122       length(const char_type* __s)
123       {
124         const char_type* __p = __s;
125         while (__p->value)
126           ++__p;
127         return (__p - __s);
128       }
129
130       static const char_type*
131       find(const char_type* __s, size_t __n, const char_type& __a)
132       {
133         for (const char_type* __p = __s; size_t(__p - __s) < __n; ++__p)
134           if (*__p == __a)
135             return __p;
136         return 0;
137       }
138
139       static char_type*
140       move(char_type* __s1, const char_type* __s2, size_t __n)
141       { 
142         return static_cast<char_type*>(std::memmove(__s1, __s2, 
143                                                     __n * sizeof(char_type))); 
144       }
145
146       static char_type*
147       copy(char_type* __s1, const char_type* __s2, size_t __n)
148       {
149         std::copy(__s2, __s2 + __n, __s1);
150         return __s1;
151       }
152
153       static char_type*
154       assign(char_type* __s, size_t __n, char_type __a)
155       {
156         std::fill_n(__s, __n, __a);
157         return __s;
158       }
159
160       static char_type
161       to_char_type(const int_type& __i)
162       { return char_type::template from(__i); }
163
164       static int_type
165       to_int_type(const char_type& __c)
166       { return char_type::template to<int_type>(__c); }
167
168       static bool
169       eq_int_type(const int_type& __c1, const int_type& __c2)
170       { return __c1 == __c2; }
171
172       static int_type
173       eof() 
174       {
175         int_type __r = { -1 };
176         return __r;
177       }
178
179       static int_type
180       not_eof(const int_type& __c)
181       { return eq_int_type(__c, eof()) ? int_type() : __c; }
182     };
183 }
184
185 #endif