OSDN Git Service

clover/util: Define equality operators for a couple of compat classes.
[android-x86/external-mesa.git] / src / gallium / state_trackers / clover / util / compat.hpp
1 //
2 // Copyright 2012 Francisco Jerez
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a
5 // copy of this software and associated documentation files (the "Software"),
6 // to deal in the Software without restriction, including without limitation
7 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 // and/or sell copies of the Software, and to permit persons to whom the
9 // Software is furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 // OTHER DEALINGS IN THE SOFTWARE.
21 //
22
23 #ifndef CLOVER_UTIL_COMPAT_HPP
24 #define CLOVER_UTIL_COMPAT_HPP
25
26 #include <new>
27 #include <cstring>
28 #include <cstdlib>
29 #include <string>
30 #include <stdint.h>
31
32 namespace clover {
33    namespace compat {
34       // XXX - For cases where we can't rely on STL...  I.e. the
35       //       interface between code compiled as C++98 and C++11
36       //       source.  Get rid of this as soon as everything can be
37       //       compiled as C++11.
38
39       namespace detail {
40          template<typename R, typename S>
41          bool
42          ranges_equal(const R &a, const S &b) {
43             if (a.size() != b.size())
44                return false;
45
46             for (size_t i = 0; i < a.size(); ++i)
47                if (a[i] != b[i])
48                   return false;
49
50             return true;
51          }
52       }
53
54       template<typename T>
55       class vector {
56       protected:
57          static T *
58          alloc(int n, const T *q, int m) {
59             T *p = reinterpret_cast<T *>(std::malloc(n * sizeof(T)));
60
61             for (int i = 0; i < m; ++i)
62                new(&p[i]) T(q[i]);
63
64             return p;
65          }
66
67          static void
68          free(int n, T *p) {
69             for (int i = 0; i < n; ++i)
70                p[i].~T();
71
72             std::free(p);
73          }
74
75       public:
76          typedef T *iterator;
77          typedef const T *const_iterator;
78          typedef T value_type;
79          typedef T &reference;
80          typedef const T &const_reference;
81          typedef std::ptrdiff_t difference_type;
82          typedef std::size_t size_type;
83
84          vector() : p(NULL), _size(0), _capacity(0) {
85          }
86
87          vector(const vector &v) :
88             p(alloc(v._size, v.p, v._size)),
89             _size(v._size), _capacity(v._size) {
90          }
91
92          vector(const_iterator p, size_type n) :
93             p(alloc(n, p, n)), _size(n), _capacity(n) {
94          }
95
96          template<typename C>
97          vector(const C &v) :
98             p(alloc(v.size(), NULL, 0)), _size(0),
99             _capacity(v.size()) {
100             for (typename C::const_iterator it = v.begin(); it != v.end(); ++it)
101                new(&p[_size++]) T(*it);
102          }
103
104          ~vector() {
105             free(_size, p);
106          }
107
108          vector &
109          operator=(const vector &v) {
110             free(_size, p);
111
112             p = alloc(v._size, v.p, v._size);
113             _size = v._size;
114             _capacity = v._size;
115
116             return *this;
117          }
118
119          bool
120          operator==(const vector &v) const {
121             return detail::ranges_equal(*this, v);
122          }
123
124          void
125          reserve(size_type n) {
126             if (_capacity < n) {
127                T *q = alloc(n, p, _size);
128                free(_size, p);
129
130                p = q;
131                _capacity = n;
132             }
133          }
134
135          void
136          resize(size_type n, T x = T()) {
137             if (n <= _size) {
138                for (size_type i = n; i < _size; ++i)
139                   p[i].~T();
140
141             } else {
142                reserve(n);
143
144                for (size_type i = _size; i < n; ++i)
145                   new(&p[i]) T(x);
146             }
147
148             _size = n;
149          }
150
151          void
152          push_back(const T &x) {
153             reserve(_size + 1);
154             new(&p[_size]) T(x);
155             ++_size;
156          }
157
158          size_type
159          size() const {
160             return _size;
161          }
162
163          size_type
164          capacity() const {
165             return _capacity;
166          }
167
168          iterator
169          begin() {
170             return p;
171          }
172
173          const_iterator
174          begin() const {
175             return p;
176          }
177
178          iterator
179          end() {
180             return p + _size;
181          }
182
183          const_iterator
184          end() const {
185             return p + _size;
186          }
187
188          reference
189          operator[](size_type i) {
190             return p[i];
191          }
192
193          const_reference
194          operator[](size_type i) const {
195             return p[i];
196          }
197
198       private:
199          iterator p;
200          size_type _size;
201          size_type _capacity;
202       };
203
204       template<typename T>
205       class vector_ref {
206       public:
207          typedef T *iterator;
208          typedef const T *const_iterator;
209          typedef T value_type;
210          typedef T &reference;
211          typedef const T &const_reference;
212          typedef std::ptrdiff_t difference_type;
213          typedef std::size_t size_type;
214
215          vector_ref(iterator p, size_type n) : p(p), n(n) {
216          }
217
218          template<typename C>
219          vector_ref(C &v) : p(&*v.begin()), n(v.size()) {
220          }
221
222          bool
223          operator==(const vector_ref &v) const {
224             return detail::ranges_equal(*this, v);
225          }
226
227          size_type
228          size() const {
229             return n;
230          }
231
232          iterator
233          begin() {
234             return p;
235          }
236
237          const_iterator
238          begin() const {
239             return p;
240          }
241
242          iterator
243          end() {
244             return p + n;
245          }
246
247          const_iterator
248          end() const {
249             return p + n;
250          }
251
252          reference
253          operator[](int i) {
254             return p[i];
255          }
256
257          const_reference
258          operator[](int i) const {
259             return p[i];
260          }
261
262       private:
263          iterator p;
264          size_type n;
265       };
266
267       class istream {
268       public:
269          typedef vector_ref<const unsigned char> buffer_t;
270
271          class error {
272          public:
273             virtual ~error() {}
274          };
275
276          istream(const buffer_t &buf) : buf(buf), offset(0) {}
277
278          void
279          read(char *p, size_t n) {
280             if (offset + n > buf.size())
281                throw error();
282
283             std::memcpy(p, buf.begin() + offset, n);
284             offset += n;
285          }
286
287       private:
288          const buffer_t &buf;
289          size_t offset;
290       };
291
292       class ostream {
293       public:
294          typedef vector<unsigned char> buffer_t;
295
296          ostream(buffer_t &buf) : buf(buf), offset(buf.size()) {}
297
298          void
299          write(const char *p, size_t n) {
300             buf.resize(offset + n);
301             std::memcpy(buf.begin() + offset, p, n);
302             offset += n;
303          }
304
305       private:
306          buffer_t &buf;
307          size_t offset;
308       };
309
310       class string {
311       public:
312          typedef char *iterator;
313          typedef const char *const_iterator;
314          typedef char value_type;
315          typedef char &reference;
316          typedef const char &const_reference;
317          typedef std::ptrdiff_t difference_type;
318          typedef std::size_t size_type;
319
320          string() : v() {
321          }
322
323          string(const char *p) : v(p, std::strlen(p)) {
324          }
325
326          template<typename C>
327          string(const C &v) : v(v) {
328          }
329
330          operator std::string() const {
331             return std::string(v.begin(), v.end());
332          }
333
334          bool
335          operator==(const string &s) const {
336             return this->v == s.v;
337          }
338
339          void
340          reserve(size_type n) {
341             v.reserve(n);
342          }
343
344          void
345          resize(size_type n, char x = char()) {
346             v.resize(n, x);
347          }
348
349          void
350          push_back(char x) {
351             v.push_back(x);
352          }
353
354          size_type
355          size() const {
356             return v.size();
357          }
358
359          size_type
360          capacity() const {
361             return v.capacity();
362          }
363
364          iterator
365          begin() {
366             return v.begin();
367          }
368
369          const_iterator
370          begin() const {
371             return v.begin();
372          }
373
374          iterator
375          end() {
376             return v.end();
377          }
378
379          const_iterator
380          end() const {
381             return v.end();
382          }
383
384          reference
385          operator[](size_type i) {
386             return v[i];
387          }
388
389          const_reference
390          operator[](size_type i) const {
391             return v[i];
392          }
393
394          const char *
395          c_str() const {
396             v.reserve(size() + 1);
397             *v.end() = 0;
398             return v.begin();
399          }
400
401          const char *
402          find(const string &s) const {
403             for (size_t i = 0; i + s.size() < size(); ++i) {
404                if (!std::memcmp(begin() + i, s.begin(), s.size()))
405                   return begin() + i;
406             }
407
408             return end();
409          }
410
411       private:
412          mutable vector<char> v;
413       };
414
415       class exception {
416       public:
417          exception() {}
418          virtual ~exception();
419
420          virtual const char *what() const;
421       };
422
423       class runtime_error : public exception {
424       public:
425          runtime_error(const string &what) : _what(what) {}
426
427          virtual const char *what() const;
428
429       protected:
430          string _what;
431       };
432    }
433 }
434
435 #endif