jvar
JS inspired Variant and Json
jvar::ObjArray< T > Class Template Reference

Detailed Description

template<class T>
class jvar::ObjArray< T >

ObjArray is similar to stl::vector. It maintains a contiguous chunk of memory as an dynamic array of objects. It takes care of calling constructors and desctructors. It allows finding elements and inserting them in order using binary search. It allows creating objects directly on the array as well.

Examples:
misc.cpp.

#include <arr.h>

+ Inheritance diagram for jvar::ObjArray< T >:
+ Collaboration diagram for jvar::ObjArray< T >:

Public Types

enum  { FLAG_FIXEDBUF = 0x1 }
typedef int(* Compare )(const void *, const void *)

Public Member Functions

 ObjArray (Compare comp)
 ObjArray (const ObjArray &src)
 ObjArray (ObjArray &src)
ObjArrayoperator= (const ObjArray &src)
ObjArrayoperator= (const ObjArray *src)
T * insert (int pos)
T * insertCustom (int pos)
T * append ()
T * addOrModify (const T *keyelem, bool modifyfound=true)
T * add (const T *keyelem)
bool remove (const T *keyelem)
bool remove (int pos)
T * find (const T *elem)
T * get (int pos)
bool forEach (Iter< T > &iter)
bool forEachReverse (Iter< T > &iter)
void useFixedMem (void *memptr, int *countptr, int maxlen)
void clear ()
void * insert (int pos, const void *elem)
void * append (const void *elem)
void * add (const void *elem)
void * addOrModify (const void *elem, bool modifyfound=true)
bool remove (const void *elem)
void * find (const void *elem)
int length ()
bool full ()
void sort (Compare comp=NULL)
bool findPos (const void *findelem, int &pos)
void reserve (int elemcount)

Public Attributes

uint mFlags

Protected Member Functions

void copyFrom (BArray &src, bool alloconly, bool move)
bool binSearch (const void *findelem, int &pos)
void ensureAlloc (int desiredlen)

Member Enumeration Documentation

anonymous enum [inherited]
Enumerator:
FLAG_FIXEDBUF 

Array is currently using fixed buffer provided via useFixedMemory


Member Function Documentation

void* jvar::BArray::add ( const void *  elem) [inline, inherited]

Adds an element in order using the compare function

Parameters:
elemPointer to the element which will be copied
Returns:
Pointer to the element in array

References jvar::BArray::addOrModify().

template<class T>
T* jvar::ObjArray< T >::add ( const T *  keyelem) [inline]

Adds an item based to the array keeping it sorted

Parameters:
keyelemElement to add
Returns:
Pointer to the added or modifed element
void * jvar::BArray::addOrModify ( const void *  elem,
bool  modifyfound = true 
) [inherited]

Adds an element or modifies it if it already exists

