-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathStrBlob.cpp
133 lines (111 loc) · 2.72 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include<memory>
#include<vector>
#include<string>
#include<stdexcept>
#include "StrBlob.h"
StrBlob::StrBlob() : data(std::make_shared<std::vector<std::string>>()) { }
StrBlob::StrBlob(std::initializer_list<std::string> li): data(std::make_shared<std::vector<std::string>>(li)) { }
void StrBlob::check(size_type i, const std::string & msg) const
{
if(i >= data->size())
throw std::out_of_range(msg);
}
void StrBlob::pop_back()
{
check(0, "pop_back on empty StrBlob");
data->pop_back();
}
const std::string & StrBlob::front() const
{
check(0, "front on empty StrBlob");
return data->front();
}
std::string & StrBlob::front()
{
check(0, "front on empty StrBlob");
return data->front();
}
const std::string & StrBlob::back() const
{
check(0, "back on empty StrBlob");
return data->back();
}
std::string & StrBlob::back()
{
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()
{
return ConstStrBlobPtr(*this);
}
ConstStrBlobPtr StrBlob::cend()
{
return ConstStrBlobPtr(*this, data->size());
}
std::shared_ptr<std::vector<std::string>> StrBlobPtr::check(std::size_t i, const std::string & msg) const
{
std::shared_ptr<std::vector<std::string>> ret = wptr.lock();
if(!ret)
throw std::runtime_error("unbound StrBlobPtr");
if(i >= ret->size())
throw std::out_of_range(msg);
return ret;
}
std::string & StrBlobPtr::deref() const
{
std::shared_ptr<std::vector<std::string>> p = check(curr, "dereference past end");
return (*p)[curr];
}
StrBlobPtr & StrBlobPtr::incr()
{
check(curr, "increment past end of StrBlobPtr");
++curr;
return *this;
}
StrBlobPtr & StrBlobPtr::add(StrBlob::size_type num)
{
check(curr + num, "increment past end of StrBlobPtr");
curr += num;
return *this;
}
bool compare(const StrBlobPtr &p1, const StrBlobPtr &p2)
{
if(p1.wptr.lock() == p2.wptr.lock() && p1.curr == p2.curr)
return true;
return false;
}
std::shared_ptr<std::vector<std::string>> ConstStrBlobPtr::check(std::size_t i, const std::string & msg) const
{
std::shared_ptr<std::vector<std::string>> ret = wptr.lock();
if(!ret)
throw std::runtime_error("unbound StrBlobPtr");
if(i >= ret->size())
throw std::out_of_range(msg);
return ret;
}
std::string & ConstStrBlobPtr::deref() const
{
std::shared_ptr<std::vector<std::string>> p = check(curr, "dereference past end");
return (*p)[curr];
}
ConstStrBlobPtr & ConstStrBlobPtr::incr()
{
check(curr, "increment past end of StrBlobPtr");
++curr;
return *this;
}
bool compare(const ConstStrBlobPtr &p1, const ConstStrBlobPtr &p2)
{
if(p1.wptr.lock() == p2.wptr.lock() && p1.curr == p2.curr)
return true;
return false;
}