-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRCObject.cpp
More file actions
171 lines (136 loc) · 3.05 KB
/
RCObject.cpp
File metadata and controls
171 lines (136 loc) · 3.05 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
#include <iostream.h>
// Implements reference counting ( Item 29 ). From More Effective C++
// Corrected the flaw is Reference.cpp.
// Also implements a base class for reference counting called RCObject
// The reference count is now modified manually by the client classes by calling
// addReference and removeReference at appropriate places.
// See also:
// > Reference.cpp*
// > AutoRCObject.cpp
class RCObject
{
public:
RCObject();
RCObject( const RCObject& rhs );
RCObject& operator=( const RCObject& rhs );
virtual ~RCObject () = 0;
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; }
class String
{
public:
String ( const char *initValue = "" );
String ( const String& rhs );
~String();
String& operator=( const String& rhs );
const char& operator [] ( int index ) const;
char& operator[] ( int index );
void Print ( );
private:
struct StringValue: public RCObject
{
char *data;
StringValue( const char *initValue );
~StringValue ( );
};
StringValue *value;
};
String::StringValue::StringValue ( const char *initValue )
{
data = new char [strlen (initValue) + 1];
strcpy(data, initValue);
this->addReference();
}
String::StringValue::~StringValue()
{
delete [] data;
}
String::String( const char *initValue )
:value ( new StringValue ( initValue ) )
{
value->addReference();
}
String::String( const String& rhs )
{
if ( rhs.value->isShareable() )
{
value = rhs.value;
}
else
{
value = new StringValue (rhs.value->data);
}
value->addReference();
}
String& String::operator=( const String& rhs )
{
if ( value == rhs.value )
return *this;
value->removeReference();
if ( rhs.value->isShareable() )
{
value = rhs.value;
}
else
{
value = new StringValue ( rhs.value->data );
}
return *this;
}
const char& String::operator [] ( int index ) const
{
return value->data[index];
}
char& String::operator[] ( int index )
{
if ( value->isShared( ) )
{
value->removeReference();
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;
}
String::~String()
{
value->removeReference();
}
int main ()
{
String s1("More Effective C++");
char *p = &s1[1];
s1.Print();
String s2 ("Hello");
s2 = s1;
*p = 'x';
s2.Print();
}