Reference Counting
Hello to all expect C++ programmer, recently i have developed a smart
pointer class but i don't know how to implement a reference counting.
My smart pointer is non-intrusive smart pointer.
My question is
1. I need a simple reference count ptr class
2. Does cyclic reference counting can be solved ?
3. Does policy based design is good approaches in smart pointer ?
4. What should i define in smart pointer template class ?
5. Does smart pointer syntax correct ?
In STL, there just write class U = XXX but why i need the template
<class>. What this mean ?
I know what is template.
This is my code so far.
[code]
template <typename T,
template <class aType>
class StoragePolicy = DefaultSPStorage,
template <class >
class OwnershipPolicy = RCPtr,
template <class>
class ConversionPolicy = DisallowConversion
>
class smart_ptr{};
#ifndef STORAGE_POLICY
#define STORAGE_POLICY
template <class aType>
class DefaultSPStorage
{
private:
/*
SmartPtr passes StoragePolicy<T>::T*
to Ownership- Policy.
*/
aType * pImpl;
public:
DefaultSPStorage () : pImpl(new aType())
{
}
explicit DefaultSPStorage<aType>(aType* pointee) : pImpl(pointee)
{
if (!pointee) throw NullPointerException();
}
DefaultSPStorage<aType>& operator=(const DefaultSPStorage<aType>&
rhs)
{
}
~DefaultSPStorage<aType>()
{
}
aType* operator->()
{
if (pImpl != 0)
{
return pIpml;
}
}
aType& operator*()
{
return *pImpl;
}
aType* GetImpl(const DefaultSPStorage & that)
{
return that.pImpl;
}
aType& GetImplRef(const DefaultSPStorage & that)
{
return that.pImpl;
}
/* storageImpl.Destroy()
*/
};
#endif
#ifndef RCPTR
#define RCPTR
template <class sameType>
class RCPtr
{
private:
unsigned int* referenceCountPtr;
public:
RCPtr<sameType>() : refereceCountPtr(new unsigned int(1))
{
}
RCPtr<sameType>(const RCPtr & that)
{
unsigned int *temp = new unsigned int(1);
temp = that.referenceCountPtr;
referenceCountPtr = temp;
}
RCPtr<sameType>& operator=(const RCPtr<sameType>& rhs)
{
}
~RCPtr<sameType>()
{
delete referenceCountPtr;
}
/*
IncreaseRef
DecreaseRef
Realease
*/
/* The Ownership policy must support
intrusive as well as nonintrusive
reference counting.
Therefore, deallocation of memory is
called by functionrather than
implicitly destructor called because
it can explicit called by user
*/
};
#endif
[/code]
My question is do you all have some instructions how to create the
reference counting ptr class.(What to do for non-intrusive smart
pointer).
hope you all can help.
A billion thanks for your help.