473,765 Members | 2,010 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
33 5078

Gianni Mariani wrote:
Earl Purple wrote:
Gianni Mariani wrote:
at::Ptr<Derived *x = new Derived;
at::Ptr<Base*y = x;

Do that one without templates.
operator= (T * ptr );

part of a template class but not a template pointer. whatever pointer
is passed in must be or implicitly conver to T*. So if T is Base then
you can pass in x because Derived derives from Base.

In addition if the type is a const X then you can pass in a pointer to
non-const because that can implicitly convert to a pointer to const.

Go ahead an try that. I don't remember all the problems but the last
thing you want to do is make it easy to inadvertently convert from a
smart pointer to a regular pointer.
You originally said assigning to a pointer not to another smart
pointer.

If you have an implicit constructor from T* then operator=( T* ) will
work even if you haven't defined it but have defined operator=(
smartptr<T)

And one can always do something wrong with it even with boost.

boost::shared_p tr< Derived p1 (new Derived );
boost::shared_p tr< Base p2 ( p1.get() );

Silly but believe me people do it. Or use reset() and pass in a managed
pointer. I'm sure programmers will manage to find a way of abusing
austria pointers too.

Nov 23 '06 #31
"Gianni Mariani" <gi*******@mari ani.wswrote in message
news:45******** **************@ per-qv1-newsreader-01.iinet.net.au ...
Chris Thomasson wrote:
>"Gianni Mariani" <gi*******@mari ani.wswrote in message
news:45******* *************** @per-qv1-newsreader-01.iinet.net.au ...
>>The overhead associated with atomic increment and decrement is not that
great.

Oh, yes they are:
http://groups.google.com/group/comp....c665e616176dce
(read all please)
...
>Any thoughts?

That thread discusses RW locks. I'm talking about atomic increment.
If you re-read the post, you should find where it specifically talks about
the instructions that are used to actually implment a rw-lock...

On IA32 and AMD64 at least these are single instructions that the CPU
synchronizes with other CPU's. It is certainly more expensive that non
atomic instructions but in the scheme of things, it's not that bad.
Okay... Try to bear with me here:

http://groups.google.com/group/comp....ef173bee4f0c03
(read last paragraph...)

http://groups.google.com/group/comp....a6db6ba9e7fd95
http://groups.google.com/group/comp....b7e857ef440520
See what I am getting at here Gianni?

Austria C++ supports both thread safe and non thread safe reference
counted base classes.
Fine. The more flexibility, the better.

:^)
Nov 24 '06 #32
Chris Thomasson wrote:
....
Okay... Try to bear with me here:

http://groups.google.com/group/comp....ef173bee4f0c03
(read last paragraph...)

http://groups.google.com/group/comp....a6db6ba9e7fd95
http://groups.google.com/group/comp....b7e857ef440520
See what I am getting at here Gianni?
Sure, yes, I agree that atomic ops are more expensive. If they're too
expensive it's usually solved by more code being written (engineering).
If it's not, you write code quicker (sloppier with performance).
Austria C++ allows you to:

a) Have various forms of intrusive reference counts (virtual (COM like),
non thread safe, and thread safe).

b) Have various types of smart pointer that allows you to reduce the
amount of redundant increment/decrement of reference counts. (usually
means littering the interfaces with "PtrDelegat e" and "PtrView" in the
right places.

c) Have various types of smart pointer. (not implemented yet, but
conceivably you can have one type of reference count handled by a
"thread safe" smart pointer and a "non thread safe" smart pointer
allowing you to choose which kind of reference count you want for
different parts of the app).

Having said all that, I think all I have needed to do was option b) and
without really analyzing the characteristics of performance.

YMMV
>
>Austria C++ supports both thread safe and non thread safe reference
counted base classes.

Fine. The more flexibility, the better.
Actually, more modern CPU's are doing better and better with cache
coherency. I wrote a program to test it a while ago and posted it on
the net called "cpulat". Occasionally I run it on new CPU's and I have
found that in the last few years, cpu<->cpu latentcies have actually
decreased. The "cpulat" code is broken on the latest AMD AM2 I tried it
on and I think it's to do with visibility, I'll need to look at it
again. Nontheless, the hardware will get better. Anything we can do to
make it easier to write good code from scratch, the better, and if that
means "fixing the CPU", so bet it!
Nov 24 '06 #33
"Gianni Mariani" <gi*******@mari ani.wswrote in message
news:45******** **************@ per-qv1-newsreader-01.iinet.net.au ...
Chris Thomasson wrote:
...
>Okay... Try to bear with me here:

http://groups.google.com/group/comp....ef173bee4f0c03
(read last paragraph...)

http://groups.google.com/group/comp....a6db6ba9e7fd95
http://groups.google.com/group/comp....b7e857ef440520
See what I am getting at here Gianni?

Sure, yes, I agree that atomic ops are more expensive. If they're too
expensive it's usually solved by more code being written (engineering). If
it's not, you write code quicker (sloppier with performance).

[...]
Anything we can do to make it easier to write good code from scratch, the
better, and if that means "fixing the CPU", so bet it!
Sadly, the current trend in hardware seems to be extremely strong cache
(e.g., due to TM populatiry)... I would prefer an arch with very weak cache,
and very clear memory model documentation.. . Why make the cache so damn
strong? Well, TM is partly to blame IMHO, Sad....

;^(...
Nov 25 '06 #34

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

Similar topics

14
2683
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
5153
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
2307
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
18207
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
2074
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
4505
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
9404
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
10007
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
9959
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
8833
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7379
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
6649
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
5277
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.