- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAutoRCObject.cpp
More file actions
Latest commit
206 lines (162 loc) · 3.58 KB
/
Copy pathAutoRCObject.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
classRCObject
{
protected:
RCObject();
RCObject( const RCObject& rhs );
RCObject& operator=( const RCObject& rhs );
virtual~RCObject () = 0;
public:
voidaddReference ();
voidremoveReference();
voidmarkUnshareable();
boolisShareable() const;
boolisShared ()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() {}
voidRCObject::addReference() { refCount++; }
voidRCObject::removeReference()
{ if ( --refCount == 0 ) deletethis; }
voidRCObject::markUnshareable ( )
{ shareable = false; }
boolRCObject::isShareable() const
{ return shareable; }
boolRCObject::isShared() const
{ return refCount > 1; }
template <classT>
classRCPtr
{
public:
RCPtr(T* realPtr=0 );
RCPtr(const RCPtr& rhs);
~RCPtr();
RCPtr& operator=(const RCPtr& rhs);
T* operator->() const;
T& operator*() const;
private:
T *pointee;
voidinit();
};
template<classT>
RCPtr<T>::RCPtr(T *realPtr):pointee(realPtr)
{
init();
}
template<classT>
RCPtr<T>::RCPtr(const RCPtr& rhs):pointee(rhs.pointee)
{
init();
}
template<classT>
void RCPtr<T>::init()
{
if ( pointee == 0 )
return;
if ( pointee->isShareable() == false )
{
pointee = newT(*pointee);
}
pointee->addReference();
}
template<classT>
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<classT>
RCPtr<T>::~RCPtr()
{
if ( pointee ) pointee->removeReference();
}
template<classT>
T* RCPtr<T>::operator->() const { return pointee; }
template<classT>
T& RCPtr<T>::operator*() const { return *pointee; }
classString
{
public:
String ( constchar *initValue = "" );
constchar& operator [] ( int index ) const;
char& operator[] ( int index );
voidPrint ( );
private:
structStringValue: publicRCObject
{
char *data;
StringValue( constchar *initValue );
StringValue( const StringValue& rhs );
~StringValue ( );
};
RCPtr<StringValue> value;
};
String::StringValue::StringValue ( constchar *initValue )
{
data = newchar [strlen (initValue) + 1];
strcpy(data, initValue);
}
String::StringValue::StringValue( const StringValue& rhs )
{
data = newchar[strlen(rhs.data) + 1];
strcpy(data, rhs.data);
}
String::StringValue::~StringValue()
{
delete [] data;
}
String::String( constchar *initValue )
:value ( new StringValue ( initValue ) )
{}
constchar& String::operator [] ( int index ) const
{
return value->data[index];
}
char& String::operator[] ( int index )
{
if ( value->isShared( ) )
{
value = newStringValue ( value->data );
}
value->markUnshareable();
return value->data[index];
}
voidString::Print()
{
cout << "Data: " << value->data << endl;
cout << "Shared: " << value->isShared() << endl;
cout << "Shareable: " << value->isShareable() << endl << endl;
}
intmain ()
{
String s1("More Effective C++");
String s2("Hello");
cout << s1[1] << endl;
char *p = &s1[1];
s2 = s1;
*p = 'x';
s1.Print();
s2.Print();
}