Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Protected Attributes
nsAutoBuffer< T, sz > Class Template Reference

A buffer which will use stack space if the requested size will fit in the stack buffer and allocate from the heap if not. More...

#include <nsAutoBuffer.h>

Collaboration diagram for nsAutoBuffer< T, sz >:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 nsAutoBuffer ()
 ~nsAutoBuffer ()
PRBool EnsureElemCapacity (PRInt32 inElemCapacity)
PRBool AddElemCapacity (PRInt32 inElemCapacity)
Tget () const
PRInt32 GetElemCapacity () const

Protected Attributes

TmBufferPtr
T mStackBuffer [sz]
PRInt32 mCurElemCapacity

Detailed Description

template<class T, PRInt32 sz>
class nsAutoBuffer< T, sz >

A buffer which will use stack space if the requested size will fit in the stack buffer and allocate from the heap if not.

Below is a usage example :

typedef nsAutoBuffer<PRUnichar, 256> nsAutoUnicharBuffer;

nsAutoUnicharBuffer buffer;

if (!buffer.EnsureElemCapacity(initialLength)) return NS_ERROR_OUT_OF_MEMORY;

PRUnichar *unicharPtr = buffer.get();

// add PRUnichar's to the buffer pointed to by |unicharPtr| as long as // the number of PRUnichar's is less than |intialLength|

// increase the capacity if (!buffer.AddElemCapacity(extraLength)) return NS_ERROR_OUT_OF_MEMORY

unicharPtr = buffer.get() + initialLength;

//continue to add PRUnichar's....

Definition at line 74 of file nsAutoBuffer.h.


Constructor & Destructor Documentation

template<class T, PRInt32 sz>
nsAutoBuffer< T, sz >::nsAutoBuffer ( ) [inline]

Definition at line 77 of file nsAutoBuffer.h.

template<class T, PRInt32 sz>
nsAutoBuffer< T, sz >::~nsAutoBuffer ( ) [inline]

Definition at line 83 of file nsAutoBuffer.h.

  {
    if (mBufferPtr != mStackBuffer)
      nsMemory::Free(mBufferPtr);
  }

Member Function Documentation

template<class T, PRInt32 sz>
PRBool nsAutoBuffer< T, sz >::AddElemCapacity ( PRInt32  inElemCapacity) [inline]

Definition at line 112 of file nsAutoBuffer.h.

  {
    return EnsureElemCapacity(mCurElemCapacity + inElemCapacity);
  }

Here is the call graph for this function:

template<class T, PRInt32 sz>
PRBool nsAutoBuffer< T, sz >::EnsureElemCapacity ( PRInt32  inElemCapacity) [inline]

Definition at line 89 of file nsAutoBuffer.h.

  {
    if (inElemCapacity <= mCurElemCapacity)
      return PR_TRUE;
    
    T* newBuffer;

    if (mBufferPtr != mStackBuffer)
      newBuffer = (T*)nsMemory::Realloc((void *)mBufferPtr, inElemCapacity * sizeof(T));
    else 
      newBuffer = (T*)nsMemory::Alloc(inElemCapacity * sizeof(T));

    if (!newBuffer)
      return PR_FALSE;

    if (mBufferPtr != mStackBuffer)
      nsMemory::Free(mBufferPtr);

    mBufferPtr = newBuffer; 
    mCurElemCapacity = inElemCapacity;
    return PR_TRUE;
  }

Here is the call graph for this function:

Here is the caller graph for this function:

template<class T, PRInt32 sz>
T* nsAutoBuffer< T, sz >::get ( ) const [inline]

Definition at line 117 of file nsAutoBuffer.h.

{ return mBufferPtr; }
template<class T, PRInt32 sz>
PRInt32 nsAutoBuffer< T, sz >::GetElemCapacity ( ) const [inline]

Definition at line 118 of file nsAutoBuffer.h.

{ return mCurElemCapacity;  }

Member Data Documentation

template<class T, PRInt32 sz>
T* nsAutoBuffer< T, sz >::mBufferPtr [protected]

Definition at line 122 of file nsAutoBuffer.h.

template<class T, PRInt32 sz>
PRInt32 nsAutoBuffer< T, sz >::mCurElemCapacity [protected]

Definition at line 124 of file nsAutoBuffer.h.

template<class T, PRInt32 sz>
T nsAutoBuffer< T, sz >::mStackBuffer[sz] [protected]

Definition at line 123 of file nsAutoBuffer.h.


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