jvar
JS inspired Variant and Json
jvar::Buffer Class Reference

Detailed Description

Buffer class maintains an allocated chunk of memory. It takes care of freeing the memory when the object goes out of scope. It uses malloc/free/realloc. It can also read a file into the buffer.

Examples:
jsonparse.cpp.

#include <util.h>

Public Member Functions

void * ptr ()
const void * cptr ()
size_t size () const
void alloc (size_t size)
void reAlloc (size_t size)
void free ()
void copyFrom (Buffer &src)
void moveFrom (Buffer &src)
bool readFile (const char *filename, bool nullterm)

Member Function Documentation

void jvar::Buffer::alloc ( size_t  size)

Allocates memory. If previously allocated, the memory is freed first.

Parameters:
sizeSize in bytes

References free(), and size().

void jvar::Buffer::copyFrom ( Buffer src)

Copies the memory from the provided buffer into this buffer

Parameters:
srcBuffer to copy memory from

References ptr(), reAlloc(), and size().

const void* jvar::Buffer::cptr ( ) [inline]

Returns a const pointer to the memory

void jvar::Buffer::moveFrom ( Buffer src)

Moves the memory from the provided buffer into this buffer. The provided buffer is emptied. No actual memcpy is done.

Parameters:
srcBuffer to move memory from

References free().

void* jvar::Buffer::ptr ( ) [inline]

Returns a pointer to the memory

Referenced by copyFrom(), and readFile().

bool jvar::Buffer::readFile ( const char *  filename,
bool  nullterm 
)

Reads a file into the buffer

Parameters:
filenameName of the file to read
nulltermIf True, add a '\0' so the buffer can be used as a null-terminated string
Returns:
Success
Examples:
jsonparse.cpp.

References free(), ptr(), and reAlloc().

void jvar::Buffer::reAlloc ( size_t  size)

Realocates the buffer to be the new size. realloc() semantics are used.

Parameters:
sizeNew size in bytes

References free(), and size().

Referenced by copyFrom(), and readFile().

size_t jvar::Buffer::size ( ) const [inline]

Returns the size of the buffer

Returns:
Number of bytes

Referenced by alloc(), copyFrom(), and reAlloc().


The documentation for this class was generated from the following files:
 All Classes Functions Variables Enumerator