我必須使用底部的以下文件。 問題是我必須使用BufferSptr。 我可以通過執行以下操作來創建BufferSptr。SubBuffer實現
BufferSptr buff (new Buffer (pMediaData->Size()));
我也可以訪問它並將其傳遞給正在使用它的媒體播放器中的功能。 我想要做的是傳遞一個BufferSptr,但訪問不是所有的,但 一些部分內容。我看到下面的文件定義了一些方法來做到這一點,但它不適合我
例如我試過這個,但不起作用。
BufferSptr subbuffer (&buff,0,100);
所以我想要的是使用BufferSptr的部分內容。感謝
的Buffer.h類是
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::GetData() const
{
return mpData;
}
inline void* Buffer::GetData()
{
return mpData;
}
inline size_t Buffer::Size() const
{
return muSize;
}
} // end namespace ahs
#endif
Buffer.cpp是
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是擁有其可以通過不同的訪問的數據的一個塊
類 *緩衝區。具有BufferBackend的要點是,每個緩衝區都可以包含一個共享指針,該指針指向由BufferBackend持有的相同數據塊 *。這個後端將會自動消失 *一旦所有對它的引用消失。 * *應使用特定的 *內存分配/釋放函數創建此類的子類。 / 類BufferBackend {
/朋友*/ 市民: 朋友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
包括 「BufferBackend.h」
using namespace ahs;
void BufferBackend::Allocate()
{
mpBlock = new char[ muSize ];
}
void BufferBackend::Deallocate()
{
delete[] mpBlock;
}
子緩衝器返回buffersptr但我不從中獲得任何數據...如果我把它傳遞給我的播放器,它的行爲就像沒有任何解碼,我也打印緩衝區的內容,它是空的....任何建議...甚至memcpy不能寫入它... – fammi 2011-03-17 12:39:00
這是因爲執行的構造函數Buffer :: Buffer(Buffer * container,int offs,int sz)可能從錯誤的地方獲取數據。不應該mpData =&mpData [offs]; be mpData =&(container-> mpData [offs]); ?? – 2011-03-17 13:24:02
它不能解決問題,我編輯了帖子,並把** bufferbackend **文件...請看看..謝謝 – fammi 2011-03-17 15:40:50