473,626 Members | 3,093 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Re: Reference Counting

These are difference template arguments:

template template arguments

and

template typename arguments

There is also template arguments that are just integer literals or pointers.

What is template template arguments and template typename arguments ?

The reason i write in policy based design is because this is the
requirement of the assignment.

My code so far:

Expand|Select|Wrap|Line Numbers
  1. #include "StoragePolicy.h"
  2.  
  3. template <typename T,
  4. template <class>
  5. class StoragePolicy = DefaultSPStorage,
  6.  
  7. template <class>
  8. class OwnershipPolicy = RCPtr,
  9.  
  10. template <class>
  11. class ConversionPolicy = DisallowConversion
  12.         
  13.                 >
  14.  
  15. class smart_ptr
  16. {
  17. T* operator->()
  18. {
  19. return aType;
  20. }
  21.  
  22. T& operator*()
  23. {
  24. return *aType;
  25. }
  26. };
  27.  
  28.  
  29.  
  30. template <class aType>
  31. class DefaultSPStorage
  32. {
  33. private:
  34.  
  35. /*
  36. SmartPtr passes StoragePolicy<T>::T*
  37. to Ownership- Policy.
  38. */
  39. aType * pImpl;
  40.  
  41. public:
  42. DefaultSPStorage () : pImpl(new aType())
  43. {
  44. }
  45.  
  46. explicit DefaultSPStorage<aType>(aType* pointee)
  47. {
  48. if (!pointee)
  49. {
  50. throw NullPointerException();
  51. }
  52. else
  53. {
  54. pImpl = pointee;
  55. }
  56. }
  57.  
  58.  
  59. ~DefaultSPStorage<aType>()
  60. {
  61. delete pImpl;
  62. }
  63.  
  64.  
  65. aType* operator->()
  66. {
  67. if (!pointee)
  68. {
  69. throw NullPointerException();
  70. }
  71. else
  72. {
  73. return pIpml;
  74. }
  75. }
  76.  
  77. aType& operator*()
  78. {
  79. if (!pointee)
  80. {
  81. throw NullPointerException();
  82. }
  83. else
  84. {
  85. return *pImpl;
  86. }
  87. }
  88.  
  89. aType* GetImpl(const DefaultSPStorage & that)
  90. {
  91. return that.pImpl;
  92. }
  93.  
  94. aType& GetImplRef(const DefaultSPStorage & that)
  95. {
  96. return that.pImpl;
  97. }
  98. };
  99.  
  100.  
  101. template <class sameType>
  102. class RCPtr
  103. {
  104.  
  105. private:
  106. unsigned int* referenceCountPtr;
  107.  
  108. public:
  109.  
  110. RCPtr<sameType>() : refereceCountPtr(new unsigned int(1))
  111. {
  112. }
  113.  
  114. RCPtr<sameType>(const RCPtr & that)
  115. {
  116. unsigned int *temp = new unsigned int(1);
  117.  
  118. temp = that.referenceCountPtr;
  119. referenceCountPtr = temp;
  120. }
  121.  
  122. ~RCPtr<sameType>()
  123. {
  124. delete referenceCountPtr;
  125. }
  126.  
  127.  
  128. void IncreaseRef()
  129. {
  130. *referenceCountPtr++;
  131. }
  132.  
  133. void DecreaseRef()
  134. {
  135. *referenceCountPtr--;
  136. }
  137.  
  138. /*
  139. IncreaseRef
  140. DecreaseRef
  141.  
  142. Realease
  143.  
  144. */
  145.  
  146.  
  147. /*    The Ownership policy must support
  148. intrusive as well as nonintrusive
  149. reference counting.
  150.  
  151. Therefore, deallocation of memory is
  152. called by function rather than
  153. implicitly destructor called because
  154. it can explicit called by user
  155. */
  156. };
  157.  
  158. int main(int argc, char *argv[])
  159. {
  160. /*    typedef smart_ptr<int,
  161. DefaultSPStorage, RCPtraSmtPtr;
  162. */
  163. smart_ptr<number, DefaultSPStorage, RCPtraSmtPtr;
  164.  
  165. return 0;
  166. }
  167.  
  168.  
  169.  

I really need you all help.

Please help me.

A billion thanks in advance for your help.
Sep 27 '08 #1
1 1484
Pe********@gmai l.com wrote:
>
>These are difference template arguments:

template template arguments

and

template typename arguments

There is also template arguments that are just integer literals or
pointers.


