]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/ext/pod_char_traits.h
pod_char_traits.cc: New.
[thirdparty/gcc.git] / libstdc++-v3 / include / ext / pod_char_traits.h
1 // POD character, std::char_traits specialization -*- C++ -*-
2
3 // Copyright (C) 2002, 2003 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 // Gabriel Dos Reis <gdr@integrable-solutions.net>
31 // Benjamin Kosnik <bkoz@redhat.com>
32
33 #ifndef _POD_CHAR_TRAITS_H
34 #define _POD_CHAR_TRAITS_H 1
35
36 #include <string>
37
38 namespace __gnu_cxx
39 {
40 template<typename V, typename I>
41 struct character
42 {
43 typedef V value_type;
44 typedef I int_type;
45 value_type value;
46 };
47
48 template<typename V, typename I>
49 inline bool
50 operator==(const character<V, I>& lhs, const character<V, I>& rhs)
51 { return lhs.value == rhs.value; }
52
53 template<typename V, typename I>
54 inline bool
55 operator<(const character<V, I>& lhs, const character<V, I>& rhs)
56 { return lhs.value < rhs.value; }
57 } // namespace __gnu_cxx
58
59 namespace std
60 {
61 // Provide std::char_traits specialization.
62 template<typename V, typename I>
63 struct char_traits<__gnu_cxx::character<V, I> >
64 {
65 typedef __gnu_cxx::character<V, I> char_type;
66
67 // NB: This type should be bigger than char_type, so as to
68 // properly hold EOF values in addition to the full range of
69 // char_type values.
70 typedef typename char_type::int_type int_type;
71
72 typedef streampos pos_type;
73 typedef streamoff off_type;
74 typedef mbstate_t state_type;
75
76 static void
77 assign(char_type& __c1, const char_type& __c2)
78 { __c1 = __c2; }
79
80 static bool
81 eq(const char_type& __c1, const char_type& __c2)
82 { return __c1 == __c2; }
83
84 static bool
85 lt(const char_type& __c1, const char_type& __c2)
86 { return __c1 < __c2; }
87
88 static int
89 compare(const char_type* __s1, const char_type* __s2, size_t __n)
90 {
91 for (size_t __i = 0; __i < __n; ++__i)
92 if (!eq(__s1[__i], __s2[__i]))
93 return lt(__s1[__i], __s2[__i]) ? -1 : 1;
94 return 0;
95 }
96
97 static size_t
98 length(const char_type* __s)
99 {
100 const char_type* __p = __s;
101 while (*__p)
102 ++__p;
103 return (__p - __s);
104 }
105
106 static const char_type*
107 find(const char_type* __s, size_t __n, const char_type& __a)
108 {
109 for (const char_type* __p = __s; size_t(__p - __s) < __n; ++__p)
110 if (*__p == __a)
111 return __p;
112 return 0;
113 }
114
115 static char_type*
116 move(char_type* __s1, const char_type* __s2, size_t __n)
117 { return (char_type*) memmove(__s1, __s2, __n * sizeof(char_type)); }
118
119 static char_type*
120 copy(char_type* __s1, const char_type* __s2, size_t __n)
121 { return (char_type*) memcpy(__s1, __s2, __n * sizeof(char_type)); }
122
123 static char_type*
124 assign(char_type* __s, size_t __n, char_type __a)
125 {
126 for (char_type* __p = __s; __p < __s + __n; ++__p)
127 assign(*__p, __a);
128 return __s;
129 }
130
131 static char_type
132 to_char_type(const int_type& __c)
133 {
134 char_type __r = { __c };
135 return __r;
136 }
137
138 static int_type
139 to_int_type(const char_type& __c)
140 { return int_type(__c.value); }
141
142 static bool
143 eq_int_type(const int_type& __c1, const int_type& __c2)
144 { return __c1 == __c2; }
145
146 static int_type
147 eof() { return static_cast<int_type>(-1); }
148
149 static int_type
150 not_eof(const int_type& __c)
151 { return eq_int_type(__c, eof()) ? int_type(0) : __c; }
152 };
153 }
154
155 #endif