开发者

SubBuffer Implementation

I have to work with the following files at the bottom. The problem is BufferSptr which i have to use. I can create a BufferSptr by doing the following.

BufferSptr buff (new Buffer (pMediaData->Size()) );

I can also access and pass it to the functions in a media player where it is being used. What i want to do is pass a BufferSptr but access not all of it but some partial content. I see the files below define some way to do it but it is not working for me

for eg i tried this, but does not work.

BufferSptr subbuffer (&buff,0,100);

So what i want is to use BufferSptr with partial content. thanks

The Buffer.h class is

namespace ahs {

/** A Buffer provides a partial view to a block of memory.  For any
 *  given Buffer, subbuffers can be created that provide a partial view
 *  of the original Buffer contents.  If all the Buffers to a block of
 *  memory are deleted, the block itself is freed automatically.
 */
class Buffer
{
/* Data types */
public:
typedef shared_ptr<Buffer> BufferSptr;
typedef shared_ptr<const Buffer> ConstBufferSptr;

/* Member functions */
public:
/** Create a new buffer of the given size (in Bytes) using the default
 *  backend */
Buffer( size_t size );

/** Create a new buffer using a provided backend store */
Buffer( const BufferBackendSptr& pBackend );

/** Create a subbuffer */
Buffer( Buffer* container, int offs, int sz );

/** Create a subbuffer (equivalent to the constructor based method) */
Buffer* CreateSubbuffer( int offs, int sz );
const Buffer* CreateConstSubbuffer( int offs, int sz ) const;

/** Get a pointer to the data */
void* GetData();
const void* GetData() const;

/** Returns the size of the buffer data */
size_t Size() const;

/* Member data */
protected:
/** Pointer to the Backend
 *
 *  We use this to keep a reference around, making sure the data does
 *  not get thrown away.  The other purpose is to create subbuffers.
 */
shared_ptr<BufferBackend> mpBackend;

/** Pointer to the data of this buffer
 *
 *  In the general case, pData points somewhere to the interior of the
 *  data block owned by pBackend.
 */
char*   mpData;

/** The buffer size */
size_t  muSize;

};

typedef Buffer::BufferSptr BufferSptr;
typedef Buffer::ConstBufferSptr ConstBufferSptr;

inline const void* Buffer::GetDa开发者_如何学运维ta() const
{
return mpData;
}

inline void* Buffer::GetData()
{
return mpData;
}

inline size_t Buffer::Size() const
{
return muSize;
}

} // end namespace ahs

#endif

Buffer.cpp is

using namespace ahs;

Buffer::Buffer(size_t sz) : mpBackend(new BufferBackend(sz)),
                        mpData(mpBackend->mpBlock),
                        muSize(sz)
{
}

Buffer::Buffer( const BufferBackendSptr& pBackend )
: mpBackend( pBackend )
, mpData( mpBackend->mpBlock )
, muSize( mpBackend->muSize )
{
}

Buffer::Buffer(Buffer *container, int offs, int sz)
{
assert(offs + sz <= container->muSize);

#if 0 // TODO: Premature optimization, either enable or take out.
if(sz == 0)
{
    /* Special case: Try to avoid holding an unnecessary reference
       to the backend. */
    muSize = 0;
    mpBackend = (BufferBackend *)0;
    mpData = 0;
}
else
#endif
{
    mpBackend = container->mpBackend;
    muSize = sz;
    mpData = &mpData[offs];
}
}

Buffer* Buffer::CreateSubbuffer(int offs, int sz)
{
return new Buffer(this, offs, sz);
}

const Buffer* Buffer::CreateConstSubbuffer(int offs, int sz) const
{
return const_cast<Buffer *>(this)->CreateSubbuffer(offs, sz);
}

Bufferbackend.h is

Class that owns a block of data which can be accessed by different * Buffers. The point of having a BufferBackend is so that each Buffer * can contain a shared pointer referring to the same data block held * by a BufferBackend. This Backend will then automatically go away * once all references to it disappear. * * Subclasses of this class should be created that use specific * memory allocation/deallocation functions. / class BufferBackend {

/ Friends */ public: friend class Buffer;

/* Data types */
public:
typedef shared_ptr<BufferBackend> BufferBackendSptr;

/* Member functions */
public:
BufferBackend( size_t sz );

virtual ~BufferBackend();

// BufferBackend( );

protected:
/** Virtual function to allocate memory */
 void Allocate();

/** Virtual function to deallocate memory */
 void Deallocate();

private:
/* Disallow copying or assignment */
BufferBackend(const BufferBackend&);
BufferBackend operator=(const BufferBackend&);

/* Member data */
protected:
char*   mpBlock;
size_t  muSize;

};

typedef BufferBackend::BufferBackendSptr BufferBackendSptr;

inline BufferBackend::BufferBackend( size_t sz )
: muSize( sz )
{
Allocate();
}

inline BufferBackend::~BufferBackend()
{
Deallocate();
}

} // end namespace ahs

#endif /* BUFFER_BACKEND_H */

Bufferbackend.cpp

include "BufferBackend.h"

using namespace ahs;

void BufferBackend::Allocate()
{
mpBlock = new char[ muSize ];
}

void BufferBackend::Deallocate()
{
delete[] mpBlock;
}


BufferSptr is just the shared pointer: you have to assign a buffer to it (you have to create the buffer). If buff is another BufferSptr (another shared pointer), then I would try:

BufferSptr subbuffer (buff->CreateSubbuffer(0,100));

or

BufferSptr subbuffer (new Buffer(buff.get(), 0,100));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