Original Post
Hi I have coded an adt_buffer where you can store any datatype in. This buffer contains one byte buffer and two pointer insert_offset and get_offet. insert_offset stores the address where the buffer will store data the next store function call and get_offset is where you are fetching data on the next get function call. Both these pointers move along the buffer. I’m thinking for making the buffer circular that will remove any move data operations. Please comment my buffer, will this buffer work on all CPU platforms? If one inserts a 32 bit value in the buffer should the buffer insert it at a 32 bit aligns?
#include "adt_buffer.h"
int main(void)
{
adt::buffer b(4096);
int a = 1;
b.add_string(L"hello world");
b.add_string("hello");
b.add(a);
UNICODE *str = b.get_string<UNICODE>();
MessageBoxW(0,str,str,MB_OK);
char *str2 = b.get_string<char>();
MessageBox(0,str2,str2,MB_OK);
int c = b.get_<int>();
return 0;
}
*/
#ifndef adt_buffer_h
#define adt_buffer_h
typedef int BOOL;
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
namespace adt
{
class buffer
{
public:
/*! add data
*/
template <typename TYPE>
buffer &add(TYPE item)
{
int size = sizeof(TYPE);
memcpy(m_insert_offset,&item,size);
m_insert_offset+=size;
return *this;
}
template <typename TYPE>
buffer &add_string(TYPE *item)
{
TYPE *p = item;
int size = sizeof(TYPE);
while(*p)
{++p;}
size+= ((int)(p - item))*sizeof(TYPE);
memcpy(m_insert_offset,item,size);
m_insert_offset+=size;
return *this;
}
template <typename TYPE>
TYPE get_()
{
char *p = m_get_offset;
m_get_offset+= sizeof(TYPE);
return *(TYPE *)p;
}
template <typename TYPE>
TYPE *get_string()
{
int tsize = sizeof(TYPE);
TYPE *p = (TYPE *)m_get_offset;
int size = tsize;
while(*p && p != m_end_offset)
{++p;}
size+=((int)((char *)p - m_get_offset)*tsize);
p = (TYPE *)m_get_offset;
m_get_offset+=size;
return p;
}
buffer &add(void *p,int size);
void addsize(int size);
void *insert_offset();
/*! returns pointer to the begining of the buffer
*/
void *data();
/*! returns current data capacity
*/
int sizeleft();
int datasize();
/*! returns total buffer capacity
*/
int totalsize();
buffer(char *provide_buffer,int size);
/*! allocated memory
*/
buffer(int size);
~buffer();
//protected:
BOOL alloc;
char *m_buf;
char *m_end_offset;
char *m_insert_offset;
char *m_get_offset;
int m_tsize;
};
}
#endif
#include "adt_buffer.h"
#include <memory.h>
namespace adt
{
buffer &buffer::add(void *p,int size)
{
memcpy(m_insert_offset,p,size);
m_insert_offset+=size;
return *this;
}
void buffer::addsize(int size)
{m_insert_offset+=size;}
void *buffer::insert_offset()
{return m_insert_offset;}
void *buffer::data()
{return (void *)m_buf;}
int buffer::sizeleft()
{return (m_tsize-((int)(m_insert_offset-m_buf)));}
int buffer::datasize()
{return (int)(m_insert_offset-m_buf);}
int buffer::totalsize()
{return m_tsize;}
buffer::buffer(char *provide_buffer,int size):alloc(FALSE),m_buf(provide_buffer),m_insert_offset(m_buf),m_get_offset(m_insert_offset),m_tsize(size)
{
m_end_offset = (m_buf+size);
}
buffer::buffer(int size):alloc(TRUE)
{
m_buf = new char[size];
m_get_offset = m_insert_offset = m_buf;
m_end_offset = (m_buf+size);
m_tsize = size;
}
buffer::~buffer()
{
if(alloc) delete m_buf;
}
}//end namespace adt