forked from jzplp/Cpp-Primer-Answer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStrBlob.h
55 lines (42 loc) · 1.16 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
#ifndef STRBLOB_H
#define STRBLOB_H
#include<vector>
#include<string>
#include<initializer_list>
#include<memory>
class StrBlobPtr;
class StrBlob
{
friend class StrBlobPtr;
public:
typedef std::vector<std::string>::size_type size_type;
StrBlob();
StrBlob(std::initializer_list<std::string> li);
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 pop_back();
const std::string & front() const;
std::string & front();
const std::string & back() const;
std::string & back();
StrBlobPtr begin();
StrBlobPtr end();
private:
std::shared_ptr<std::vector<std::string>> data;
void check(size_type i, const std::string & msg) const;
};
class StrBlobPtr
{
friend bool compare(StrBlobPtr &p1, StrBlobPtr &p2);
public:
StrBlobPtr() : curr(0) { }
StrBlobPtr(StrBlob &a, std::size_t sz = 0) : wptr(a.data), curr(sz) { }
std::string & deref() const;
StrBlobPtr & 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;
};
#endif