texdepends/memory_string.hpp

87 lines
2.1 KiB
C++

/*
* memoy_string.hpp
*
* Created on: 08.10.2017
* Author: julian
*/
#ifndef MEMORY_STRING_HPP_
#define MEMORY_STRING_HPP_
#include <iterator>
class MemoryString {
template <typename T>
struct _iterator : public std::iterator<std::forward_iterator_tag, char> {
T* pos;
_iterator operator++(int){ _iterator temp(*this); pos++; return temp; }
_iterator operator--(int){ _iterator temp(*this); pos--; return temp; }
_iterator &operator++() { pos++; return *this; }
_iterator &operator--() { pos--; return *this; }
_iterator &operator+=(int i) { pos += i; return *this; }
_iterator &operator-=(int i) { pos -= i; return *this; }
_iterator operator+(int i) { _iterator temp(*this); temp += i; return temp; }
_iterator operator-(int i) { _iterator temp(*this); temp -= i; return temp; }
T operator*() { return *pos; }
T operator*() const { return *pos; }
T * operator&() { return pos; }
T * operator->() { return pos; }
template <typename U>
_iterator(const _iterator<U> &other) { *this = other; }
template <typename U>
_iterator &operator=(const _iterator<U> &other) const {
if (this != &other) pos = other.pos;
return *this;
}
template <typename U>
bool operator==(const _iterator<U> &other) const {
return pos == other.pos;
}
template <typename U>
bool operator!=(const _iterator<U> &other) const {
return pos != other.pos;
}
private:
_iterator(T *pos) : pos(pos) {}
friend MemoryString;
};
public:
typedef size_t size_type;
typedef char value_type;
typedef _iterator<const value_type> const_iterator;
typedef _iterator<value_type> iterator;
MemoryString(value_type * base, size_type size): _ptr(base), _size(size) {}
~MemoryString() {}
iterator begin() { return iterator(_ptr); }
const_iterator begin() const { return const_iterator(_ptr); }
iterator end() { return iterator(_ptr + _size); }
const_iterator end() const { return const_iterator(_ptr + _size); }
size_type size() { return _size; }
protected:
value_type * _ptr;
size_type _size;
};
#endif /* MEMORY_STRING_HPP_ */