/
StringProxy.cpp
276 lines (217 loc) · 5.1 KB
/
StringProxy.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include <iostream.h>
// Proxy Classes ( Item 30 ). From More Effective C++
// Adds a proxy class to AutoRCObject.cpp to distinguish between
// read usage and write usage of operator[]
// References
// > Item 1: Distinguish between pointers and references
// > Item 2: Prefer C++-style class.
// > Item 5: Be wary of user-defined conversion functions.
// > Item 7: Gives a list of overloadable operators and otherwise
// > Item 17: Lazy evaluation.
// > Item 19: Understand the origin of temporary objects.
// > Item 29: Reference Counting
// See also:
// > Reference.cpp*
// > RCObject.cpp
// > AutoRCObject.cpp
class RCObject
{
protected:
RCObject();
RCObject( const RCObject& rhs );
RCObject& operator=( const RCObject& rhs );
virtual ~RCObject () = 0;
public:
void addReference ();
void removeReference();
void markUnshareable();
bool isShareable() const;
bool isShared ()const;
private:
int refCount;
bool shareable;
};
RCObject::RCObject()
:refCount(0), shareable(true) {}
RCObject::RCObject( const RCObject& )
:refCount(0), shareable(true){}
RCObject& RCObject::operator=( const RCObject& )
{ return *this; }
RCObject::~RCObject() {}
void RCObject::addReference() { refCount++; }
void RCObject::removeReference()
{ if ( --refCount == 0 ) delete this; }
void RCObject::markUnshareable ( )
{ shareable = false; }
bool RCObject::isShareable() const
{ return shareable; }
bool RCObject::isShared() const
{ return refCount > 1; }
template <class T>
class RCPtr
{
public:
RCPtr(T* realPtr=0 );
RCPtr(const RCPtr& rhs);
~RCPtr();
RCPtr& operator=(const RCPtr& rhs);
T* operator->() const;
T& operator*() const;
private:
T *pointee;
void init();
};
template<class T>
RCPtr<T>::RCPtr(T *realPtr):pointee(realPtr)
{
init();
}
template<class T>
RCPtr<T>::RCPtr(const RCPtr& rhs):pointee(rhs.pointee)
{
init();
}
template<class T>
void RCPtr<T>::init()
{
if ( pointee == 0 )
return;
if ( pointee->isShareable() == false )
{
pointee = new T(*pointee);
}
pointee->addReference();
}
template<class T>
RCPtr<T>& RCPtr<T>::operator=( const RCPtr& rhs )
{
if ( pointee == rhs.pointee )
return *this;
if ( pointee )
{
pointee->removeReference();
}
pointee = rhs.pointee;
init();
return *this;
}
template<class T>
RCPtr<T>::~RCPtr()
{
if ( pointee ) pointee->removeReference();
}
template<class T>
T* RCPtr<T>::operator->() const { return pointee; }
template<class T>
T& RCPtr<T>::operator*() const { return *pointee; }
class String
{
public:
class CharProxy
{
public:
CharProxy ( String& str, int index );
CharProxy& operator= (const CharProxy& rhs);
CharProxy& operator= (char c);
char * operator&();
const char * operator&() const;
operator char() const;
private:
String& theString;
int charIndex;
};
const CharProxy operator[] (int index)const; // See Item 1
CharProxy operator[] (int index); // See Item 1
friend class CharProxy;
String ( const char *initValue = "" );
void Print ( );
private:
struct StringValue: public RCObject
{
char *data;
StringValue( const char *initValue );
StringValue( const StringValue& rhs );
~StringValue ( );
};
RCPtr<StringValue> value;
};
String::StringValue::StringValue ( const char *initValue )
{
data = new char [strlen (initValue) + 1];
strcpy(data, initValue);
}
String::StringValue::StringValue( const StringValue& rhs )
{
data = new char[strlen(rhs.data) + 1];
strcpy(data, rhs.data);
}
String::StringValue::~StringValue()
{
delete [] data;
}
String::CharProxy::CharProxy(String& str, int index)
:theString(str), charIndex(index)
{
}
String::CharProxy& String::CharProxy::operator= (const CharProxy& rhs)
{
if ( theString.value->isShared())
{
theString.value = new StringValue(theString.value->data);
}
theString.value->data[charIndex] = rhs.theString.value->data[rhs.charIndex];
return *this;
}
String::CharProxy& String::CharProxy::operator= (char c)
{
if ( theString.value->isShared() )
{
theString.value = new StringValue(theString.value->data);
}
theString.value->data[charIndex] = c;
return *this;
}
char * String::CharProxy::operator&()
{
if ( theString.value->isShared() )
{
theString.value = new StringValue(theString.value->data);
}
theString.value->markUnshareable();
return &(theString.value->data[charIndex]);
}
const char * String::CharProxy::operator&() const
{
return &(theString.value->data[charIndex]);
}
String::CharProxy::operator char() const
{
return theString.value->data[charIndex];
}
const String::CharProxy String::operator[](int index)const
{
return CharProxy(const_cast<String&>(*this), index);
}
String::CharProxy String::operator[] (int index)
{
return CharProxy(*this, index);
}
String::String( const char *initValue )
:value ( new StringValue ( initValue ) )
{}
void String::Print()
{
cout << "Data: " << value->data << endl;
cout << "Shared: " << value->isShared() << endl;
cout << "Shareable: " << value->isShareable() << endl << endl;
}
int main ()
{
String s1("More Effective C++");
char *p = &s1[1];
s1.Print();
String s2 ("Hello");
s2 = s1;
*p = 'x';
s2.Print();
}