|
-
September 19th, 2008, 10:00 PM
#1
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
Another question is do you all have some instructions how to create the reference counting ptr class.
I hope you all can help.
A billion thanks for your help.
-
September 20th, 2008, 10:42 AM
#2
Re: Reference Counting
 Originally Posted by Peter_APIIT
My question is
1. I need a simple reference count ptr class
This isn't a question.
 Originally Posted by Peter_APIIT
2. Does cyclic reference counting can be solved ?
Cyclic referencing is partially solved by using weak pointers where appropriate, which are convertible to and from shared pointers but do not by themselves increase the reference count.
 Originally Posted by Peter_APIIT
3. Does policy based design is good approaches in smart pointer ?
It can be, if your application needs to use smart pointers in varied and unusual ways, but for most programmers something like tr1::shared_ptr would do fine. In fact, my advice is that you just use tr1::shared_ptr.
I assume by policy-based design you mean the sort of approach Alexandrescu takes with Loki's smart pointer. If there's any benefit to that approach, it's that the resulting smart pointer template can do pretty much anything, not that its flexibility is something the average programmer would need. Where's the sense in rewriting a template like that? You'd be defeating the idea of code reuse by rewriting instead of reusing a template designed specifically for code reuse.
 Originally Posted by Peter_APIIT
4. What should i define in smart pointer template class ?
Not sure what you mean. What do you want defined in your smart pointer class? You're writing it, so it's up to you. That's the only benefit I can see in not using an existing smart pointer class.
Obviously you'd want a unary operator* and operator->.
 Originally Posted by Peter_APIIT
5. Does smart pointer syntax correct ?
Not sure what you mean.
 Originally Posted by Peter_APIIT
In STL, there just write class U = XXX but why i need the template <class>. What this mean ?
Look up (and this isn't a typo) template template arguments. That might be what you're asking about.
- Alon
-
September 21st, 2008, 04:48 AM
#3
Re: Reference Counting
Cyclic referencing is partially solved by using weak pointers where appropriate, which are convertible to and from shared pointers but do not by themselves increase the reference count.
How it is partially solve cyclic reference by using weak_ptr ?
[
-
September 21st, 2008, 04:52 AM
#4
Re: Reference Counting
Cyclic referencing is partially solved by using weak pointers where appropriate, which are convertible to and from shared pointers but do not by themselves increase the reference count.
How it is partially solve cyclic reference by using weak_ptr ?
but do not by themselves increase the reference count.
What you mean of this sentence?
It can be, if your application needs to use smart pointers in varied and unusual ways, but for most programmers something like tr1::shared_ptr would do fine. In fact, my advice is that you just use tr1::shared_ptr.
I assume by policy-based design you mean the sort of approach Alexandrescu takes with Loki's smart pointer. If there's any benefit to that approach, it's that the resulting smart pointer template can do pretty much anything, not that its flexibility is something the average programmer would need. Where's the sense in rewriting a template like that? You'd be defeating the idea of code reuse by rewriting instead of reusing a template designed specifically for code reuse.
Not really understand what you say here. I'm student who want learn to make used of policy based design and create smart pointer. Therefore, i refuse to use tr1::shared_ptr;
Obviously you'd want a unary operator* and operator->.
I got it at class template storage policy.
Look up (and this isn't a typo) template template arguments. That might be what you're asking about.
I will look up when i free. By the way, is my template template arguments is correct ?
A billion thanks for your help.
-
September 23rd, 2008, 05:14 AM
#5
Re: Reference Counting
I just want the requirement of non-intrusive smart pointer in this case.
A billion thanks for your help.
-
September 23rd, 2008, 06:09 AM
#6
Re: Reference Counting
 Originally Posted by Peter_APIIT
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.
Reference counting smart pointers are notoriously difficult to implement. If you don't absolutely have to do it yourself I suggest you use the Boost implementation instead.
-
September 25th, 2008, 02:41 AM
#7
Re: Reference Counting
I just want to learn to develop because i'm a student.
-
September 25th, 2008, 06:36 AM
#8
Re: Reference Counting
 Originally Posted by Peter_APIIT
I just want to learn to develop because i'm a student.
Have a look at this,
http://www.josuttis.com/libbook/
Under Contents select Examples, then Even more C++ Examples, then CountedPtr<>.
Last edited by _uj; September 25th, 2008 at 06:40 AM.
-
September 26th, 2008, 11:19 PM
#9
-
September 27th, 2008, 03:38 AM
#10
Re: Reference Counting
I have checked that but i doesn't relate me with Loki smart pointer.
I looking for a approach to in policy based design.
This is my code so far.
Code:
#include "StoragePolicy.h"
template <typename T,
template <class>
class StoragePolicy = DefaultSPStorage,
template <class>
class OwnershipPolicy = RCPtr,
template <class>
class ConversionPolicy = DisallowConversion
>
class smart_ptr
{
T* operator->()
{
return aType;
}
T& operator*()
{
return *aType;
}
};
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)
{
if (!pointee)
{
throw NullPointerException();
}
else
{
pImpl = pointee;
}
}
~DefaultSPStorage<aType>()
{
delete pImpl;
}
aType* operator->()
{
if (!pointee)
{
throw NullPointerException();
}
else
{
return pIpml;
}
}
aType& operator*()
{
if (!pointee)
{
throw NullPointerException();
}
else
{
return *pImpl;
}
}
aType* GetImpl(const DefaultSPStorage & that)
{
return that.pImpl;
}
aType& GetImplRef(const DefaultSPStorage & that)
{
return that.pImpl;
}
};
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>()
{
delete referenceCountPtr;
}
void IncreaseRef()
{
*referenceCountPtr++;
}
void DecreaseRef()
{
*referenceCountPtr--;
}
/*
IncreaseRef
DecreaseRef
Realease
*/
/* The Ownership policy must support
intrusive as well as nonintrusive
reference counting.
Therefore, deallocation of memory is
called by function rather than
implicitly destructor called because
it can explicit called by user
*/
};
int main(int argc, char *argv[])
{
/* typedef smart_ptr<int,
DefaultSPStorage, RCPtr> aSmtPtr;
*/
smart_ptr<number, DefaultSPStorage, RCPtr> aSmtPtr;
return 0;
}
Please help me.
Thanks for your help.
-
September 27th, 2008, 10:41 AM
#11
Re: Reference Counting
If you want to implement a policy based smart pointer like loki, then why don't you just look at the source code for loki.
Wakeup in the morning and kick the day in the teeth!! Or something like that.
"i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."
-
September 27th, 2008, 12:57 PM
#12
Re: Reference Counting
 Originally Posted by Peter_APIIT
I have checked that but i doesn't relate me with Loki smart pointer.
I looking for a approach to in policy based design.
Well, if you want to know something ask for it right away. Why wait until post #10?
And if you want to know more about Loki, buy the book (Modern C++ Design by Alexandrescu) and download the library.
Last edited by _uj; September 27th, 2008 at 01:44 PM.
-
October 3rd, 2008, 04:08 AM
#13
Re: Reference Counting
I got the book and its library but i not really understand the code and its design principles.
Please explain and guide me on what to do.
I don't need my code 100% compliant with Loki but at least 50% same with him and learn from him
Thanks.
A billion thanks again.
Last edited by Peter_APIIT; October 3rd, 2008 at 04:13 AM.
-
October 4th, 2008, 11:54 PM
#14
-
October 7th, 2008, 04:36 AM
#15
Re: Reference Counting
I not urge for help but really want to get this done.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|