chxavbuffer.h
上传用户:dangjiwu
上传日期:2013-07-19
资源大小:42019k
文件大小:6k
- /*============================================================================*
- *
- * (c) 1995-2002 RealNetworks, Inc. Patents pending. All rights reserved.
- *
- *============================================================================*/
-
- #ifndef _UT_BUFFER_H
- #define _UT_BUFFER_H
- #include "chxbody.h"
- #include "chxsmartptr.h"
- #include "chxmakesmartptr.h"
- class CHXAvBuffer;
- // Symbian
- #include <e32std.h>
- #define NEW new (ELeave)
- // CHXAvBufferRep:
- // Underlying (shared) buffer representation.
- class CHXAvBufferRep : public CHXBody {
- private:
- friend class CHXAvBuffer;
- friend class CHXSmartCPtr<CHXAvBufferRep>;
- CHXAvBufferRep(int size);
- ~CHXAvBufferRep();
- CHXAvBufferRep(const CHXAvBufferRep& rep);
- CHXAvBufferRep& operator=(const CHXAvBufferRep& rep);
- void Reaize(int size);
- int Length() const;
- int MaxLength() const;
- void Copy(const char* buf, int size);
- void Append(const char* buf, int size);
- void Grow(int size);
- const char* Base() const;
- char *Base();
- char* m_pData; // Data
- int m_len; // Current Length
- int m_maxLen; // Maximum Length
- };
- MakeSmartPtr(CHXAvBufferRep);
- // CHXAvBuffer:
- // Implements a referenced counted buffer. Copy construction and
- // assignment result in a shared copy of the underlying buffer.
- // This is implemented by the utSmartPtr semantics of the underlying
- // buffer rep. CHXAvBuffer's should always be used as objects, not
- // pointers.
- class CHXAvBuffer {
- public:
- CHXAvBuffer();
- // Allocate buffer, with max size
- CHXAvBuffer(int size);
- // Allocate buffer, and initialize
- CHXAvBuffer(const char* pData, int size);
- CHXAvBuffer(const unsigned char* pData, int size);
- // Deep copy. Shared references
- // do not change.
- void Copy(const CHXAvBuffer& buffer);
- // Shallow operations: Shared references change
- void Set(const CHXAvBuffer& buffer);
- void Set(const unsigned char*, int size);
- void Set(const char* pData, int size);
- void Append(const CHXAvBuffer& buffer);
- void Append(const unsigned char*, int size);
- void Append(const char* pData, int size);
- // Current length of buffer
- int Length() const;
- // Maximum length of buffer
- int MaxLength() const;
- // MaxLength will increase if necessary
- void Resize(int size);
- // These leak the pointer and break the
- // interface, but are required to to get a
- // handle on the raal data. The data pointed
- // to may go away before the pointer does.
- // The pointers should never be deleted.
- operator const char*() const;
- operator char*() const;
- operator const unsigned char*() const;
- operator unsigned char*() const;
- private:
- CHXAvBufferRepPtr m_pRep; // The real stuff
- };
- inline
- CHXAvBufferRep::CHXAvBufferRep(int size)
- : m_pData(0),
- m_len(0),
- m_maxLen(0)
- {
- if (size > 0)
- Grow(size);
- }
- inline
- CHXAvBufferRep::~CHXAvBufferRep()
- {
- delete [] m_pData;
- }
- inline
- int CHXAvBufferRep::Length() const
- {
- return m_len;
- }
- inline
- int CHXAvBufferRep::MaxLength() const
- {
- return m_maxLen;
- }
- inline
- const char* CHXAvBufferRep::Base() const
- {
- return m_pData;
- }
- inline
- char* CHXAvBufferRep::Base()
- {
- return m_pData;
- }
- //
- // CHXAvBuffer
- //
- inline
- CHXAvBuffer::CHXAvBuffer()
- : m_pRep(NEW CHXAvBufferRep(0))
- {}
- inline
- CHXAvBuffer::CHXAvBuffer(int size)
- : m_pRep(NEW CHXAvBufferRep(size))
- {}
-
- inline
- CHXAvBuffer::CHXAvBuffer(const char* pData, int size)
- : m_pRep(NEW CHXAvBufferRep(size))
- {
- m_pRep->Copy(pData, size);
- }
- inline
- CHXAvBuffer::CHXAvBuffer(const unsigned char* pData, int size)
- : m_pRep(NEW CHXAvBufferRep(size))
- {
- m_pRep->Copy((const char*) pData, size);
- }
- // CHXAvBuffer::Copy:
- // Deep copy. If there is another reference to the buffer,
- // Let it go, and create a new one for this CHXAvBuffer
- inline
- void CHXAvBuffer::Copy(const CHXAvBuffer& buffer)
- {
- if (m_pRep->refcount() > 1)
- m_pRep = NEW CHXAvBufferRep(buffer.m_pRep->Length());
- m_pRep->Copy(buffer.m_pRep->Base(), buffer.m_pRep->Length());
- }
- // CHXAvBuffer::Set
- // Shallow set operations. All reference are changed
- inline
- void CHXAvBuffer::Set(const char* pData, int size)
- {
- m_pRep->Copy(pData, size);
- }
- inline
- void CHXAvBuffer::Set(const unsigned char* pData, int size)
- {
- m_pRep->Copy((const char*) pData, size);
- }
- inline
- void CHXAvBuffer::Set(const CHXAvBuffer& buffer)
- {
- if (m_pRep != buffer.m_pRep) // check for self copy
- m_pRep->Copy(buffer.m_pRep->Base(), buffer.m_pRep->Length());
- }
- // CHXAvBuffer::Append
- // Shallow append operations. All reference are changed
- inline
- void CHXAvBuffer::Append(const char* pData, int size)
- {
- m_pRep->Append(pData, size);
- }
- inline
- void CHXAvBuffer::Append(const unsigned char* pData, int size)
- {
- m_pRep->Append((const char*) pData, size);
- }
- inline
- void CHXAvBuffer::Append(const CHXAvBuffer& buffer)
- {
- CHXAvBufferRepPtr p = buffer.m_pRep;
- if (m_pRep == buffer.m_pRep) // self append
- p = NEW CHXAvBufferRep(*buffer.m_pRep);
- m_pRep->Append(p->Base(), p->Length());
- }
- inline
- void CHXAvBuffer::Resize(int size)
- {
- if (size > m_pRep->MaxLength())
- m_pRep->Grow(size);
- m_pRep->m_len = size;
- }
- inline
- int CHXAvBuffer::Length() const
- {
- return m_pRep->Length();
- }
- inline
- int CHXAvBuffer::MaxLength() const
- {
- return m_pRep->MaxLength();
- }
- inline
- CHXAvBuffer::operator const char*() const
- {
- return m_pRep->Base();
- }
- inline
- CHXAvBuffer::operator char*() const
- {
- return m_pRep->Base();
- }
- inline
- CHXAvBuffer::operator const unsigned char*() const
- {
- return (const unsigned char*) m_pRep->Base();
- }
- inline
- CHXAvBuffer::operator unsigned char*() const
- {
- return (unsigned char*) m_pRep->Base();
- }
- #endif // _UT_BUFFER_H