[nocturnal-users] Using an SmartPtr with BaseRefAggregator<T>
Geoff Evans
geoff at insomniacgames.com
Tue May 20 20:17:51 EDT 2008
Hi Ricardo, I totally agree with your assessment.
To give some background... RefCountAggregator doesn't get much use around our code, it was written kind of as an academic exercise so I am not surprised that there are some problems J. Typically we inherit classes from RefCountBase like DelegateImpl, EventImpl, and Property. This is more natural and makes programming with the objects easier. RefCountAggregator was primarily designed to append a refcount to API structure outside the control of the programmer (like Win32 structures or structures from external SDKs). If the class of object you want to reference count is within your codebase I would recommend subclassing RefCountBase instead of using RefCountAggregator.
I have updated the authoritative SmartPtr.h to define RefCountAggregator as follows (per your suggestion), and this code should be in our next release:
template<typename T>
class RefCountAggregator : public RefCountBase<T>
{
public:
T m_Object;
RefCountAggregator()
{
}
RefCountAggregator(const T& rhs)
: m_Object (rhs)
{
}
__inline T* operator-> () const
{
return &m_Object;
}
__inline operator T* () const
{
return &m_Object;
}
};
From: nocturnal-users-bounces at nocturnal.insomniacgames.com [mailto:nocturnal-users-bounces at nocturnal.insomniacgames.com] On Behalf Of Ricardo Amores Hernández
Sent: Sunday, May 18, 2008 3:00 PM
To: nocturnal-users at nocturnal.insomniacgames.com
Subject: [nocturnal-users] Using an SmartPtr with BaseRefAggregator<T>
Hi.
I've been peeking into the Nocturnal code, and I would like to make a petition:
could anyone provide an example on using the SmartPtr with a class which does not inherit from RefCountBase<T> ?
I think it's used like this:
class MyRefCountedAggregatorClass() {};
typedef RefCountAggregator < MyRefCountedAggregatorClass > MyClassTypedef;
SmartPtr< MyClassTypedef > testPtr
= new MyClassTypedef ();
That works pretty well for the default constructor of MyRefCountedAggregatorClass(), but if you want to create an instance with a parameter you must use something like this:
MyClassTypedef * tempPtr
= new MyClassTypedef ();
tempPtr->m_Object = MyRefCountedAggregatorClass( 15 );
SmartPtr< MyClassTypedef > testPtr2 = tempPtr;
The code above could be improved (IMHO) adding a conversion constructor to RefCountAggregator<T> :
template <class T >
class RefCountAggregator : RefCountBase<T>
{
RefCountAggregator(const T & copyObj)
{
m_Object = copyObj;
}
// rest of RefCountAggregator code
};
So the previous example now can be coded like this:
typedef RefCountAggregator < MyRefCountedAggregatorClass > MyClassTypedef;
SmartPtr< MyClassTypedef > testPtr2 = new MyClassTypedef( 15 );
The problem with both approaches is that when we create a RefCountAggregator<T> instance, a temporal object of type T is created using the default constructor, even if we want to use a using a parameterized constructor to initialize T. And of course, the code doesn't work for a class without a default constructor.
That makes me think I'm not using Nocturnal's SmartPtr implementation correctly.
Could anybody provide some help?
Thanks in advance
Ricardo Amores Hernández.
PS: I'm from Spain, so excuse my English.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://nocturnal.insomniacgames.com/pipermail/nocturnal-users/attachments/20080520/e30ba08b/attachment.html
More information about the nocturnal-users
mailing list