Expandable, Data Type-Neutral Buffer Class

CBuffer is an expandable buffer class which can expand when needed and allows insertion of data and many operation on buffer

Using the CBuffer Class

In order to use the class, simply do the following. (Remember that if any of this is unclear, I have included a demo application that will hopefully clear up any misunderstandings.
  1. Instantiate the CBuffer object
  2. Call the CBuffer Init method
  3. CBuffer:Init(int nSizeOfBuffer, int nNbrOfElements, 
      int nIncrementalGrowthValue)
  4. Insert data into the buffer by using any of the following mechanisms:
    • int CBuffer::PutData(BYTE *pBuff)
    • void CBuffer::operator+=(BYTE *pBuff)
    • int CBuffer::InserData(BYTE * pBuff,int nOffset)
  5. Retrieve data from the buffer by calling any of the following methods:
    • int CBuffer::GetData(BYTE * pBuff,int nIndex)
    • BYTE *CBuffer::GetPointer(int nIndex) const
  6. Use any of the following helper methods
    • m_Buffer.GetSize();
    • m_Buffer.GetTotalElements();
    • m_Buffer.GetStructureSize();
    • m_Buffer.GetFreeSp();
    • m_Buffer.GetFreeStSp();

Header File (lists available CBuffer methods)


#pragma once

class CBuffer 
{
public:
 CBuffer();
 virtual ~CBuffer();

 int Init(int,int,int);
 int PutData(BYTE*);
 void operator+=(BYTE *);
 
 int InserData(BYTE*,int);
 int GetData(BYTE*,int);
 int ExpandBuff(int);
 
 int GetSize();
 int GetTotalElements();
 int GetStructureSize();
 int GetFreeSp();
 int GetFreeStSp();
 
 BYTE *GetPointer(int nIndex)const;
 BYTE *CBuffer::operator[](int nIndex)const;
 
 private:
 BYTE *m_pBuff;
 int m_nStructSize;
 int m_nTotal;
 int m_nOffset;
 int m_nSize;
 int m_nStructOffset;
 int m_nIncerment;
};

Downloads

Download demo project - 14 Kb


Comments

  • stl vector

    Posted by Legacy on 01/28/2002 12:00am

    Originally posted by: stuart

    Why not use an STL vector<BYTE> and be done with it? What advantage does CBuffer add?

    Reply
  • CByteArray alternative

    Posted by Legacy on 02/14/2000 12:00am

    Originally posted by: Adrian Stanley

    I can't see that this offers much more than the existing MFC class CByteArray, which also automatically grows, and has an Append method for adding new data to the end.

    For 32 bit programs not using MFC, I'd suggest using the STL containers.

    Where CBuffer would be useful is in non MFC 16 bit code, but you'd have to remove all the dependencies on MFC first. This won't be that difficult, as it is mainly the TRACE and ASSERT macros. You might also consider replacing Win32 APIs (e.g., ZeroMemory) with the CRT equivalents (e.g., memset), then you'd have a truly portable class.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: April 22, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Database professionals — whether developers or DBAs — can often save valuable time by learning to get the most from their new or existing productivity tools. Whether you're responsible for managing database projects, performing database health checks and reporting, analyzing code, or measuring software engineering metrics, it's likely you're not taking advantage of some of the lesser-known features of Toad from Dell. Attend this live …

  • Download the Information Governance Survey Benchmark Report to gain insights that can help you further establish business value in your Records and Information Management (RIM) program and across your entire organization. Discover how your peers in the industry are dealing with this evolving information lifecycle management environment and uncover key insights such as: 87% of organizations surveyed have a RIM program in place 8% measure compliance 64% cannot get employees to "let go" of information for …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds