CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    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.

  2. #2
    Join Date
    Aug 2005
    Location
    LI, NY
    Posts
    576

    Re: Reference Counting

    Quote Originally Posted by Peter_APIIT
    My question is
    1. I need a simple reference count ptr class
    This isn't a question.

    Quote 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.

    Quote 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.
    Quote 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->.
    Quote Originally Posted by Peter_APIIT
    5. Does smart pointer syntax correct ?
    Not sure what you mean.

    Quote 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

  3. #3
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    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 ?

    [

  4. #4
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    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.

  5. #5
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Reference Counting

    I just want the requirement of non-intrusive smart pointer in this case.

    A billion thanks for your help.

  6. #6
    Join Date
    Nov 2003
    Posts
    1,405

    Re: Reference Counting

    Quote 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.

  7. #7
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Reference Counting

    I just want to learn to develop because i'm a student.

  8. #8
    Join Date
    Nov 2003
    Posts
    1,405

    Re: Reference Counting

    Quote 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.

  9. #9
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Reference Counting

    Thanks man.

  10. #10
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    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.

  11. #11
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863

    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."

  12. #12
    Join Date
    Nov 2003
    Posts
    1,405

    Re: Reference Counting

    Quote 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.

  13. #13
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    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.

  14. #14
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Reference Counting

    Please help me.

  15. #15
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Reference Counting

    I not urge for help but really want to get this done.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured