473,594 Members | 2,770 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A smart pointer and type forwarding

Hello.

I have written a simple reference-counting smart pointer class template
called RefCountPtr<T>. It works in conjunction with another class,
ReferenceCounta ble, which is responsible for the actual counting. Here
is the latter's definition:

// --- Begin ReferenceCounta ble.h ----------

class ReferenceCounta ble
{
public:
virtual ~ReferenceCount able() {}
void incrementRefere nceCount() { count++; }
void decrementRefere nceCount() { count--; }
unsigned int referenceCount( ) const { return count; }
protected:
ReferenceCounta ble() : count(0) {}
private:
unsigned int count;
};

// --- End ReferenceCounta ble.h ----------

Classes whose objects are to be reference-counted must derive from
ReferenceCounta ble. E.g.:

// --- Begin Foo.h ----------

#include "ReferenceCount able.h"

class Foo : public ReferenceCounta ble
{
public:
void f() {}
};

// --- End Foo.h ----------

From now on, one can create instances of the smart pointer class and
use them as regular pointers. E.g.:

RefCountPtr<Foo pfoo = new Foo;
pfoo->f();

The idea of making the smart pointer behave like regular pointers is key
and is also the reason for this post. There is a situation where I have
found myself unable to make my smart pointers behave like regular ones.
It occurs when a given class holds RefCountPtr<Tas member variables.
For a regular pointer, one could do:

class Foo;

class Test
{
Foo* mFoo;
};

Therefore, I would like to be able to have:

#include "RefCountPtr.hp p"

class Foo;

class Test
{
RefCountPtr<Foo mFoo;
};

But, unfortunately, the compiler does not find it sufficient to know
that Foo is a class. Here is a relevant snippet of the /RefCountPtr<T>/
class template, showing its constructors, destructor and only member
variable:

// --- Begin snippet of RefCountPtr.hpp ----------

#include "ReferenceCount able.h"

