-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlobPtr.cpp
182 lines (154 loc) · 3.95 KB
/
BlobPtr.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//
// Created by martin on 24/05/2022.
//
#include "BlobPtr.h"
#include "Blob.h"
template <typename T> BlobPtr<T>::BlobPtr() : current (0) {}
template <typename T> BlobPtr<T>::BlobPtr (Blob<T> &a, size_t sz) : w_ptr (a.data), current (sz) {}
template <typename T> BlobPtr<T> &BlobPtr<T>::operator= (const BlobPtr &rhs)
{
if (this == &rhs)
{ // is there any need of self-assignment .
return *this;
}
w_ptr = rhs.w_ptr;
current = rhs.current;
cout << "copy assignment constructor called." << endl;
return *this;
}
template <typename T> string &BlobPtr<T>::dereference() const
{
// auto p = check(current, "dereference past end");
// return ( *p )[current];
return (*check (current, "dereference past end"))[current];
}
template <typename T> BlobPtr<T> &BlobPtr<T>::increment()
{
auto ret = check (current, "increment past end of Str_blob_ptr");
++current;
return *this;
}
template <typename T> shared_ptr<vector<T>> BlobPtr<T>::check (size_t i, const string &msg) const
{
auto ret = w_ptr.lock();
if (! ret)
{
throw runtime_error ("unbound Str_blob_ptr");
}
if (i >= ret->size())
{
throw out_of_range (msg);
}
return ret;
}
template <typename T> BlobPtr<T>::~BlobPtr()
{
// cout << "destroying str_blob_ptr" << endl;
}
template <typename U> bool operator== (const BlobPtr<U> &lhs, const BlobPtr<U> &rhs)
{
auto lhs_ptr = lhs.w_ptr.lock();
auto rhs_ptr = rhs.w_ptr.lock();
if (! (lhs_ptr && rhs_ptr))
{
throw runtime_error ("weak pointer is expired");
return false;
}
return *lhs_ptr == *rhs_ptr && lhs.current == rhs.current;
}
template <typename U> bool operator!= (const BlobPtr<U> &lhs, const BlobPtr<U> &rhs) { return ! (rhs == lhs); }
template <typename U> ostream &operator<< (ostream &os, const BlobPtr<U> &ptr)
{
// if (auto spt = ptr.w_ptr.lock())
// {
// for (auto &s : *spt)
// {
// cout << s;
// if (s != spt->back())
// {
// cout << " ";
// }
// }
// }
// else
// {
// std::cout << "weak pointer is expired" << endl;
// }
if (auto spt = ptr.w_ptr.lock())
{
cout << (*spt)[ptr.current];
}
else
{
std::cout << "weak pointer is expired" << endl;
}
return os;
}
template <typename T> string &BlobPtr<T>::operator[] (size_t n)
{
if (auto spt = w_ptr.lock())
{
return (*spt)[n];
}
else
{
throw runtime_error ("weak pointer is expired");
exit (EXIT_FAILURE);
}
}
template <typename T> const string &BlobPtr<T>::operator[] (size_t n) const
{
if (auto spt = w_ptr.lock())
{
return (*spt)[n];
}
else
{
throw runtime_error ("weak pointer is expired");
exit (EXIT_FAILURE);
}
}
template <typename T> BlobPtr<T> &BlobPtr<T>::operator++()
{
auto ret = check (current, "increment past end of Str_blob_ptr");
++current;
return *this;
}
template <typename T> BlobPtr<T> &BlobPtr<T>::operator--()
{
--current;
auto ret = check (current, "increment past begin of Str_blob_ptr");
return *this;
}
template <typename T> BlobPtr<T> BlobPtr<T>::operator++ (int)
{
auto ret = *this;
++*this;
return ret;
}
template <typename T> BlobPtr<T> BlobPtr<T>::operator-- (int)
{
auto ret = *this;
--*this;
return ret;
}
template <typename T> size_t BlobPtr<T>::size() const
{
if (auto spt = w_ptr.lock())
{
return (*spt).size();
}
else
{
throw runtime_error ("weak pointer is expired");
exit (EXIT_FAILURE);
}
}
template <typename T> BlobPtr<T> &BlobPtr<T>::operator+() { return ++*this; }
template <typename T> BlobPtr<T> &BlobPtr<T>::operator-() { return --*this; }
template <typename T> string &BlobPtr<T>::operator*() const
{
auto p = check (current, "dereference past end");
return (*p)[current];
}
template <typename T> string *BlobPtr<T>::operator->() const { return &this->operator*(); }