forked from jzplp/Cpp-Primer-Answer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStrBlob.h
94 lines (76 loc) · 2.7 KB
/
StrBlob.h
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
#ifndef STRBLOB_H
#define STRBLOB_H
#include<vector>
#include<string>
#include<initializer_list>
#include<utility>
#include<memory>
class StrBlobPtr;
class ConstStrBlobPtr;
class StrBlob
{
friend class StrBlobPtr;
friend class ConstStrBlobPtr;
friend bool operator==(const StrBlob &lhs, const StrBlob &rhs);
friend bool operator!=(const StrBlob &lhs, const StrBlob &rhs);
public:
typedef std::vector<std::string>::size_type size_type;
StrBlob();
StrBlob(std::initializer_list<std::string> li);
StrBlob(const StrBlob &sb);
StrBlob & operator=(const StrBlob &sb);
inline size_type size() const { return data->size(); }
bool empty() const { return data->empty(); }
void push_back(const std::string &t) { data->push_back(t); }
void push_back(std::string &&t);
void pop_back();
const std::string & front() const;
std::string & front();
const std::string & back() const;
std::string & back();
StrBlobPtr begin();
StrBlobPtr end();
ConstStrBlobPtr cbegin() const;
ConstStrBlobPtr cend() const;
private:
std::shared_ptr<std::vector<std::string>> data;
void check(size_type i, const std::string & msg) const;
};
class StrBlobPtr
{
friend bool compare(const StrBlobPtr &p1, const StrBlobPtr &p2);
friend bool operator==(const StrBlobPtr &lhs, const StrBlobPtr &rhs);
friend bool operator!=(const StrBlobPtr &lhs, const StrBlobPtr &rhs);
public:
StrBlobPtr() : curr(0) { }
StrBlobPtr(StrBlob &a, std::size_t sz = 0) : wptr(a.data), curr(sz) { }
std::string & deref() const;
StrBlobPtr & incr();
StrBlobPtr & add(StrBlob::size_type num);
private:
std::shared_ptr<std::vector<std::string>> check(std::size_t , const std::string &) const;
std::weak_ptr<std::vector<std::string>> wptr;
std::size_t curr;
};
class ConstStrBlobPtr
{
friend bool compare(const ConstStrBlobPtr &p1, const ConstStrBlobPtr &p2);
friend bool operator==(const ConstStrBlobPtr &lhs, const ConstStrBlobPtr &rhs);
friend bool operator!=(const ConstStrBlobPtr &lhs, const ConstStrBlobPtr &rhs);
public:
ConstStrBlobPtr() : curr(0) { }
ConstStrBlobPtr(const StrBlob &a, std::size_t sz = 0) : wptr(a.data), curr(sz) { }
std::string & deref() const;
ConstStrBlobPtr & incr();
private:
std::shared_ptr<std::vector<std::string>> check(std::size_t , const std::string &) const;
std::weak_ptr<std::vector<std::string>> wptr;
std::size_t curr;
};
bool operator==(const StrBlob &lhs, const StrBlob &rhs);
bool operator==(const StrBlobPtr &lhs, const StrBlobPtr &rhs);
bool operator==(const ConstStrBlobPtr &lhs, const ConstStrBlobPtr &rhs);
bool operator!=(const StrBlob &lhs, const StrBlob &rhs);
bool operator!=(const StrBlobPtr &lhs, const StrBlobPtr &rhs);
bool operator!=(const ConstStrBlobPtr &lhs, const ConstStrBlobPtr &rhs);
#endif