-
Notifications
You must be signed in to change notification settings - Fork 246
/
StrBlob.cpp
95 lines (75 loc) · 2.24 KB
/
StrBlob.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "StrBlob.h"
#include "StrBlobPtr.h"
#include "ConstStrBlobPtr.h"
StrBlob::StrBlob() : data(std::make_shared<std::vector<std::string>>()) {}
StrBlob::StrBlob(std::initializer_list<std::string> il)
: data(std::make_shared<std::vector<std::string>>(il)) {}
StrBlob::StrBlob(const StrBlob &sb)
: data(std::make_shared<std::vector<std::string>>(*sb.data)) {}
StrBlob &StrBlob::operator=(const StrBlob &sb) {
data = std::make_shared<std::vector<std::string>>(*sb.data);
return *this;
}
void StrBlob::check(size_type pos, const std::string &msg) const {
if (pos >= data->size())
throw std::out_of_range(msg);
}
void StrBlob::pop_back() {
check(0, "pop_back on empty StrBlob");
data->pop_back();
}
std::string &StrBlob::front() {
check(0, "front on empty StrBlob");
return data->front();
}
const std::string &StrBlob::front() const {
check(0, "front on empty StrBlob");
return data->front();
}
std::string &StrBlob::back() {
check(0, "back on empty StrBlob");
return data->back();
}
const std::string &StrBlob::back() const {
check(0, "back on empty StrBlob");
return data->back();
}
StrBlobPtr StrBlob::begin() {
return StrBlobPtr(*this);
}
StrBlobPtr StrBlob::end() {
return StrBlobPtr(*this, data->size());
}
ConstStrBlobPtr StrBlob::cbegin() const {
return ConstStrBlobPtr(*this);
}
ConstStrBlobPtr StrBlob::cend() const {
return ConstStrBlobPtr(*this, data->size());
}
std::string &StrBlob::operator[](size_type n) {
check(n, "subscript nonexist element");
return (*data)[n];
}
const std::string &StrBlob::operator[](size_type n) const {
check(n, "subscript nonexist element");
return (*data)[n];
}
bool operator==(const StrBlob &lhs, const StrBlob &rhs) {
//return lhs.data == rhs.data; // compare identity(address)
return *lhs.data == *rhs.data; // compare value
}
bool operator!=(const StrBlob &lhs, const StrBlob &rhs) {
return !(lhs == rhs);
}
bool operator<(const StrBlob &lhs, const StrBlob &rhs) {
return *lhs.data < *rhs.data; // compare value
}
bool operator>(const StrBlob &lhs, const StrBlob &rhs) {
return rhs < lhs;
}
bool operator<=(const StrBlob &lhs, const StrBlob &rhs) {
return !(lhs > rhs);
}
bool operator>=(const StrBlob &lhs, const StrBlob &rhs) {
return !(lhs < rhs);
}