-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAutoRCObject.cpp
More file actions
206 lines (162 loc) · 3.58 KB
/
AutoRCObject.cpp
File metadata and controls
206 lines (162 loc) · 3.58 KB
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
#include <iostream.h>
// Implements reference counting ( Item 29 ). From More Effective C++
// Implements a base class for reference counting called RCObject
// The reference count is now modified automatically by the wrapper RCPtr
// that in this case makes pointers to Sting::StringValue smart.
// See also:
// > Reference.cpp*
// > RCObject.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:
String ( const char *initValue = "" );
const char& operator [] ( int index ) const;
char& operator[] ( int index );
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::String( const char *initValue )
:value ( new StringValue ( initValue ) )
{}
const char& String::operator [] ( int index ) const
{
return value->data[index];
}
char& String::operator[] ( int index )
{
if ( value->isShared( ) )
{
value = new StringValue ( value->data );
}
value->markUnshareable();
return value->data[index];
}
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++");
String s2("Hello");
cout << s1[1] << endl;
char *p = &s1[1];
s2 = s1;
*p = 'x';
s1.Print();
s2.Print();
}