AVPKit
com::avpkit::ferry::RefPointer< T > Class Template Reference

This class is only useful from C++. More...

#include <RefPointer.h>

Inheritance diagram for com::avpkit::ferry::RefPointer< T >:

Public Member Functions

 RefPointer ()
 Default constructor; sets the managed pointer to 0.
 
 RefPointer (T *ptr)
 Construct a RefPointer from a RefCounted object, and take over management of the ref-count this pointer had. More...
 
 RefPointer (const RefPointer &rPtr)
 Copy another RefPointer object. More...
 
 RefPointer (RefPointer &rPtr)
 Copy and acquire. More...
 
RefPointeroperator= (const RefPointer &rPtr)
 Assignment operator to copy another ref pointer. More...
 
RefPointeroperator= (RefPointer &rPtr)
 Assignment operator to copy another ref pointer. More...
 
RefPointeroperator= (T *ptr)
 Assignment from a RefCounted* where we take over ownership of the refcount. More...
 
T * operator-> () const
 Allow people to def-ref the RefPointer() without RefCounted::acquire() the managed pointer. More...
 
 operator bool ()
 This convenience method allows the use of the following code to determine if the underlying pointer is null or not: RefPointer<Foo> p(someObj); if (p) { // underlying pointer is valid p->DoSomething(); } More...
 
virtual ~RefPointer ()
 Always release the ref-count of our underlying pointer.
 
T * get ()
 Call RefCounted::acquire() on the managed pointer and return it. More...
 
T * value ()
 Return the managed pointer without calling RefCounted::acquire() on it. More...
 
void reset (T *ptr=0, bool acquire=false)
 Reset the managed pointer, calling RefCounted::release() on the previously managed pointer first. More...
 

Detailed Description

template<class T>
class com::avpkit::ferry::RefPointer< T >

This class is only useful from C++.

If you pass a RefCounted pointer to a RefPointer it assumes you want it to manage the ref-count associated with the RefCounted pointer.

That means, whenever the RefPointer is destroyed, it will RefCounted::release() the pointer it was asked to manage. It will not call acquire() on the pointer for it's own use, but will RefCounted::acquire() it if someone calls get() for the caller to manage.

This can be a handy way to make sure RefCounted::acquire() is called for you in order to reduce the chance of leaking memory from C++ code.

Definition at line 46 of file RefPointer.h.

Constructor & Destructor Documentation

◆ RefPointer() [1/3]

template<class T >
com::avpkit::ferry::RefPointer< T >::RefPointer ( T *  ptr)
inline

Construct a RefPointer from a RefCounted object, and take over management of the ref-count this pointer had.

(i.e. it does not RefCounted::acquire() the pointer, but instead assumes it is now the owner. The person who called this method should not call RefCounted::release() on the passed in pointer once this object is managing it).

Parameters
ptrThe pointer to start managing.

Definition at line 67 of file RefPointer.h.

67  : mPtr(ptr)
68  {
69  }

◆ RefPointer() [2/3]

template<class T >
com::avpkit::ferry::RefPointer< T >::RefPointer ( const RefPointer< T > &  rPtr)
inline

Copy another RefPointer object.

Since the other RefPointer object will RefCounted::release() its ref-count upon destruction, we RefCounted::acquire() the pointer for ourselves.

Parameters
rPtrThe pointer to RefCounted::acquire() a reference from
Returns
A new object

Definition at line 80 of file RefPointer.h.

80  : mPtr(rPtr.mPtr)
81  {
82  VS_REF_ACQUIRE(mPtr);
83  }

◆ RefPointer() [3/3]

template<class T >
com::avpkit::ferry::RefPointer< T >::RefPointer ( RefPointer< T > &  rPtr)
inline

Copy and acquire.

Parameters
rPtrThe pointer to RefCounted::acquire() a reference from
Returns
A new object

Definition at line 90 of file RefPointer.h.

90  : mPtr(rPtr.mPtr)
91  {
92  VS_REF_ACQUIRE(mPtr);
93  }

Member Function Documentation

◆ get()

template<class T >
T* com::avpkit::ferry::RefPointer< T >::get ( )
inline

Call RefCounted::acquire() on the managed pointer and return it.

Returns
Return a copy of the managed pointer. We RefCounted::acquire() the pointer for the caller for the caller.

Definition at line 206 of file RefPointer.h.

207  {
208  VS_REF_ACQUIRE(mPtr);
209  return static_cast<T*>(mPtr);
210  }

Referenced by com::avpkit::core::Container::addNewStream(), com::avpkit::core::VideoPicture::getData(), and com::avpkit::core::MediaDataWrapper::unwrap().

◆ operator bool()

template<class T >
com::avpkit::ferry::RefPointer< T >::operator bool ( )
inline

This convenience method allows the use of the following code to determine if the underlying pointer is null or not: RefPointer<Foo> p(someObj); if (p) { // underlying pointer is valid p->DoSomething(); }

Returns
true if we're managing a non-null pointer

Definition at line 173 of file RefPointer.h.

174  {
175  return mPtr!=0;
176  }

◆ operator->()

template<class T >
T* com::avpkit::ferry::RefPointer< T >::operator-> ( ) const
inline

Allow people to def-ref the RefPointer() without RefCounted::acquire() the managed pointer.

To avoid the cost of a run-time check, we DON'T check for nullness of the managed pointer.

Returns
The underlying RefCounted pointer we manage, WITHOUT an extra RefCounted::acquire() call

Definition at line 153 of file RefPointer.h.

154  {
155  // we don't protect against a null defer here.
156  return static_cast<T*>(mPtr);
157  }

◆ operator=() [1/3]

template<class T >
RefPointer& com::avpkit::ferry::RefPointer< T >::operator= ( const RefPointer< T > &  rPtr)
inline

Assignment operator to copy another ref pointer.

We RefCounted::acquire() since the other side will RefCounted::release() it's own ref-count later.

Parameters
rPtrThe pointer to assign from
Returns
A reference to ourself

Definition at line 102 of file RefPointer.h.

103  {
104  reset(static_cast<T*>(rPtr.mPtr));
105  VS_REF_ACQUIRE(mPtr);
106  return *this;
107  }
void reset(T *ptr=0, bool acquire=false)
Reset the managed pointer, calling RefCounted::release() on the previously managed pointer first.
Definition: RefPointer.h:237

References com::avpkit::ferry::RefPointer< T >::reset().

◆ operator=() [2/3]

template<class T >
RefPointer& com::avpkit::ferry::RefPointer< T >::operator= ( RefPointer< T > &  rPtr)
inline

Assignment operator to copy another ref pointer.

We RefCounted::acquire() since the other side will RefCounted::release() it's own ref-count later.

Parameters
rPtrThe pointer to assign from
Returns
A reference to ourself

Definition at line 116 of file RefPointer.h.

117  {
118  reset(static_cast<T*>(rPtr.mPtr));
119  VS_REF_ACQUIRE(mPtr);
120  return *this;
121  }

References com::avpkit::ferry::RefPointer< T >::reset().

◆ operator=() [3/3]

template<class T >
RefPointer& com::avpkit::ferry::RefPointer< T >::operator= ( T *  ptr)
inline

Assignment from a RefCounted* where we take over ownership of the refcount.

This is a pretty handy way of doing: RefPointer<foo> = Obj->getFoo(); Since by convention getFoo() functions should RefCounted::acquire() the returned value for the caller, this ensures you don't accidentally leak a value.

Parameters
ptrThe pointer to take over management of
Returns
a reference to ourself

Definition at line 138 of file RefPointer.h.

139  {
140  reset(ptr);
141  return *this;
142  }

References com::avpkit::ferry::RefPointer< T >::reset().

◆ reset()

template<class T >
void com::avpkit::ferry::RefPointer< T >::reset ( T *  ptr = 0,
bool  acquire = false 
)
inline

Reset the managed pointer, calling RefCounted::release() on the previously managed pointer first.

Parameters
ptrThe new value to manage.
acquireshould we also acquire a reference? defaults to no (i.e. we take don't call RefCounted::acquire() to take over).

Definition at line 237 of file RefPointer.h.

238  {
239  VS_REF_RELEASE(mPtr);
240  mPtr = ptr;
241  if (acquire)
242  VS_REF_ACQUIRE(mPtr);
243  }

Referenced by com::avpkit::core::StreamCoder::encodeAudio(), com::avpkit::core::VideoPicture::make(), com::avpkit::ferry::RefPointer< T >::operator=(), and com::avpkit::core::VideoPicture::setData().

◆ value()

template<class T >
T* com::avpkit::ferry::RefPointer< T >::value ( )
inline

Return the managed pointer without calling RefCounted::acquire() on it.

This is how you pass values to methods expecting a RefCounted*

void someMethod(RefCounted* arg);
RefPointer<RefCounted> b = ....;
someMethod(b.value());

Returns
Return the managed pointer without calling RefCounted::acquire() on it.

Definition at line 226 of file RefPointer.h.

227  {
228  return static_cast<T*>(mPtr);
229  }

Referenced by com::avpkit::core::Container::addNewStream(), com::avpkit::core::StreamCoder::decodeAudio(), com::avpkit::core::StreamCoder::decodeVideo(), com::avpkit::core::StreamCoder::encodeAudio(), com::avpkit::core::StreamCoder::encodeVideo(), com::avpkit::core::Container::readNextPacket(), com::avpkit::core::Stream::stampOutputPacket(), com::avpkit::core::MediaDataWrapper::unwrap(), com::avpkit::core::MediaDataWrapper::wrap(), and com::avpkit::core::Container::writePacket().


The documentation for this class was generated from the following file: