Subbuffer help with implementation
<p>I posted today to ask for some help with subbuffer implementation. 
I am working with the files Buffer.h and Buffer.cpp at the bottom.
The thing is that i have to use BufferSptr to send it to my media player.</p>

<p>I create a BufferSptr as follows</p>

<pre><code>BufferSptr buff (new Buffer (pMediaData-&gt;Size()) );
</code></pre>

<p>And a Subbuffer for it as follows, to point to some specific data.</p>

<pre><code>BufferSptr subbuffer (buff-&gt;CreateSubbuffer(0,100));
</code></pre>

<p>The problem occurs as the subbuffer actually doesnot contain anything at all. I do not know what it points to as I dont get anything in my buffer.
The point is that i want a specific byte range in my BufferSptr.
Thanks</p>

<p>buffer.h file is</p>

<pre><code>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&lt;Buffer&gt; BufferSptr;
 typedef shared_ptr&lt;const Buffer&gt; 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&amp; 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&lt;BufferBackend&gt; 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_H */
</code></pre>

<p>Buffer.cpp is as follows</p>

<pre><code>#include "Buffer.h"
#include "BufferBackend.h"

using namespace ahs;

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

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

Buffer::Buffer(Buffer *container, int offs, int sz)
{
assert(offs + sz &lt;= container-&gt;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-&gt;mpBackend;
    muSize = sz;
    mpData = &amp;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&lt;Buffer *&gt;(this)-&gt;CreateSubbuffer(offs, sz);
}
</code></pre>

<p>bufferbackend.h is </p>

<pre><code>namespace ahs {

class Buffer;

/** BufferBackend
 *
 *  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&lt;BufferBackend&gt; 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&amp;);
//BufferBackend operator=(const BufferBackend&amp;);

/* 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 */
</code></pre>
