Import of virgin gcc 4.0.0 distribution.
[dragonfly.git] / contrib / gcc-4.0 / libstdc++-v3 / include / ext / pod_char_traits.h
1 // POD character, std::char_traits specialization -*- C++ -*-
2
3 // Copyright (C) 2002, 2003, 2004 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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   /// @brief A POD class that serves as a character abstraction class.
45   template<typename V, typename I, typename S = mbstate_t>
46     struct character
47     {
48       typedef V         value_type;
49       typedef I         int_type;
50       typedef S         state_type;
51       value_type        value;
52     };
53
54   template<typename V, typename I>
55     inline bool
56     operator==(const character<V, I>& lhs, const character<V, I>& rhs)
57     { return lhs.value == rhs.value; }
58
59   template<typename V, typename I>
60     inline bool
61     operator<(const character<V, I>& lhs, const character<V, I>& rhs)
62     { return lhs.value < rhs.value; }
63 } // namespace __gnu_cxx
64
65 namespace std
66 {
67   /// char_traits<__gnu_cxx::character> specialization.
68   template<typename V, typename I, typename S>
69     struct char_traits<__gnu_cxx::character<V, I, S> >
70     {
71       typedef __gnu_cxx::character<V, I, S>     char_type;
72
73       // NB: This type should be bigger than char_type, so as to
74       // properly hold EOF values in addition to the full range of
75       // char_type values.
76       // Also, assumes
77       // int_type(value_type) is valid.
78       // int_type(-1) is possible.
79       typedef typename char_type::int_type      int_type;
80       typedef typename char_type::state_type    state_type;
81       typedef fpos<state_type>                  pos_type;
82       typedef streamoff                         off_type;
83
84       static void
85       assign(char_type& __c1, const char_type& __c2)
86       { __c1 = __c2; }
87
88       static bool
89       eq(const char_type& __c1, const char_type& __c2)
90       { return __c1 == __c2; }
91
92       static bool
93       lt(const char_type& __c1, const char_type& __c2)
94       { return __c1 < __c2; }
95
96       static int
97       compare(const char_type* __s1, const char_type* __s2, size_t __n)
98       {
99         for (size_t __i = 0; __i < __n; ++__i)
100           if (!eq(__s1[__i], __s2[__i]))
101             return lt(__s1[__i], __s2[__i]) ? -1 : 1;
102         return 0;
103       }
104
105       static size_t
106       length(const char_type* __s)
107       {
108         const char_type* __p = __s;
109         while (__p->value)
110           ++__p;
111         return (__p - __s);
112       }
113
114       static const char_type*
115       find(const char_type* __s, size_t __n, const char_type& __a)
116       {
117         for (const char_type* __p = __s; size_t(__p - __s) < __n; ++__p)
118           if (*__p == __a)
119             return __p;
120         return 0;
121       }
122
123       static char_type*
124       move(char_type* __s1, const char_type* __s2, size_t __n)
125       { return (char_type*) memmove(__s1, __s2, __n * sizeof(char_type)); }
126
127       static char_type*
128       copy(char_type* __s1, const char_type* __s2, size_t __n)
129       { return (char_type*) memcpy(__s1, __s2, __n * sizeof(char_type)); }
130
131       static char_type*
132       assign(char_type* __s, size_t __n, char_type __a)
133       {
134         for (char_type* __p = __s; __p < __s + __n; ++__p)
135           assign(*__p, __a);
136         return __s;
137       }
138
139       static char_type
140       to_char_type(const int_type& __c)
141       {
142         char_type __r = { __c };
143         return __r;
144       }
145
146       static int_type
147       to_int_type(const char_type& __c)
148       { return int_type(__c.value); }
149
150       static bool
151       eq_int_type(const int_type& __c1, const int_type& __c2)
152       { return __c1 == __c2; }
153
154       static int_type
155       eof() { return static_cast<int_type>(-1); }
156
157       static int_type
158       not_eof(const int_type& __c)
159       { return eq_int_type(__c, eof()) ? int_type(0) : __c; }
160     };
161 }
162
163 #endif