3ds Max C++ API Reference
RefPtr< RefObjectType, SafeRefObjectType, LifeTimePolicy > Class Template Reference

This template class act similar as CComPtr. More...

#include <IRefObject.h>

+ Inheritance diagram for RefPtr< RefObjectType, SafeRefObjectType, LifeTimePolicy >:

Public Types

typedef RefObjectType ObjectType
 
typedef RefPtr< RefObjectType, SafeRefObjectType, LifeTimePolicy > SelfType
 

Public Member Functions

 RefPtr ()
 
 RefPtr (RefObjectType *pPtr)
 
 RefPtr (const SelfType &other)
 
template<class OtherInterface , class OtherSafeRefObjectType , class OtherLifeTimePolicy >
 RefPtr (const RefPtr< OtherInterface, OtherSafeRefObjectType, OtherLifeTimePolicy > &other)
 
template<class OtherInterface >
 RefPtr (OtherInterface *pPtr)
 
 ~RefPtr ()
 
void Swap (SelfType &other)
 
void Release ()
 
 operator RefObjectType * () const
 
RefObjectType & operator* () const
 
RefObjectType * GetPointer () const
 
SafeRefObjectType * operator-> () const
 
RefObjectType * operator= (RefObjectType *pPtr)
 
RefObjectType * operator= (const SelfType &other)
 
template<class OtherInterface >
RefObjectType * operator= (OtherInterface *pPtr)
 
template<class OtherInterface , class OtherSafeRefObjectType , class OtherLifeTimePolicy >
RefObjectType * operator= (const RefPtr< OtherInterface, OtherSafeRefObjectType, OtherLifeTimePolicy > &other)
 
bool operator! () const
 
bool operator< (RefObjectType *pPtr) const
 
bool operator< (const SelfType &pPtr) const
 
bool operator== (RefObjectType *pPtr) const
 
bool operator== (const SelfType &pPtr) const
 
bool operator!= (RefObjectType *pPtr) const
 
bool operator!= (const SelfType &pPtr) const
 

Additional Inherited Members

- Static Public Member Functions inherited from MaxHeapOperators
static UtilExport voidoperator new (size_t size)
 Standard new operator used to allocate objects If there is insufficient memory, an exception will be thrown. More...
 
static UtilExport voidoperator new (size_t size, const std::nothrow_t &e)
 Standard new operator used to allocate objects if there is insufficient memory, NULL will be returned. More...
 
static UtilExport voidoperator new (size_t size, const char *filename, int line)
 New operator used to allocate objects that takes the filename and line number where the new was called If there is insufficient memory, an exception will be thrown. More...
 
static UtilExport voidoperator new (size_t size, int block_type, const char *filename, int line)
 New operator used to allocate objects that takes the type of memory, filename and line number where the new was called If there is insufficient memory, an exception will be thrown. More...
 
static UtilExport voidoperator new (size_t size, const std::nothrow_t &e, const char *filename, int line)
 New operator used to allocate objects that takes the filename and line number where the new was called If there is insufficient memory, NULL will be returned. More...
 
static UtilExport voidoperator new (size_t size, unsigned long flags)
 New operator used to allocate objects that takes extra flags to specify special operations If there is insufficient memory, an exception will be thrown. More...
 
static UtilExport voidoperator new (size_t size, const std::nothrow_t &e, unsigned long flags)
 New operator used to allocate objects that takes extra flags to specify special operations If there is insufficient memory, NULL will be returned. More...
 
static UtilExport voidoperator new[] (size_t size)
 New operator used to allocate arrays of objects If there is insufficient memory, an exception will be thrown. More...
 
static UtilExport voidoperator new[] (size_t size, const std::nothrow_t &e)
 New operator used to allocate arrays of objects If there is insufficient memory, NULL will be returned. More...
 
static UtilExport voidoperator new[] (size_t size, const char *filename, int line)
 New operator used to allocate arrays of objects If there is insufficient memory, an exception will be thrown. More...
 
static UtilExport voidoperator new[] (size_t size, int block_type, const char *filename, int line)
 New operator used to allocate arrays of objects. More...
 
static UtilExport voidoperator new[] (size_t size, const std::nothrow_t &e, const char *filename, int line)
 New operator used to allocate arrays of objects If there is insufficient memory, NULL will be returned. More...
 
static UtilExport voidoperator new[] (size_t size, unsigned long flags)
 New operator used to allocate arrays of objects If there is insufficient memory, an exception will be thrown. More...
 
static UtilExport voidoperator new[] (size_t size, const std::nothrow_t &e, unsigned long flags)
 New operator used to allocate arrays of objects If there is insufficient memory, NULL will be returned. More...
 
static UtilExport void operator delete (void *ptr)
 Standard delete operator used to deallocate an object If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport void operator delete (void *ptr, const std::nothrow_t &e)
 Standard delete operator used to deallocate an object If the pointer is invalid, nothing will happen. More...
 
