gpt4 book ai didi

c++ - 子缓冲区实现

转载 作者:行者123 更新时间:2023-11-28 03:51:31 27 4
gpt4 key购买 nike

我必须在底部使用以下文件。问题是我必须使用的 BufferSptr。我可以通过执行以下操作创建 BufferSptr。

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是

拥有一 block 数据的类,可以被不同的访问 *缓冲器。拥有 BufferBackend 的关键在于每个 Buffer * 可以包含指向同一数据 block 的共享指针 * 通过 BufferBackend。然后这个后端会自动消失 * 一旦对它的所有引用都消失了。 * * 应该创建此类的子类,使用特定的 * 内存分配/释放函数。 / 类 BufferBackend {
/
好友 */ 民众: friend 类缓冲区;

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

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

virtual ~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 */

缓冲后端.cpp

包含“BufferBackend.h”

using namespace ahs;

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

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

最佳答案

BufferSptr 只是共享指针:您必须为其分配一个缓冲区(您必须创建缓冲区)。如果 buff 是另一个 BufferSptr(另一个共享指针),那么我会尝试:

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

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

关于c++ - 子缓冲区实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5336591/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com