2011-03-17 2 views
0

나는 아래쪽에 다음 파일로 작업해야합니다. 문제는 내가 사용해야하는 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 임 다른 액세스 할 수있는 데이터의 블록을 소유

클래스 * 버퍼. 각 버퍼 *가 BufferBackend 의해 개최 동일한 데이터 블록 * 참조 공유 포인터를 포함 할 수 있도록 BufferBackend를 갖는 점이다. 이 백엔드는 자동으로 멀리 는 * 한 번에 대한 모든 참조가 사라 갈 것입니다. * *이 클래스의 하위 클래스는 특정 * 메모리 할당/할당 해제 기능을 사용하여 만들어야합니다. / 클래스 BufferBackend {
/
친구 */ 공공 : 친구 클래스 버퍼;

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

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

virtual ~BufferBackend(); 

// BufferBackend(); Bufferbackend.cpp

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.h"를 포함

using namespace ahs; 

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

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

답변

2

BufferSptr은 공유 포인터입니다 : 당신이 그것에 버퍼를 (당신이 버퍼를 만들어야합니다)를 할당해야 . 버프는 다른 BufferSptr (다른 공유 포인터) 인 경우 , 나는 시도 것 :

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

또는

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

서브 버퍼가 buffersptr를 반환하지만 난 그것에서 데이터를 얻을하지 않습니다 ... 난 경우 내 플레이어에게 전달 해독 할 것이없는 것처럼 보이고, 버퍼의 내용을 인쇄했다. 비어있다. ... 어떤 제안이든 ... memcpy로도 쓸 수 없다 ... – fammi

+0

이것은 Buffer :: Buffer (Buffer * container, int offs, int sz)의 생성자가 잘못된 장소에서 데이터를 가져 오는 것일 수 있습니다. 해야하지 mpData = & mpData [끄기]; be mpData = & (컨테이너 -> mpData [끄기]); ?? –

+0

그것은 문제를 해결하지 않습니다, 나는 게시물을 편집하고 ** bufferbackend ** 파일도 넣어 ... 좀 봐 ... 감사합니다 – fammi