template <class T>
class RefCountPtr
{
public:
RefCountPtr()
: object(0)
{
}

RefCountPtr(T* pointer)
: object(pointer)
{
if (object)
object->incrementRefer enceCount();
}

RefCountPtr(con st RefCountPtr& original)
: object(original .object)
{
if (object)
object->incrementRefer enceCount();
}

~RefCountPtr()
{
if (object)
{
object->decrementRefer enceCount();
if (object->referenceCount () == 0)
delete object;
}
}
Nov 7 '06 #1
33 5037
Ney André de Mello Zunino wrote:
....
Error E2315 RefCountPtr.hpp 44: 'decrementRefer enceCount' is not a
member of 'Foo', because the type is not yet defined in function
RefCountPtr<Foo >::~RefCountPtr ()

As I understand it, upon finding the declaration of the /Test::mFoo/
member variable, the compiler goes on to instantiate the template for
the type /Foo/. Because the definition for /Foo/ has not been provided
(yet), it fails to compile the calls object->decrementRefer enceCount()
and object->referenceCount ().

The main point of my post is to try to figure out how I could arrange
the RefCountPtr<Tcl ass template so that type forwarding would suffice
when using it as a member of another class. The goal is to *reduce
dependencies* and to have the smart pointer behave like regular ones.

Note: if necessary, I can post the complete code of a simple test program.
It's important to have some example code that shows the error. BTW, the
Austria C++ smart pointer (at::Ptr<>)does all this and more so if you
want an example, go check it out.

Also, your ReferenceCounta ble class does dot handle copy construct and
assignment correctly and neither does your RefCountPtr.
>
Thank you,
Nov 7 '06 #2
Ney André de Mello Zunino wrote:
Hello.

I have written a simple reference-counting smart pointer class template
called RefCountPtr<T>. It works in conjunction with another class,
ReferenceCounta ble, which is responsible for the actual counting. Here
is the latter's definition:

// --- Begin ReferenceCounta ble.h ----------

class ReferenceCounta ble
{
public:
virtual ~ReferenceCount able() {}
void incrementRefere nceCount() { count++; }
void decrementRefere nceCount() { count--; }
unsigned int referenceCount( ) const { return count; }
protected:
ReferenceCounta ble() : count(0) {}
private:
unsigned int count;
};

// --- End ReferenceCounta ble.h ----------

Classes whose objects are to be reference-counted must derive from
ReferenceCounta ble. E.g.:

// --- Begin Foo.h ----------

#include "ReferenceCount able.h"

class Foo : public ReferenceCounta ble
{
public:
void f() {}
};

// --- End Foo.h ----------

From now on, one can create instances of the smart pointer class and
use them as regular pointers. E.g.:

RefCountPtr<Foo pfoo = new Foo;
pfoo->f();

The idea of making the smart pointer behave like regular pointers is key
and is also the reason for this post. There is a situation where I have
found myself unable to make my smart pointers behave like regular ones.
It occurs when a given class holds RefCountPtr<Tas member variables.
For a regular pointer, one could do:

class Foo;

class Test
{
Foo* mFoo;
};

Therefore, I would like to be able to have:

#include "RefCountPtr.hp p"

class Foo;

class Test
{
RefCountPtr<Foo mFoo;
};

But, unfortunately, the compiler does not find it sufficient to know
that Foo is a class. Here is a relevant snippet of the /RefCountPtr<T>/
class template, showing its constructors, destructor and only member
variable:

// --- Begin snippet of RefCountPtr.hpp ----------

#include "ReferenceCount able.h"

template <class T>
class RefCountPtr
{
public:
RefCountPtr()
: object(0)
{
}

RefCountPtr(T* pointer)
: object(pointer)
{
if (object)
object->incrementRefer enceCount();
}

RefCountPtr(con st RefCountPtr& original)
: object(original .object)
Bug: You need to decrement the reference count before reassigning.
{
if (object)
object->incrementRefer enceCount();
}

~RefCountPtr()
{
if (object)
{
object->decrementRefer enceCount();
if (object->referenceCount () == 0)
delete object;
}
}
.
.
.
private:
T* object;
};

// --- End snippet of RefCountPtr.hpp ----------

Here is one of the error messages output by the compiler:

Error E2315 RefCountPtr.hpp 44: 'decrementRefer enceCount' is not a
member of 'Foo', because the type is not yet defined in function
RefCountPtr<Foo >::~RefCountPtr ()

As I understand it, upon finding the declaration of the /Test::mFoo/
member variable, the compiler goes on to instantiate the template for
the type /Foo/. Because the definition for /Foo/ has not been provided
(yet), it fails to compile the calls object->decrementRefer enceCount()
and object->referenceCount ().

The main point of my post is to try to figure out how I could arrange
the RefCountPtr<Tcl ass template so that type forwarding would suffice
when using it as a member of another class. The goal is to *reduce
dependencies* and to have the smart pointer behave like regular ones.

Note: if necessary, I can post the complete code of a simple test program.
Several solutions come to mind:

1. *Declare* Foo fully before your use of it as a member of Test. Of
course, this doesn't aid in reducing dependencies.

2. Put the *definitions* of RefCoutPtr's member functions after all
instances of its use as a member. Absent the export keyword (cf.
http://www.parashift.com/c++-faq-lit...tml#faq-35.14), this
is ugly but not exceedingly dangerous since you'll get compile-time
errors if you mess it up.

3. Use non-intrusive reference counting such as that found in
std::tr1::share d_ptr (aka boost::shared_p tr), Loki::SmartPtr, or this
FAQ:
<http://www.parashift.c om/c++-faq-lite/freestore-mgmt.html#faq-16.22>.

Cheers! --M

Nov 7 '06 #3

Ney André de Mello Zunino wrote:
The idea of making the smart pointer behave like regular pointers is key
and is also the reason for this post. There is a situation where I have
found myself unable to make my smart pointers behave like regular ones.
It occurs when a given class holds RefCountPtr<Tas member variables.
For a regular pointer, one could do:

class Foo;

class Test
{
Foo* mFoo;
};

Therefore, I would like to be able to have:

#include "RefCountPtr.hp p"

class Foo;

class Test
{
RefCountPtr<Foo mFoo;
};

But, unfortunately, the compiler does not find it sufficient to know
that Foo is a class. Here is a relevant snippet of the /RefCountPtr<T>/
class template, showing its constructors, destructor and only member
variable:
You should declare a destructor in Test and put its implementation into
the compilation unit, even if it is empty (which is probably will be).
Then your compiler won't attempt to call the destructor of your
RefCountPtr class.

Note that the type does have to be complete at the point of its
deletion.

Another alternative, that I used in my final version of the intrusive
smart pointer before I did away with it, was to use a second template
parameter and use that in the destructor. RefCountPtr< T, BASE_T=T >
and now BASE_T was the base-class from which you derive and it calls
methods through that. Only BASE_T has to be complete so in Test you
would declare as

class Test
{
RefCountPtr< Foo, ReferenceCounta ble mFoo;
};

Even if you don't want to use it, I would suggest you look at the code
for boost::intrusiv e_ptr (I hope you can decipher it) just to see how
they did it.

Nov 7 '06 #4
Gianni Mariani wrote:
It's important to have some example code that shows the error. BTW, the
Austria C++ smart pointer (at::Ptr<>)does all this and more so if you
want an example, go check it out.
I will have a look at it. Thank you. And let me just clarify that the
reason I decided to create my own smart pointer class was simply as an
opportunity for improving my experience and not because existing
alternatives did not suffice.
Also, your ReferenceCounta ble class does dot handle copy construct and
assignment correctly and neither does your RefCountPtr.
You are right. I am infringing the "rule of three". I suppose that, in
the case of /ReferenceCounta ble/, I should make sure both the copy
constructor and assignment operator reset the reference count for the
new object, right? Is there anything else I should take into account?

As for /RefCountPtr<T>/, could you help me see what I am doing wrong in
the copy constructor? As for the assignment operator, I did implement
it, though I did not include the code in the original post. Here it is:

RefCountPtr& operator=(const RefCountPtr& original)
{
return operator=(origi nal.object);
}

RefCountPtr& operator=(T* pointer)
{
// Check for self-assignment and assignment from
// equivalent smart pointer
if (this->object != pointer)
{
if (object)
{
object->decrementRefer enceCount();
if (object->referenceCount () == 0)
delete object;
}
object = pointer;
if (pointer)
pointer->incrementRefer enceCount();
}
return *this;
}

Is the implementation of the assignment operator for /RefCountPtr<T>/
also incorrect or incomplete?

Thank you,

--
Ney André de Mello Zunino
http://blog.zunino.eti.br/
Nov 8 '06 #5
mlimber wrote:
Ney André de Mello Zunino wrote:
> RefCountPtr(con st RefCountPtr& original)
: object(original .object)

Bug: You need to decrement the reference count before reassigning.
> {
if (object)
object->incrementRefer enceCount();
}
I am sorry, but I could not understand your remark. Why should I
decrement the reference count? For example, suppose I have an instance
/foo/ of type /Foo/ being managed by an instance of /RefCountPtr<Foo >/
named /pFoo1/. So far, the reference counter on the /foo/ object has
value 1. If a new /RefCountPtr<Foo >/, say /pFoo2/, is created by copy
construction from /pFoo1/, I expect both smart pointers to refer to the
same object and, therefore, its reference count should go up by 1,
becoming 2. That is why I increment the managed object's reference count
in the copy constructor. Am I wrong?
Several solutions come to mind:

1. *Declare* Foo fully before your use of it as a member of Test. Of
course, this doesn't aid in reducing dependencies.
Okay. That surely works and it's what I have been doing. But, as you
mentioned, the dependencie issue is still there and the smart pointer is
still not behaving like a regular pointer.
2. Put the *definitions* of RefCoutPtr's member functions after all
instances of its use as a member. Absent the export keyword (cf.
http://www.parashift.com/c++-faq-lit...tml#faq-35.14), this
is ugly but not exceedingly dangerous since you'll get compile-time
errors if you mess it up.
Could you show me what you mean?
3. Use non-intrusive reference counting such as that found in
std::tr1::share d_ptr (aka boost::shared_p tr), Loki::SmartPtr, or this
FAQ:
<http://www.parashift.c om/c++-faq-lite/freestore-mgmt.html#faq-16.22>.
When I decided to write my own smart pointer class, I read a little
about the different approaches one can take. I remember having picked
the intrusive one because it looked easier to implement and because it
incurred little penalty on performance.

Thank you,

--
Ney André de Mello Zunino
Nov 8 '06 #6
Ney André de Mello Zunino wrote:
mlimber wrote:
Ney André de Mello Zunino wrote:
RefCountPtr(con st RefCountPtr& original)
: object(original .object)
Bug: You need to decrement the reference count before reassigning.
{
if (object)
object->incrementRefer enceCount();
}

I am sorry, but I could not understand your remark. Why should I
decrement the reference count? For example, suppose I have an instance
/foo/ of type /Foo/ being managed by an instance of /RefCountPtr<Foo >/
named /pFoo1/. So far, the reference counter on the /foo/ object has
value 1. If a new /RefCountPtr<Foo >/, say /pFoo2/, is created by copy
construction from /pFoo1/, I expect both smart pointers to refer to the
same object and, therefore, its reference count should go up by 1,
becoming 2. That is why I increment the managed object's reference count
in the copy constructor. Am I wrong?
Mea culpa. I was thinking of the assignment operator.
Several solutions come to mind:

1. *Declare* Foo fully before your use of it as a member of Test. Of
course, this doesn't aid in reducing dependencies.

Okay. That surely works and it's what I have been doing. But, as you
mentioned, the dependencie issue is still there and the smart pointer is
still not behaving like a regular pointer.
2. Put the *definitions* of RefCoutPtr's member functions after all
instances of its use as a member. Absent the export keyword (cf.
http://www.parashift.com/c++-faq-lit...tml#faq-35.14), this
is ugly but not exceedingly dangerous since you'll get compile-time
errors if you mess it up.

Could you show me what you mean?
Declare RefCountPtr in a header RefCountPtr.hpp (or whatever), but put
the implementation of its functions in RefCountPtr.cpp (or whatever).
Include the latter file explicitly in every .cpp file that uses
RefCountPtr but make sure it is *after* the full declarations of the
forward-declared objects. E.g.,

In Bar.hpp
#include "RefCountPtr.hp p"
class Foo;
class Bar
{
// ...
RefCountPtr<Foo foo_;
};

In Bar.cpp:
#include "Bar.hpp"
#include "Foo.hpp"
#include "RefCountPtr.cp p"
// ...
3. Use non-intrusive reference counting such as that found in
std::tr1::share d_ptr (aka boost::shared_p tr), Loki::SmartPtr, or this
FAQ:
<http://www.parashift.c om/c++-faq-lite/freestore-mgmt.html#faq-16.22>.

When I decided to write my own smart pointer class, I read a little
about the different approaches one can take. I remember having picked
the intrusive one because it looked easier to implement and because it
incurred little penalty on performance.
There are certainly trade-offs to be had -- one of which is the ability
to forward-declare a class. The smart pointer in the FAQ is pretty
simple and straight-forward, though it doesn't have all the options of
shared_ptr and the like. I'm all for exercising your skills by writing
your own smart pointer, but for production code, I'd suggest going with
a tried-and-true smart pointer from the standard library, Boost, or
similar if possible. They're notoriously tricky to get right.

Cheers! --M

Nov 8 '06 #7

Ney André de Mello Zunino wrote:
>
RefCountPtr& operator=(const RefCountPtr& original)
{
return operator=(origi nal.object);
}
RefCountPtr& operator=(T* pointer)
{
// Check for self-assignment and assignment from
// equivalent smart pointer
if (this->object != pointer)
{
stop right there!

operator= should be implemented in terms of copy-construct and swap!

operator=( T* ) is not recommended either. Better to use a method such
as reset( T* )

swap is nice and simple and will never throw.

Nov 8 '06 #8
Earl Purple:
operator= should be implemented in terms of copy-construct and swap!

operator=( T* ) is not recommended either. Better to use a method such
as reset( T* )

swap is nice and simple and will never throw.

Nicely inefficient, yes.

--

Frederick Gotham
Nov 8 '06 #9
Ney André de Mello Zunino wrote:
Gianni Mariani wrote:
>It's important to have some example code that shows the error. BTW,
the Austria C++ smart pointer (at::Ptr<>)does all this and more so if
you want an example, go check it out.

I will have a look at it. Thank you. And let me just clarify that the
reason I decided to create my own smart pointer class was simply as an
opportunity for improving my experience and not because existing
alternatives did not suffice.
Excellent.
>
>Also, your ReferenceCounta ble class does dot handle copy construct and
assignment correctly and neither does your RefCountPtr.

You are right. I am infringing the "rule of three". I suppose that, in
the case of /ReferenceCounta ble/, I should make sure both the copy
constructor and assignment operator reset the reference count for the
new object, right? Is there anything else I should take into account?
No. Yes.

Think about what the reference count is for. The reference count is the
number of existing smart pointers that are pointing to the object being
reference counted ! This does not change when the object is assigned.
Since the default assignment operator and copy constructor copy the
reference count, this is not the right behaviour. So you need to ignore
the reference count of the RHS on assignment and set the reference count
to whatever you initial reference count is on copy construct.

>
As for /RefCountPtr<T>/, could you help me see what I am doing wrong in
the copy constructor? As for the assignment operator, I did implement
it, though I did not include the code in the original post. Here it is:

RefCountPtr& operator=(const RefCountPtr& original)
{
return operator=(origi nal.object);
return * this;
}
Pointer assignment should be a template. You want to allow assignment
from types derived from T.
RefCountPtr& operator=(T* pointer)
{
// Check for self-assignment and assignment from
// equivalent smart pointer
if (this->object != pointer)
{
if (object)
{
Is it better that an object "delete" itself of that the smart pointer
calls delete ? You need to also consider using this in a threaded
environment. The first thing you want to do is to place the pointer
being assigned with the incremeneted count in place and the very last
thing is call decrement reference and in your case delete. The FAQ does
have it in the right order, so take a look at the FAQ.
object->decrementRefer enceCount();
if (object->referenceCount () == 0)
delete object;
}
object = pointer;
if (pointer)
pointer->incrementRefer enceCount();
}
return *this;
}

Is the implementation of the assignment operator for /RefCountPtr<T>/
also incorrect or incomplete?
incorrect ...

Also, consider letting the object do whatever it does on reference count
reaching zero and the smart pointer call whatever interface to manage it.

Also, what you're designing here is an "intrusive" smart pointer. There
is also an "extrusive" smart pointer idea which is what seems to be
targeted for approval for the next version of the standard. You can
find it in the boost shared_ptr implementation.
Nov 8 '06 #10

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

Similar topics

14
2663
by: David B. Held | last post by:
I wanted to post this proposal on c.l.c++.m, but my news server apparently does not support that group any more. I propose a new class of exception safety known as the "smart guarantee". Essentially, the smart guarantee promises to clean up resources whose ownership is passed into the function, for whatever defintion of "clean up" is most appropriate for the resource passed. Note that this is different from both the basic and the...
8
5128
by: Axter | last post by:
I normally use a program call Doxygen to document my source code.(http://www.stack.nl/~dimitri/doxygen) This method works great for small and medium size projects, and you can get good documentation like the following: http://axter.com/smartptr Now I'm on a client site, and I'm trying to create the same type of documentation on a very large project. I ran the Doxygen program, and it ran for over 16 hours, before I had
6
2296
by: zl2k | last post by:
hi, When I considered about preventing memory leaking, the method came up to my mind is using boost smart pointer if possible (use stl::vector instead of type, use smart pointer whenever declare an instance of class). With the container of STL, this may often result in a vector of smart pointers, or a smart pointer of STL container. Am I making things too complicated or this is an usual implementation? Thanks in advance. zl2k
14
18172
by: Ian | last post by:
I am looking at porting code from a C++ application to C#. This requires implementing data sharing functionality similar to what is provided by a smart pointer in C++. I have only recently begun to work in C# and am asking for suggestions/comments of how to implement a similar data sharing technique in C#. A C++ smart pointer can be used to share common information. For example, assume information managed by objects I1, I2, I3,...
13
2060
by: Phil Bouchard | last post by:
I am currently writting a smart pointer which is reasonnably stable and I decided supporting allocators for completion because of its increase in efficiency when the same pool used by containers is shared. I was told the new standards are being finalized and I am hoping minor but important changes could be applied before anything else. To give a quick overview on the current status of this topic, you will find below the latest text...
50
4457
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): //------------------------------------------------------------------ template<typename Data_t> class SmartPointer { Data_t* data; void(*deleterFunc)(Data_t*);
0
7936
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7874
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
8241
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
8366
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
7997
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
8227
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...
0
5402
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
3893
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1469
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.