What is template template arguments and template typename arguments ?
What about reading a little?

The reason i write in policy based design is because this is the
requirement of the assignment.
Ok.

My code so far:

[code]
#include "StoragePolicy. h"

template <typename T,
template <class>
class StoragePolicy = DefaultSPStorag e,

template <class>
class OwnershipPolicy = RCPtr,

template <class>
class ConversionPolic y = DisallowConvers ion
Way too complicated. Start with a smart pointer that only has different
ownership policies, e.g., a reference counted ownership and a deep copy
policy.
class smart_ptr
{
T* operator->()
{
return aType;
}
Not good. The operator->() is a typical example of an operator that you will
want a policy to provide. There should be no hard-coded aType in your smart
pointer.
>
T& operator*()
{
return *aType;
}
};
operator* on the other hand, can be defined in terms of operator->() quite
generically as:

reference operator* ( void ) const {
return( *( this->operator->() ) );
}
Also: how is smart_ptr supposed to take advantage of all these policies?
[snip lots of "code"]

Maybe, you are in need of an example showing how policies actually can be
used to (a) reuse code that is common and (b) supply the code that differs.
Here is a simple smart pointer with two different ownership models:
#include <algorithm>
#include <functional>
using std::swap;
template < typename T >
class refcounted_poli cy {

typedef T value_type;
typedef value_type * pointer;
typedef value_type const * const_pointer;

struct count {
pointer the_ptr;
unsigned long ref_count;

count ( pointer ptr )
: the_ptr ( ptr )
, ref_count ( 1 )
{}

~count ( void ) {
delete ( the_ptr );
}

};

count * count_ptr;

public:

friend
void swap ( refcounted_poli cy & lhs, refcounted_poli cy rhs ) {
swap( lhs.count_ptr, rhs.count_ptr );
}

refcounted_poli cy ( pointer ptr )
: count_ptr( new count ( ptr ) )
{}

refcounted_poli cy ( refcounted_poli cy const & other )
: count_ptr ( other.count_ptr )
{
++ count_ptr->ref_count;
}

~refcounted_pol icy ( void ) {
-- count_ptr->ref_count;
if ( count_ptr->ref_count == 0 ) {
delete( count_ptr );
}
}

refcounted_poli cy & operator= ( refcounted_poli cy other ) {
swap ( *this, other );
return( *this );
}

const_pointer operator-( void ) const {
return( count_ptr->t_ptr );
}

pointer operator-( void ) {
return( count_ptr->t_ptr );
}

};
template < typename T >
class deepcopy_policy {

typedef T value_type;
typedef value_type * pointer;
typedef value_type const * const_pointer;

pointer the_ptr;

public:

friend
void swap ( deepcopy_policy & lhs, deepcopy_policy rhs ) {
swap( lhs.the_ptr, rhs.the_ptr );
}

deepcopy_policy ( pointer ptr )
: the_ptr( ptr )
{}

deepcopy_policy ( deepcopy_policy const & other )
: the_ptr ( other.the_ptr ? new value_type ( *other.the_ptr ) : 0 )
{}

~deepcopy_polic y ( void ) {
delete ( the_ptr );
}

deepcopy_policy & operator= ( deepcopy_policy other ) {
swap ( *this, other );
return( *this );
}

const_pointer operator-( void ) const {
return( the_ptr );
}

pointer operator-( void ) {
return( the_ptr );
}

};


template < typename T,
template <classclass ownership_polic y >
class smart_ptr :
public ownership_polic y< T >
{

typedef ownership_polic y< T ownership;

public:

typedef T value_type;
typedef value_type * pointer;
typedef value_type const * const_pointer;
typedef value_type & reference;
typedef value_type const & const_reference ;

smart_ptr ( pointer ptr = 0 )
: ownership ( ptr )
{}

const_reference operator* ( void ) const {
return( *( this->operator->() ) );
}

reference operator* ( void ) {
return( *( this->operator->() ) );
}

friend
bool operator== ( smart_ptr const & lhs, smart_ptr const & rhs ) {
return ( lhs.operator->() == rhs.operator->() );
}

friend
bool operator!= ( smart_ptr const & lhs, smart_ptr const & rhs ) {
return ( lhs.operator->() != rhs.operator->() );
}

friend
bool operator< ( smart_ptr const & lhs, smart_ptr const & rhs ) {
return ( std::less<point er>
( lhs.operator->(), rhs.operator->() ) );
}

friend
bool operator<= ( smart_ptr const & lhs, smart_ptr const & rhs ) {
return ( std::less_equal <pointer>
( lhs.operator->(), rhs.operator->() ) );
}
friend
bool operator( smart_ptr const & lhs, smart_ptr const & rhs ) {
return ( std::greater<po inter>
( lhs.operator->(), rhs.operator->() ) );
}

friend
bool operator>= ( smart_ptr const & lhs, smart_ptr const & rhs ) {
return ( std::greater_eq ual<pointer>
( lhs.operator->(), rhs.operator->() ) );
}

};