static UtilExport void operator delete (void *ptr, const char *filename, int line)
 Delete operator used to deallocate an object that takes the filename and line number where the delete was called If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport void operator delete (void *ptr, int block_type, const char *filename, int line)
 Delete operator used to deallocate an object that takes the type of memory, filename and line number where the delete was called If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport void operator delete (void *ptr, const std::nothrow_t &e, const char *filename, int line)
 Delete operator used to deallocate an object that takes the filename and line number where the delete was called If the pointer is invalid, nothing will happen. More...
 
static UtilExport void operator delete (void *ptr, unsigned long flags)
 Delete operator used to deallocate an object that takes extra flags to specify special operations If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport void operator delete (void *ptr, const std::nothrow_t &e, unsigned long flags)
 Delete operator used to deallocate an object that takes extra flags to specify special operations If the pointer is invalid, nothing will happen. More...
 
static UtilExport void operator delete[] (void *ptr)
 Standard delete operator used to deallocate an array of objects If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport void operator delete[] (void *ptr, const std::nothrow_t &e)
 Standard delete operator used to deallocate an array of objects If the pointer is invalid, nothing will happen. More...
 
static UtilExport void operator delete[] (void *ptr, const char *filename, int line)
 Delete operator used to deallocate an array of objects that takes the filename and line number where the delete was called If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport void operator delete[] (void *ptr, int block_type, const char *filename, int line)
 Delete operator used to deallocate an array of objects that takes the type of memory, filename and line number where the delete was called If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport void operator delete[] (void *ptr, const std::nothrow_t &e, const char *filename, int line)
 Delete operator used to deallocate an array of objects that takes the filename and line number where the delete was called If the pointer is invalid, nothing will happen. More...
 
static UtilExport void operator delete[] (void *ptr, unsigned long flags)
 Delete operator used to deallocate an array of objects that takes extra flags to specify special operations If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport void operator delete[] (void *ptr, const std::nothrow_t &e, unsigned long flags)
 Delete operator used to deallocate an array of objects that takes extra flags to specify special operations If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport voidoperator new (size_t size, void *placement_ptr)
 Placement new operator. More...
 
static UtilExport void operator delete (void *ptr, void *placement_ptr)
 Placement delete operator. More...
 
static UtilExport voidaligned_malloc (size_t size, size_t alignment)
 Allocates memory on a specified alignment boundary. More...
 
static UtilExport voidaligned_realloc (void *ptr, size_t size, size_t alignment)
 Reallocates memory on a specified alignment boundary. More...
 
static UtilExport void aligned_free (void *ptr)
 Frees a block of memory that was allocated with aligned_malloc/aligned_realloc. More...
 

Detailed Description

template<class RefObjectType, class SafeRefObjectType = SafeReferenceObjectProxy<RefObjectType>, class LifeTimePolicy = DefaultLifeTimePolicy<RefObjectType>>
class MaxSDK::Graphics::RefPtr< RefObjectType, SafeRefObjectType, LifeTimePolicy >

This template class act similar as CComPtr.

Using RefPtr<RefObjectType> will be much safer than RefObjectType* since this template class handles the AddRef and Release in its constructor and destructor.

Usage Example: class DerivedRefObject : public ARefObject { public: void foo(); };

