74template<
class T >
class RefCountIPtr {
77 RefCountIPtr(T * realPtr = 0)
78 : counter_(
new CountHolder){
79 counter_->pointee_ = realPtr;
83 RefCountIPtr(
const RefCountIPtr & rhs)
84 : counter_(rhs.counter_){
88 RefCountIPtr(RefCountIPtr & rhs)
89 : counter_(rhs.counter_){
94 counter_->removeReference();
97 RefCountIPtr & operator = (
const RefCountIPtr & rhs){
99 if (counter_ != rhs.counter_) {
100 counter_->removeReference();
101 counter_ = rhs.counter_;
107 RefCountIPtr & operator = (RefCountIPtr & rhs){
109 if (counter_ != rhs.counter_) {
110 counter_->removeReference();
111 counter_ = rhs.counter_;
121 return counter_->pointee_;
124 T * operator->()
const{
126 return counter_->pointee_;
132 return *counter_->pointee_;
135 T & operator*()
const {
137 return *counter_->pointee_;
141 struct CountHolder :
public RCObject {
142 ~CountHolder() {
delete pointee_; }
147 if (counter_->isShareable() ==
false) {
148 T *oldValue = counter_->pointee_;
149 counter_ =
new CountHolder;
150 counter_->pointee_ = oldValue ?
new T(*oldValue) : 0;
152 counter_->addReference();
155 CountHolder *counter_;