2 // Copyright 2012 Francisco Jerez
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:
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
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.
23 #ifndef CLOVER_UTIL_COMPAT_HPP
24 #define CLOVER_UTIL_COMPAT_HPP
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
43 alloc(int n, const T *q, int m) {
44 T *p = reinterpret_cast<T *>(std::malloc(n * sizeof(T)));
46 for (int i = 0; i < m; ++i)
54 for (int i = 0; i < n; ++i)
62 typedef const T *const_iterator;
65 typedef const T &const_reference;
66 typedef std::ptrdiff_t difference_type;
67 typedef std::size_t size_type;
69 vector() : p(NULL), _size(0), _capacity(0) {
72 vector(const vector &v) :
73 p(alloc(v._size, v.p, v._size)),
74 _size(v._size), _capacity(v._size) {
77 vector(const_iterator p, size_type n) :
78 p(alloc(n, p, n)), _size(n), _capacity(n) {
83 p(alloc(v.size(), &*v.begin(), v.size())),
84 _size(v.size()) , _capacity(v.size()) {
92 operator=(const vector &v) {
95 p = alloc(v._size, v.p, v._size);
103 reserve(size_type n) {
105 T *q = alloc(n, p, _size);
114 resize(size_type n, T x = T()) {
116 for (size_type i = n; i < _size; ++i)
122 for (size_type i = _size; i < n; ++i)
130 push_back(const T &x) {
167 operator[](size_type i) {
172 operator[](size_type i) const {
186 typedef const T *const_iterator;
187 typedef T value_type;
188 typedef T &reference;
189 typedef const T &const_reference;
190 typedef std::ptrdiff_t difference_type;
191 typedef std::size_t size_type;
193 vector_ref(iterator p, size_type n) : p(p), n(n) {
197 vector_ref(C &v) : p(&*v.begin()), n(v.size()) {
231 operator[](int i) const {
242 typedef vector_ref<const unsigned char> buffer_t;
249 istream(const buffer_t &buf) : buf(buf), offset(0) {}
252 read(char *p, size_t n) {
253 if (offset + n > buf.size())
256 std::memcpy(p, buf.begin() + offset, n);
267 typedef vector<unsigned char> buffer_t;
269 ostream(buffer_t &buf) : buf(buf), offset(buf.size()) {}
272 write(const char *p, size_t n) {
273 buf.resize(offset + n);
274 std::memcpy(buf.begin() + offset, p, n);
285 typedef char *iterator;
286 typedef const char *const_iterator;
287 typedef char value_type;
288 typedef char &reference;
289 typedef const char &const_reference;
290 typedef std::ptrdiff_t difference_type;
291 typedef std::size_t size_type;
296 string(const char *p) : v(p, std::strlen(p)) {
300 string(const C &v) : v(v) {
303 operator std::string() const {
304 return std::string(v.begin(), v.end());
308 reserve(size_type n) {
313 resize(size_type n, char x = char()) {
353 operator[](size_type i) {
358 operator[](size_type i) const {
364 v.reserve(size() + 1);
370 find(const string &s) const {
371 for (size_t i = 0; i + s.size() < size(); ++i) {
372 if (!std::memcmp(begin() + i, s.begin(), s.size()))
380 mutable vector<char> v;
385 operator==(const vector_ref<T> &a, const vector_ref<T> &b) {
386 if (a.size() != b.size())
389 for (size_t i = 0; i < a.size(); ++i)
399 virtual ~exception();
401 virtual const char *what() const;
404 class runtime_error : public exception {
406 runtime_error(const string &what) : _what(what) {}
408 virtual const char *what() const;