Parameters:
elemPointer to element which will be copied
modifyfoundShould it be modified (true=allow, false=don't allow)
Returns:
Pointer to the element in array

References jvar::BArray::insert().

Referenced by jvar::BArray::add().

template<class T>
T* jvar::ObjArray< T >::addOrModify ( const T *  keyelem,
bool  modifyfound = true 
) [inline]

Adds a new element or modifies it if exists.

Parameters:
keyelemElement to search and add if doesn't exist or modify
modifyfoundAllow modifying the lement
Returns:
Pointer to the added or modified element

Referenced by jvar::ObjArray< int >::add().

void* jvar::BArray::append ( const void *  elem) [inline, inherited]

Appends or adds an element at the end of the array

Parameters:
elemPointer to the element which will be copied
Returns:
Pointer to the element in array

References jvar::BArray::insert(), and jvar::BArray::length().

Referenced by jvar::ObjArray< int >::append().

template<class T>
T* jvar::ObjArray< T >::append ( ) [inline]

Appends a new element at the end of the array

Returns:
Pointer to the newly created element
void jvar::BArray::clear ( ) [inherited]
void * jvar::BArray::find ( const void *  elem) [inherited]

Finds an element in array that matches the provided element

Parameters:
elemPointer to an element to search
Returns:
Pointer to the element in array
template<class T>
T* jvar::ObjArray< T >::find ( const T *  elem) [inline]

Finds the item

Parameters:
elemElement to find (only the key is used)
Returns:
Pointer to the element

Referenced by jvar::ObjArray< int >::find().

bool jvar::BArray::findPos ( const void *  findelem,
int &  pos 
) [inline, inherited]

Finds the position of an element

Parameters:
findelemPointer to an element to search
posReturns the position of the found element
Returns:
True if found, false otherwise
template<class T>
bool jvar::ObjArray< T >::forEach ( Iter< T > &  iter) [inline]

Returns an iterator to go over elements of the array in order

Parameters:
iterIterator
Returns:
Success
template<class T>
bool jvar::ObjArray< T >::forEachReverse ( Iter< T > &  iter) [inline]

Returns an iterator go over elements in reverse order

Parameters:
iterIterator
Returns:
Success
bool jvar::BArray::full ( ) [inline, inherited]

Determines if the array is at capacity.

Returns:
True if full, false otherwise

Referenced by jvar::BArray::insert().

template<class T>
T* jvar::ObjArray< T >::get ( int  pos) [inline]

Returns an element from the array

Parameters:
posPosition of retrieve the element
Returns:
Pointer to the element

Reimplemented from jvar::BArray.

Referenced by jvar::PropArray< Variant >::forEach(), jvar::ObjArray< int >::get(), jvar::PropArray< Variant >::get(), jvar::PropArray< Variant >::getKey(), and jvar::PropArray< Variant >::remove().

void * jvar::BArray::insert ( int  pos,
const void *  elem 
) [inherited]

Insert an element into the array

Parameters:
posPosition to insert at
elemPointer to element which will be copied

References jvar::BArray::FLAG_FIXEDBUF, jvar::BArray::full(), jvar::BArray::length(), and jvar::BArray::mFlags.

Referenced by jvar::BArray::addOrModify(), jvar::ObjArray< int >::addOrModify(), jvar::BArray::append(), and jvar::ObjArray< int >::insert().

template<class T>
T* jvar::ObjArray< T >::insert ( int  pos) [inline]

Inserts a new element at the provided position in the array

Parameters:
posPosition to insert
Returns:
Pointer to the newly created element

Referenced by jvar::PropArray< Variant >::addOrModify(), and jvar::ObjArray< int >::insertCustom().

template<class T>
T* jvar::ObjArray< T >::insertCustom ( int  pos) [inline]

Inserts an element at the provided position in the array. The new element is not constructed by ths call. The caller uses "inplace new" to call a non-default constructor.

Parameters:
posPosition to insert
Returns:
Pointer to the inserted element
bool jvar::BArray::remove ( const void *  elem) [inherited]

Removes an element from array that matches the provided element

Parameters:
elemPointer to an element to search
Returns:
Success
template<class T>
bool jvar::ObjArray< T >::remove ( const T *  keyelem) [inline]

Removes an item using a key

Parameters:
keyelemElement to remove (only key is used)
Returns:
Success

Referenced by jvar::ObjArray< int >::remove(), and jvar::PropArray< Variant >::remove().

template<class T>
bool jvar::ObjArray< T >::remove ( int  pos) [inline]

Removes an element from the provided position

Parameters:
posPosition to remove the item
Returns:
Success

Reimplemented from jvar::BArray.

void jvar::BArray::reserve ( int  elemcount) [inline, inherited]

Ensures that the array has at least the specified allocation

Parameters:
elemcountNumber of element to reserve
void jvar::BArray::sort ( Compare  comp = NULL) [inline, inherited]

Sorts the array

Parameters:
compCompare function (if NULL, the one from constructor is used)
void jvar::BArray::useFixedMem ( void *  memptr,
int *  countptr,
int  maxlen 
) [inherited]

Enters fixed memory mode using the provided memory

Parameters:
memptrPointer to allocated fixed memory
countptrPointer to a variable where current count is held
maxlenMaximum number of elements (size specified in constructor)

References jvar::BArray::FLAG_FIXEDBUF, jvar::Buffer::free(), and jvar::BArray::mFlags.


Member Data Documentation


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