Use DerivedRefObject without RefPtr: { DerivedRefObject* pObject = new DerivedRefObject; pObject->AddRef(); // Add reference pObject->foo(); pObject->Release(); // release the object pObject = NULL; }

Use DerivedRefObject with RefPtr: { RefPtr<DerivedRefObject> pObject = new DerivedRefObject; pObject->foo(); don't need to worry about deleting this object }

More RefPtr examples: RefPtr<DerivedRefObject> pObject2 = new DerivedRefObject; RefPtr<DerivedRefObject> pObject3 = pObject2; pObject2 = NULL; // pObject3 is still valid. pObject3->foo(); pObject3 = NULL; // Release the last reference to the object and the object is destroyed automatically.

Member Typedef Documentation

◆ ObjectType

typedef RefObjectType ObjectType

◆ SelfType

typedef RefPtr<RefObjectType, SafeRefObjectType, LifeTimePolicy> SelfType

Constructor & Destructor Documentation

◆ RefPtr() [1/5]

RefPtr ( )
inline
156  {
157  mpObject = NULL;
158  }
#define NULL
Definition: autoptr.h:18

◆ RefPtr() [2/5]

RefPtr ( RefObjectType *  pPtr)
inline
161  {
162  mpObject = pPtr;
163  if (mpObject != NULL)
164  {
165  LifeTimePolicy::AddRef(mpObject);
166  }
167  }

◆ RefPtr() [3/5]

RefPtr ( const SelfType other)
inline
170  {
171  mpObject = other.mpObject;
172  if (mpObject != NULL)
173  {
174  LifeTimePolicy::AddRef(mpObject);
175  }
176  }

◆ RefPtr() [4/5]

RefPtr ( const RefPtr< OtherInterface, OtherSafeRefObjectType, OtherLifeTimePolicy > &  other)
inlineexplicit
183  {
184  if (other == NULL)
185  {
186  mpObject = NULL;
187  return;
188  }
189  mpObject = dynamic_cast<RefObjectType*>(other.GetPointer());
190  if (NULL != mpObject)
191  {
192  LifeTimePolicy::AddRef(mpObject);
193  }
194  }

◆ RefPtr() [5/5]

RefPtr ( OtherInterface *  pPtr)
inlineexplicit
198  {
199  if (pPtr == NULL)
200  {
201  mpObject = NULL;
202  }
203  else
204  {
205  mpObject = dynamic_cast<RefObjectType*>(pPtr);
206  }
207  if (NULL != mpObject)
208  {
209  LifeTimePolicy::AddRef(mpObject);
210  }
211  }

◆ ~RefPtr()

~RefPtr ( )
inline
214  {
215  Release();
216  }
void Release()
Definition: IRefObject.h:225

Member Function Documentation

◆ Swap()

void Swap ( SelfType other)
inline
219  {
220  RefObjectType* pTemp = mpObject;
221  mpObject = other.mpObject;
222  other.mpObject = pTemp;
223  }

◆ Release()

void Release ( )
inline
226  {
227  RefObjectType* pTemp = mpObject;
228  if (mpObject)
229  {
230  mpObject = NULL;
231  LifeTimePolicy::Release(pTemp);
232  }
233  }

◆ operator RefObjectType *()

operator RefObjectType * ( ) const
inline
236  {
237  return mpObject;
238  }

◆ operator*()

RefObjectType& operator* ( ) const
inline
241  {
242  return *mpObject;
243  }

◆ GetPointer()

RefObjectType* GetPointer ( ) const
inline
246  {
247  return mpObject;
248  }

◆ operator->()

SafeRefObjectType* operator-> ( ) const
inline
251  {
252  return (SafeRefObjectType*)mpObject;
253  }

◆ operator=() [1/4]

RefObjectType* operator= ( RefObjectType *  pPtr)
inline
256  {
257  if (pPtr != NULL)
258  {
259  LifeTimePolicy::AddRef(pPtr);
260  }
261  if (mpObject)
262  {
263  LifeTimePolicy::Release(mpObject);
264  }
265  mpObject = pPtr;
266  return pPtr;
267  }

◆ operator=() [2/4]

RefObjectType* operator= ( const SelfType other)
inline
270  {
271  RefObjectType* pPtr = other.mpObject;
272  if (pPtr != NULL)
273  {
274  LifeTimePolicy::AddRef(pPtr);
275  }
276  if (mpObject)
277  {
278  LifeTimePolicy::Release(mpObject);
279  }
280  mpObject = pPtr;
281  return pPtr;
282  }

◆ operator=() [3/4]

RefObjectType* operator= ( OtherInterface *  pPtr)
inline
286  {
287  RefObjectType* pBackup = mpObject;
288  if (pPtr != NULL)
289  {
290  mpObject = dynamic_cast<RefObjectType*>(pPtr);
291  }
292  else
293  {
294  mpObject = NULL;
295  }
296  if (NULL != mpObject)
297  {
298  LifeTimePolicy::AddRef(mpObject);
299  }
300 
301  if (pBackup != NULL)
302  {
303  LifeTimePolicy::Release(pBackup);
304  }
305  return mpObject;
306  }

◆ operator=() [4/4]

RefObjectType* operator= ( const RefPtr< OtherInterface, OtherSafeRefObjectType, OtherLifeTimePolicy > &  other)
inline
313  {
314  RefObjectType* pBackup = mpObject;
315  if (other != NULL)
316  {
317  mpObject = dynamic_cast<RefObjectType*>(other.GetPointer());
318  }
319  else
320  {
321  mpObject = NULL;
322  }
323  if (NULL != mpObject)
324  {
325  LifeTimePolicy::AddRef(mpObject);
326  }
327 
328  if (pBackup != NULL)
329  {
330  LifeTimePolicy::Release(pBackup);
331  }
332  return mpObject;
333  }

◆ operator!()

bool operator! ( ) const
inline
336  {
337  return mpObject == NULL;
338  }

◆ operator<() [1/2]

bool operator< ( RefObjectType *  pPtr) const
inline
341  {
342  return mpObject < pPtr;
343  }

◆ operator<() [2/2]

bool operator< ( const SelfType pPtr) const
inline
346  {
347  return mpObject < pPtr.mpObject;
348  }

◆ operator==() [1/2]

bool operator== ( RefObjectType *  pPtr) const
inline
351  {
352  return mpObject == pPtr;
353  }

◆ operator==() [2/2]

bool operator== ( const SelfType pPtr) const
inline
356  {
357  return mpObject == pPtr.mpObject;
358  }

◆ operator!=() [1/2]

bool operator!= ( RefObjectType *  pPtr) const
inline
361  {
362  return mpObject != pPtr;
363  }

◆ operator!=() [2/2]

bool operator!= ( const SelfType pPtr) const
inline
366  {
367  return mpObject != pPtr.mpObject;
368  }