#include <iostream>

struct Base {

Base ( void ) {
std::cout << "base is born.\n";
}

Base ( Base const & other ) {
std::cout << "base is copied.\n";
}

virtual ~Base () {
std::cout << "base dies.\n";
}

friend
std::ostream & operator<< ( std::ostream & ostr,
Base const & obj ) {
return( ostr << "base" );
}

};
int main ( void ) {
{
smart_ptr< Base, deepcopy_policy a_ptr ( new Base() );
smart_ptr< Base, deepcopy_policy b_ptr ( a_ptr );
smart_ptr< Base, deepcopy_policy c_ptr ( a_ptr );
a_ptr = smart_ptr< Base, deepcopy_policy >();
std::cout << "first handle deleted.\n";
b_ptr = smart_ptr< Base, deepcopy_policy >();
std::cout << "second handle deleted.\n";
}
std::cout << "------------------------\n";
{
smart_ptr< Base, refcounted_poli cy a_ptr ( new Base() );
smart_ptr< Base, refcounted_poli cy b_ptr ( a_ptr );
smart_ptr< Base, refcounted_poli cy c_ptr ( a_ptr );
a_ptr = smart_ptr< Base, refcounted_poli cy >();
std::cout << "first handle deleted.\n";
b_ptr = smart_ptr< Base, refcounted_poli cy >();
std::cout << "second handle deleted.\n";
}
}

Note that derived classes are not handled correctly (true cloning is missing
from the deepcopy policy). Also, there is no support for incomplete types.
In short, many things that you would expect from a good smart pointer are
missing.
Best

Kai-Uwe Bux

Sep 28 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2174
by: Elbert Lev | last post by:
Please correct me if I'm wrong. Python (as I understand) uses reference counting to determine when to delete the object. As soon as the object goes out of the scope it is deleted. Python does not use garbage collection (as Java does). So if the script runs a loop: for i in range(100): f = Obj(i)
1
2701
by: ash | last post by:
hi does anyone has any experience with flyweight pattern with refernce counting i want to share objects between multiple clients and want to delete the object from shared pool when the last client deletes a refernce to it
27
5944
by: Jason Heyes | last post by:
To my understanding, std::vector does not use reference counting to avoid the overhead of copying and initialisation. Where can I get a reference counted implementation of std::vector? Thanks.
0
1745
by: Kalle Rutanen | last post by:
Hello I implemented reference counting in my program, and found out many problems associated with it. I wonder if the following problems can be solved automatically rather manually ? 1. In its member function, an object manages to destroy last reference to itself and thus destructs before the end of the member function.
1
3247
by: Tony Johansson | last post by:
Hello Experts! I reading a book called programming with design pattern revealed by Tomasz Muldner and here I read something that I don't understand completely. It says "A garbarage collector, such as the one used in Java, maintains a record of whether or not an object is currentlys being used. An unused object is tagged as garbage,
1
1873
by: Tony Johansson | last post by:
Hello Experts! I reading a book called programming with design pattern revealed by Tomasz Muldner and here I read something that I don't understand completely. It says "A garbarage collector, such as the one used in Java, maintains a record of whether or not an object is currentlys being used. An unused object is tagged as garbage,
4
4191
by: aaronfude | last post by:
Hi, Please consider the following class (it's not really my class, but it's a good example for my question): class Vector { int myN; double *myX; Vector(int n) : myN(n), myX(new double) { } double &operator()(int i) { return myX; }
1
2271
by: oec.deepak | last post by:
Hi Cn any one telll me what is Reference counting in C++.
8
1854
by: mathieu | last post by:
Hi there I have implemented a very simple smartpointer class (invasive design). And I was wondering what should be the natural API when using those in a Container. I choose to define the following operator in my smartpointer class: .... operator ObjectType * () const
275
12233
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
8192
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8696
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8637
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8358
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8502
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6119
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5571
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4195
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1805
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.