|
| 1 | +#include "ConstStrBlobPtr.h" |
| 2 | +#include "StrBlob.h" |
| 3 | + |
| 4 | +ConstStrBlobPtr::ConstStrBlobPtr() : wptr(), curr(0) {} |
| 5 | +ConstStrBlobPtr::ConstStrBlobPtr(const StrBlob &sb, size_type pos) |
| 6 | + : wptr(sb.data), curr(pos) {} |
| 7 | + |
| 8 | +std::shared_ptr<std::vector<std::string>> |
| 9 | +ConstStrBlobPtr::check(size_type pos, const std::string &msg) const { |
| 10 | + auto ret = wptr.lock(); |
| 11 | + if (!ret) |
| 12 | + throw std::runtime_error("unbound ConstStrBlobPtr"); |
| 13 | + if (pos >= ret->size()) |
| 14 | + throw std::out_of_range(msg); |
| 15 | + return ret; |
| 16 | +} |
| 17 | + |
| 18 | +const std::string &ConstStrBlobPtr::deref() const { |
| 19 | + auto sp = check(curr, "deference past end of ConstStrBlobPtr"); |
| 20 | + return (*sp)[curr]; |
| 21 | +} |
| 22 | + |
| 23 | +//ConstStrBlobPtr &ConstStrBlobPtr::inc() { |
| 24 | +// check(curr, "increment past end of ConstStrBlobPtr"); |
| 25 | +// ++curr; |
| 26 | +// return *this; |
| 27 | +//} |
| 28 | + |
| 29 | +ConstStrBlobPtr &ConstStrBlobPtr::operator++() { |
| 30 | + check(curr, "increment past end of ConstStrBlobPtr"); |
| 31 | + ++curr; |
| 32 | + return *this; |
| 33 | +} |
| 34 | + |
| 35 | +ConstStrBlobPtr ConstStrBlobPtr::operator++(int) { |
| 36 | + auto ret = *this; |
| 37 | + ++*this; |
| 38 | + return ret; |
| 39 | +} |
| 40 | + |
| 41 | +ConstStrBlobPtr &ConstStrBlobPtr::operator--() { |
| 42 | + --curr; |
| 43 | + check(curr, "decrement past begin of ConstStrBlobPtr"); |
| 44 | + return *this; |
| 45 | +} |
| 46 | + |
| 47 | +ConstStrBlobPtr ConstStrBlobPtr::operator--(int) { |
| 48 | + auto ret = *this; |
| 49 | + --*this; |
| 50 | + return ret; |
| 51 | +} |
| 52 | + |
| 53 | +bool operator==(const ConstStrBlobPtr &lhs, const ConstStrBlobPtr &rhs) { |
| 54 | + // compare identity |
| 55 | + return lhs.wptr.lock() == rhs.wptr.lock() && lhs.curr == rhs.curr; |
| 56 | +} |
| 57 | + |
| 58 | +bool operator!=(const ConstStrBlobPtr &lhs, const ConstStrBlobPtr &rhs) { |
| 59 | + return !(lhs == rhs); |
| 60 | +} |
| 61 | + |
| 62 | +bool operator<(const ConstStrBlobPtr &lhs, const ConstStrBlobPtr &rhs) { |
| 63 | + return lhs.wptr.lock() == rhs.wptr.lock() && lhs.curr < rhs.curr; |
| 64 | +} |
| 65 | + |
| 66 | +bool operator>(const ConstStrBlobPtr &lhs, const ConstStrBlobPtr &rhs) { |
| 67 | + return rhs < lhs; |
| 68 | +} |
| 69 | + |
| 70 | +bool operator<=(const ConstStrBlobPtr &lhs, const ConstStrBlobPtr &rhs) { |
| 71 | + return !(lhs > rhs); |
| 72 | +} |
| 73 | + |
| 74 | +bool operator>=(const ConstStrBlobPtr &lhs, const ConstStrBlobPtr &rhs) { |
| 75 | + return !(lhs < rhs); |
| 76 | +} |
0 commit comments