forked from jaege/Cpp-Primer-5th-Exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStrBlob.cpp
64 lines (52 loc) · 1.63 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
#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) {
//auto d = std::make_shared<std::vector<std::string>>(*sb.data);
//data = d;
// The following code works the same as above code. The destruction of the
// old pointer is done automatically since the pointer is a smart pointer.
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());
}