Import XArray and XObjArray.

Create a basic unit test of C++ classes.
This commit is contained in:
jief 2020-02-23 14:21:28 +03:00
parent 0d110b4b72
commit 56ca2c3e67
18 changed files with 1127 additions and 52 deletions

View File

@ -0,0 +1,336 @@
//*************************************************************************************************
//*************************************************************************************************
//
// XArray
//
// Developed by jief666, from 1997.
//
//*************************************************************************************************
//*************************************************************************************************
#if !defined(__XARRAY_H__)
#define __XARRAY_H__
//#include "../Platform/Platform.h" // for DebugLog
VOID EFIAPI DebugLog(IN INTN DebugMode, IN CONST CHAR8 *FormatString, ...); // To avoid include Platform just for this
extern "C" {
#include <Library/MemoryAllocationLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/BaseLib.h> // for CpuDeadLoop();
}
#include "XToolsCommon.h"
#if 0
#define XArray_DBG(...) DebugLog(2, __VA_ARGS__)
#else
#define XArray_DBG(...)
#endif
template<class TYPE>
class XArray
{
protected:
TYPE *m_data;
xsize m_len;
xsize m_size;
xsize _GrowBy;
public:
void Init();
XArray() { Init(); }
XArray(const XArray<TYPE> &anArray);
const XArray<TYPE> &operator =(const XArray<TYPE> &anArray);
virtual ~XArray();
public:
const TYPE *Data() const { return m_data; }
TYPE *Data() { return m_data; }
public:
xsize Size() const { return m_size; }
xsize Length() const { return m_len; }
void SetLength(xsize l);
const TYPE& ElementAt(xsize nIndex) const;
TYPE& ElementAt(xsize nIndex);
const TYPE& operator[](xsize nIndex) const { return ElementAt(nIndex); }
TYPE& operator[](xsize nIndex) { return ElementAt(nIndex); }
const TYPE& operator[]( int nIndex) const { return ElementAt(nIndex); }
TYPE& operator[]( int nIndex) { return ElementAt(nIndex); }
operator const void *() const { return m_data; };
operator void *() { return m_data; };
operator const TYPE *() const { return m_data; };
operator TYPE *() { return m_data; };
const TYPE * operator +( int i) const { return m_data+i; };
TYPE * operator +( int i) { return m_data+i; };
const TYPE * operator +(xsize i) const { return m_data+i; };
TYPE * operator +(xsize i) { return m_data+i; };
const TYPE * operator -( int i) const { return m_data-i; };
TYPE * operator -( int i) { return m_data-i; };
const TYPE * operator -(xsize i) const { return m_data-i; };
TYPE * operator -(xsize i) { return m_data-i; };
// xsize Add(const TYPE newElement);
// TYPE AddNew();
// xsize Inserts(const TYPE &newElement, xsize pos, xsize count);
void CheckSize(xsize nNewSize);
void CheckSize(xsize nNewSize, xsize nGrowBy);
xsize AddUninitialized(xsize count); // add count uninitialzed elements
xsize Add(const TYPE newElement, xsize count = 1);
xsize AddArray(const TYPE *newElements, xsize count = 1);
xsize Insert(const TYPE newElement, xsize pos, xsize count = 1);
void Remove(const TYPE *Element);
void Remove(const TYPE &Element);
void RemoveAtIndex(xsize nIndex);
void RemoveAtIndex(int nIndex);
void Empty();
xsize IdxOf(TYPE& e) const;
bool ExistIn(TYPE& e) const { return IdxOf(e) != MAX_XSIZE; }
};
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//
// XArray
//
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
template<class TYPE>
xsize XArray<TYPE>::IdxOf(TYPE& e) const
{
xsize i;
for ( i=0 ; i<Length() ; i+=1 ) {
if ( ElementAt(i) == e ) return i;
}
return MAX_XSIZE;
}
/* Constructeur */
template<class TYPE>
void XArray<TYPE>::Init()
{
m_data = NULL;
m_size = 0;
m_len = 0;
_GrowBy = XArrayGrowByDefault;
}
/* Constructeur */
template<class TYPE>
XArray<TYPE>::XArray(const XArray<TYPE> &anArray)
{
Init();
Add(anArray.Data(), anArray.Length());
}
/* operator = */
template<class TYPE>
const XArray<TYPE> &XArray<TYPE>::operator =(const XArray<TYPE> &anArray)
{
xsize ui;
Empty();
for ( ui=0 ; ui<anArray.Length() ; ui+=1 ) AddCopy(anArray.Data() );
return *this;
}
/* Destructeur */
template<class TYPE>
XArray<TYPE>::~XArray()
{
//printf("XArray Destructor\n");
if ( m_data ) Xfree(m_data);
}
/* CheckSize() */
template<class TYPE>
void XArray<TYPE>::CheckSize(xsize nNewSize, xsize nGrowBy)
{
//XArray_DBG("CheckSize: m_len=%d, m_size=%d, nGrowBy=%d, nNewSize=%d\n", m_len, m_size, nGrowBy, nNewSize);
if ( nNewSize > m_size ) {
nNewSize += nGrowBy;
m_data = (TYPE *)Xrealloc( m_size * sizeof(TYPE), nNewSize * sizeof(TYPE), (void *)m_data);
if ( !m_data ) {
DebugLog(2, "XArray<TYPE>::CheckSize(nNewSize=%llu, nGrowBy=%llu) : Xrealloc(%d, %d, %d) returned NULL. System halted\n", nNewSize, nGrowBy, m_size, nNewSize*sizeof(TYPE), m_data);
CpuDeadLoop();
}
// memset(&_Data[_Size], 0, (nNewSize-_Size) * sizeof(TYPE)); // Could help for debugging, but zeroing in not needed.
m_size = nNewSize;
}
}
/* CheckSize() */
template<class TYPE>
void XArray<TYPE>::CheckSize(xsize nNewSize)
{
CheckSize(nNewSize, XArrayGrowByDefault);
}
/* SetLength (xsize i) */
template<class TYPE>
void XArray<TYPE>::SetLength(xsize l)
{
m_len = l;
#ifdef DEBUG
if(m_len > m_size) {
DebugLog(2, "XArray::SetLength(xsize) -> _Len > _Size");
CpuDeadLoop();
}
#endif
}
/* ElementAt() */
template<class TYPE>
TYPE &XArray<TYPE>::ElementAt(xsize index)
{
#ifdef _DEBUG
if ( index >= m_len ) {
DebugLog(2, "XArray::ElementAt(xsize) -> Operator [] : index > m_len");
CpuDeadLoop();
}
#endif
return m_data[index];
}
/* ElementAt() */
template<class TYPE>
const TYPE& XArray<TYPE>::ElementAt(xsize index) const
{
#ifdef _DEBUG
if ( index >= m_len ) {
DebugLog(2, "XArray::ElementAt(xsize) const -> Operator [] : index > m_len");
CpuDeadLoop();
}
#endif
return m_data[index];
}
/* Add(xsize) */
template<class TYPE>
xsize XArray<TYPE>::AddUninitialized(xsize count)
{
CheckSize(m_len+count);
m_len += count;
return m_len-count;
}
/* Add(TYPE, xsize) */
template<class TYPE>
xsize XArray<TYPE>::Add(const TYPE newElement, xsize count)
{
// XArray_DBG("xsize XArray<TYPE>::Add(const TYPE newElement, xsize count) -> Enter. count=%d _Len=%d _Size=%d\n", count, m_len, m_size);
xsize i;
CheckSize(m_len+count);
for ( i=0 ; i<count ; i++ ) {
m_data[m_len+i] = newElement;
}
m_len += count;
return m_len-count;
}
/* Add(TYPE *, xsize) */
template<class TYPE>
xsize XArray<TYPE>::AddArray(const TYPE *newElements, xsize count)
{
xsize i;
CheckSize(m_len+count);
for ( i=0 ; i<count ; i++ ) {
m_data[m_len+i] = newElements[i];
}
m_len += count;
return m_len-count;
}
/* Insert(TYPE &, xsize, xsize) */
template<class TYPE>
xsize XArray<TYPE>::Insert(const TYPE newElement, xsize pos, xsize count)
{
xsize i;
if ( pos < m_len ) {
CheckSize(m_len+count);
memmove(&m_data[pos], &m_data[pos+count], (m_len-pos)*sizeof(TYPE));
for ( i=0 ; i<count ; i++ ) {
m_data[pos+i] = newElement;
}
m_len += count;
return pos;
}else{
return Add(newElement, count);
}
}
/* Remove(xsize) */
template<class TYPE>
void XArray<TYPE>::RemoveAtIndex(xsize nIndex)
{
if ( nIndex < m_len ) {
if ( nIndex<m_len-1 ) Xmemcpy(&m_data[nIndex], &m_data[nIndex+1], (m_len-nIndex-1)*sizeof(TYPE));
m_len -= 1;
return;
}
#if defined(_DEBUG) && defined(TRACE)
TRACE("XArray::Remove(xsize) -> nIndex > m_len\n");
#endif
}
/* Remove(int) */
template<class TYPE>
void XArray<TYPE>::RemoveAtIndex(int nIndex)
{
#if defined(__XTOOLS_INT_CHECK__)
if ( nIndex < 0 ) {
DebugLog(2, "XArray<TYPE>::RemoveAtIndex(int nIndex) : BUG nIndex (%d) is < 0. System halted\n", nIndex);
CpuDeadLoop();
}
#endif
RemoveAtIndex( (xsize)nIndex ); // Check of nIndex is made in Remove(xsize nIndex)
return;
}
/* Remove(TYPE) */
template<class TYPE>
void XArray<TYPE>::Remove(const TYPE &Element)
{
xsize i;
for ( i=0 ; i<m_len ; i+= 1) {
if ( m_data[i] == Element ) {
Remove(i);
return ;
}
}
#if defined(_DEBUG) && defined(TRACE)
TRACE("XArray::Remove(const TYPE &) -> Element not found\n");
#endif
}
/* Remove(TYPE *) */
template<class TYPE>
void XArray<TYPE>::Remove(const TYPE *Element)
{
Remove(*Element);
}
/* Empty() */
template<class TYPE>
void XArray<TYPE>::Empty()
{
//printf("XArray Empty\n");
m_len = 0;
}
#endif

View File

@ -0,0 +1,577 @@
//*************************************************************************************************
//*************************************************************************************************
//
// XObjArray
//
// Developed by jief666, from 1997.
//
//*************************************************************************************************
//*************************************************************************************************
#if !defined(__XOBJARRAY_H__)
#define __XOBJARRAY_H__
//#include "../Platform/Platform.h" // for DebugLog
VOID EFIAPI DebugLog(IN INTN DebugMode, IN CONST CHAR8 *FormatString, ...); // To avoid include Platform just for this
extern "C" {
#include <Library/MemoryAllocationLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/BaseLib.h> // for CpuDeadLoop();
}
#include "XToolsCommon.h"
#if 1
#define XObjArray_DBG(...) DebugLog(2, __VA_ARGS__)
#else
#define XObjArray_DBG(...)
#endif
template <class TYPE>
class XObjArrayEntry
{
public:
TYPE* Object;
bool FreeIt;
};
template<class TYPE>
class XObjArrayNC
{
public:
XObjArrayEntry<TYPE> *_Data;
xsize _Len;
xsize _Size;
public:
void Init();
XObjArrayNC() { Init(); }
virtual ~XObjArrayNC();
protected:
XObjArrayNC(const XObjArrayNC<TYPE> &anObjArrayNC) { throw "Intentionally not defined"; }
const XObjArrayNC<TYPE> &operator =(const XObjArrayNC<TYPE> &anObjArrayNC) { throw "Intentionally not defined"; }
xsize _getLen() const { return _Len; }
public:
xsize Size() const { return _Size; }
xsize Length() const { return _Len; }
bool NotNull() const { return Length() > 0; }
bool IsNull() const { return Length() == 0; }
const TYPE &ElementAt(xsize nIndex) const;
TYPE &ElementAt(xsize nIndex);
// This was useful for realtime debugging with a debugger that do not recognise references. That was years and years ago. Probably not needed anymore.
#ifdef _DEBUG_iufasdfsfk
const TYPE *DbgAt(int i) const { if ( i >= 0 && (xsize)i < _Len ) return &ElementAt ((xsize) i); else return NULL; }
#endif
const TYPE &operator[](xsize nIndex) const { return ElementAt(nIndex); }
TYPE &operator[](xsize nIndex) { return ElementAt(nIndex); }
xsize AddReference(TYPE *newElement, bool FreeIt);
// xsize InsertRef(TYPE *newElement, xsize pos, bool FreeIt = false);
xsize InsertRef(TYPE *newElement, xsize pos, bool FreeIt);
void SetFreeIt(xsize nIndex, bool Flag);
void SetFreeIt(const TYPE *Element, bool Flag);
void Remove(const TYPE *Element);
void RemoveWithoutFreeing(const TYPE *Element);
void Remove(const TYPE &Element);
void RemoveAtIndex(xsize nIndex);
void RemoveAtIndex(int nIndex);
void RemoveWithoutFreeing(xsize nIndex); // If you use this, there might be a design problem somewhere ???
//void Remove(int nIndex);
void RemoveAllBut(const TYPE *Element);
void Empty();
public:
void CheckSize(xsize nNewSize, xsize nGrowBy = XArrayGrowByDefault);
};
template<class TYPE>
class XObjArray : public XObjArrayNC<TYPE>
{
public:
XObjArray() : XObjArrayNC<TYPE>() {}
XObjArray(const XObjArray<TYPE> &anObjArray);
const XObjArray<TYPE> &operator =(const XObjArray<TYPE> &anObjArray);
xsize AddCopy(const TYPE &newElement, bool FreeIt = true);
xsize AddCopies(const TYPE &n1, bool FreeIt = true);
xsize AddCopies(const TYPE &n1, const TYPE &n2, bool FreeThem = true);
xsize AddCopies(const TYPE &n1, const TYPE &n2, const TYPE &n3, bool FreeThem = true);
xsize AddCopies(const TYPE &n1, const TYPE &n2, const TYPE &n3, const TYPE &n4, bool FreeThem = true);
xsize AddCopies(const TYPE &n1, const TYPE &n2, const TYPE &n3, const TYPE &n4, const TYPE &n5, bool FreeThem = true);
xsize AddCopies(const TYPE &n1, const TYPE &n2, const TYPE &n3, const TYPE &n4, const TYPE &n5, const TYPE &n6, bool FreeThem = true);
xsize AddCopies(const TYPE &n1, const TYPE &n2, const TYPE &n3, const TYPE &n4, const TYPE &n5, const TYPE &n6, const TYPE &n7, bool FreeThem = true);
xsize AddCopies(const TYPE &n1, const TYPE &n2, const TYPE &n3, const TYPE &n4, const TYPE &n5, const TYPE &n6, const TYPE &n7, const TYPE &n8, bool FreeThem = true);
xsize AddCopies(const TYPE &n1, const TYPE &n2, const TYPE &n3, const TYPE &n4, const TYPE &n5, const TYPE &n6, const TYPE &n7, const TYPE &n8, const TYPE &n9, bool FreeThem = true);
xsize AddCopies(const TYPE &n1, const TYPE &n2, const TYPE &n3, const TYPE &n4, const TYPE &n5, const TYPE &n6, const TYPE &n7, const TYPE &n8, const TYPE &n9, const TYPE &n10, bool FreeThem = true);
xsize AddCopies(const TYPE &n1, const TYPE &n2, const TYPE &n3, const TYPE &n4, const TYPE &n5, const TYPE &n6, const TYPE &n7, const TYPE &n8, const TYPE &n9, const TYPE &n10, const TYPE &n11, bool FreeThem = true);
xsize AddCopies(const TYPE &n1, const TYPE &n2, const TYPE &n3, const TYPE &n4, const TYPE &n5, const TYPE &n6, const TYPE &n7, const TYPE &n8, const TYPE &n9, const TYPE &n10, const TYPE &n11, const TYPE &n12, bool FreeThem = true);
xsize AddCopies(const TYPE &n1, const TYPE &n2, const TYPE &n3, const TYPE &n4, const TYPE &n5, const TYPE &n6, const TYPE &n7, const TYPE &n8, const TYPE &n9, const TYPE &n10, const TYPE &n11, const TYPE &n12, const TYPE &n13, bool FreeThem = true);
xsize AddCopies(const TYPE &n1, const TYPE &n2, const TYPE &n3, const TYPE &n4, const TYPE &n5, const TYPE &n6, const TYPE &n7, const TYPE &n8, const TYPE &n9, const TYPE &n10, const TYPE &n11, const TYPE &n12, const TYPE &n13, const TYPE &n14, bool FreeThem = true);
//TYPE & AddNew(bool FreeIt = true);
xsize InsertCopy(const TYPE &newElement, xsize pos);
};
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//
// XObjArray
//
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
/* Constructeur */
template<class TYPE>
void XObjArrayNC<TYPE>::Init()
{
_Data = NULL;
_Size = 0;
_Len = 0;
// THis was useful for realtime debugging with a debugger that do not recognise references.
#ifdef _DEBUG_iufasdfsfk
{
const TYPE *tmp;
tmp = DbgAt(0);
}
#endif
}
/* Constructeur */
template<class TYPE>
XObjArray<TYPE>::XObjArray(const XObjArray<TYPE> &anObjArray)
{
xsize ui;
XObjArrayNC<TYPE>::Init();
this->CheckSize(anObjArray.Length(), (xsize)0);
for ( ui=0 ; ui<anObjArray.Length() ; ui+=1 ) AddCopy(anObjArray.ElementAt(ui));
}
/* operator = */
template<class TYPE>
const XObjArray<TYPE> &XObjArray<TYPE>::operator =(const XObjArray<TYPE> &anObjArray)
{
xsize ui;
XObjArrayNC<TYPE>::Empty();
CheckSize(anObjArray.Length(), 0);
for ( ui=0 ; ui<anObjArray.Length() ; ui+=1 ) AddCopy(anObjArray.ElementAt(ui));
return *this;
}
/* Destructeur */
template<class TYPE>
XObjArrayNC<TYPE>::~XObjArrayNC()
{
//printf("XObjArray Destructor\n");
Empty();
if ( _Data ) Xfree(_Data);
}
/* CheckSize() */
template<class TYPE>
void XObjArrayNC<TYPE>::CheckSize(xsize nNewSize, xsize nGrowBy)
{
if ( _Size < nNewSize ) {
nNewSize += nGrowBy + 1;
_Data = (XObjArrayEntry<TYPE> *)Xrealloc(sizeof(XObjArrayEntry<TYPE>) * _Size, sizeof(XObjArrayEntry<TYPE>) * nNewSize, (void *)_Data );
if ( !_Data ) {
XObjArray_DBG("XObjArrayNC<TYPE>::CheckSize(nNewSize=%llu, nGrowBy=%llu) : Xrealloc(%d, %d, %d) returned NULL. System halted\n", nNewSize, nGrowBy, _Size, sizeof(XObjArrayEntry<TYPE>) * nNewSize, _Data);
}
// memset(&_Data[_Size], 0, (nNewSize-_Size) * sizeof(XObjArrayEntry<TYPE>));
_Size = nNewSize;
}
}
/* ElementAt() */
template<class TYPE>
TYPE &XObjArrayNC<TYPE>::ElementAt(xsize index)
{
if ( index >= _Len ) {
DebugLog(2, "XObjArray<TYPE>::ElementAt(xsize) -> operator [] - index (%d) greater than length (%d)\n", index, _Len);
CpuDeadLoop();
}
return *((TYPE *)(_Data[index].Object));
}
/* ElementAt() */
template<class TYPE>
const TYPE &XObjArrayNC<TYPE>::ElementAt(xsize index) const
{
if ( index >= _Len ) {
DebugLog(2, "XObjArray<TYPE>::ElementAt(xsize) -> operator [] - index (%d) greater than length (%d)\n", index, _Len);
CpuDeadLoop();
}
return *((TYPE *)(_Data[index].Object));
}
///* Add() */
//template<class TYPE>
//TYPE &XObjArray<TYPE>::AddNew(bool FreeIt)
//{
// XObjArrayNC<TYPE>::CheckSize(XObjArray<TYPE>::_getLen()+1);
// XObjArray<TYPE>::_Data[XObjArray<TYPE>::_Len].Object = new TYPE;
// XObjArray<TYPE>::_Data[XObjArray<TYPE>::_Len].FreeIt = FreeIt;
// XObjArray<TYPE>::_Len += 1;
// return *((TYPE *)(XObjArray<TYPE>::_Data[XObjArray<TYPE>::_Len-1].Object));
//}
/* Add(TYPE &, xsize) */
template<class TYPE>
xsize XObjArray<TYPE>::AddCopy(const TYPE &newElement, bool FreeIt)
{
XObjArrayNC<TYPE>::CheckSize(XObjArray<TYPE>::_Len+1);
XObjArray<TYPE>::_Data[XObjArray<TYPE>::_Len].Object = new TYPE(newElement);
XObjArray<TYPE>::_Data[XObjArray<TYPE>::_Len].FreeIt = FreeIt;
XObjArray<TYPE>::_Len += 1;
return XObjArray<TYPE>::_Len-1;
}
template<class TYPE>
xsize XObjArray<TYPE>::AddCopies(const TYPE &n1, bool FreeIt)
{
return AddCopy(n1, FreeIt);
}
template<class TYPE>
xsize XObjArray<TYPE>::AddCopies(const TYPE &n1, const TYPE &n2, bool FreeThem)
{
xsize ui = AddCopies(n1, FreeThem);
AddCopy(n2, FreeThem);
return ui;
}
template<class TYPE>
xsize XObjArray<TYPE>::AddCopies(const TYPE &n1, const TYPE &n2, const TYPE &n3, bool FreeThem)
{
xsize ui = AddCopies(n1, n2, FreeThem);
AddCopy(n3, FreeThem);
return ui;
}
template<class TYPE>
xsize XObjArray<TYPE>::AddCopies(const TYPE &n1, const TYPE &n2, const TYPE &n3, const TYPE &n4, bool FreeThem)
{
xsize ui = AddCopies(n1, n2, n3, FreeThem);
AddCopy(n4, FreeThem);
return ui;
}
template<class TYPE>
xsize XObjArray<TYPE>::AddCopies(const TYPE &n1, const TYPE &n2, const TYPE &n3, const TYPE &n4, const TYPE &n5, bool FreeThem)
{
xsize ui = AddCopies(n1, n2, n3, n4, FreeThem);
AddCopy(n5, FreeThem);
return ui;
}
template<class TYPE>
xsize XObjArray<TYPE>::AddCopies(const TYPE &n1, const TYPE &n2, const TYPE &n3, const TYPE &n4, const TYPE &n5, const TYPE &n6, bool FreeThem)
{
xsize ui = AddCopies(n1, n2, n3, n4, n5, FreeThem);
AddCopy(n6, FreeThem);
return ui;
}
template<class TYPE>
xsize XObjArray<TYPE>::AddCopies(const TYPE &n1, const TYPE &n2, const TYPE &n3, const TYPE &n4, const TYPE &n5, const TYPE &n6, const TYPE &n7, bool FreeThem)
{
xsize ui = AddCopies(n1, n2, n3, n4, n5, n6, FreeThem);
AddCopy(n7, FreeThem);
return ui;
}
template<class TYPE>
xsize XObjArray<TYPE>::AddCopies(const TYPE &n1, const TYPE &n2, const TYPE &n3, const TYPE &n4, const TYPE &n5, const TYPE &n6, const TYPE &n7, const TYPE &n8, bool FreeThem)
{
xsize ui = AddCopies(n1, n2, n3, n4, n5, n6, n7, FreeThem);
AddCopy(n8, FreeThem);
return ui;
}
template<class TYPE>
xsize XObjArray<TYPE>::AddCopies(const TYPE &n1, const TYPE &n2, const TYPE &n3, const TYPE &n4, const TYPE &n5, const TYPE &n6, const TYPE &n7, const TYPE &n8, const TYPE &n9, bool FreeThem)
{
xsize ui = AddCopies(n1, n2, n3, n4, n5, n6, n7, n8, FreeThem);
AddCopy(n9, FreeThem);
return ui;
}
template<class TYPE>
xsize XObjArray<TYPE>::AddCopies(const TYPE &n1, const TYPE &n2, const TYPE &n3, const TYPE &n4, const TYPE &n5, const TYPE &n6, const TYPE &n7, const TYPE &n8, const TYPE &n9, const TYPE &n10, bool FreeThem)
{
xsize ui = AddCopies(n1, n2, n3, n4, n5, n6, n7, n8, n9, FreeThem);
AddCopy(n10, FreeThem);
return ui;
}
template<class TYPE>
xsize XObjArray<TYPE>::AddCopies(const TYPE &n1, const TYPE &n2, const TYPE &n3, const TYPE &n4, const TYPE &n5, const TYPE &n6, const TYPE &n7, const TYPE &n8, const TYPE &n9, const TYPE &n10, const TYPE &n11, bool FreeThem)
{
xsize ui = AddCopies(n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, FreeThem);
AddCopy(n11, FreeThem);
return ui;
}
template<class TYPE>
xsize XObjArray<TYPE>::AddCopies(const TYPE &n1, const TYPE &n2, const TYPE &n3, const TYPE &n4, const TYPE &n5, const TYPE &n6, const TYPE &n7, const TYPE &n8, const TYPE &n9, const TYPE &n10, const TYPE &n11, const TYPE &n12, bool FreeThem)
{
xsize ui = AddCopies(n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, n11, FreeThem);
AddCopy(n12, FreeThem);
return ui;
}
template<class TYPE>
xsize XObjArray<TYPE>::AddCopies(const TYPE &n1, const TYPE &n2, const TYPE &n3, const TYPE &n4, const TYPE &n5, const TYPE &n6, const TYPE &n7, const TYPE &n8, const TYPE &n9, const TYPE &n10, const TYPE &n11, const TYPE &n12, const TYPE &n13, bool FreeThem)
{
xsize ui = AddCopies(n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, n11, n12, FreeThem);
AddCopy(n13, FreeThem);
return ui;
}
template<class TYPE>
xsize XObjArray<TYPE>::AddCopies(const TYPE &n1, const TYPE &n2, const TYPE &n3, const TYPE &n4, const TYPE &n5, const TYPE &n6, const TYPE &n7, const TYPE &n8, const TYPE &n9, const TYPE &n10, const TYPE &n11, const TYPE &n12, const TYPE &n13, const TYPE &n14, bool FreeThem)
{
xsize ui = AddCopies(n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, n11, n12, n13, FreeThem);
AddCopy(n14, FreeThem);
return ui;
}
/* Add(TYPE *, xsize) */
template<class TYPE>
xsize XObjArrayNC<TYPE>::AddReference(TYPE *newElement, bool FreeIt)
{
XObjArrayNC<TYPE>::CheckSize(XObjArrayNC<TYPE>::_Len+1);
XObjArrayNC<TYPE>::_Data[XObjArrayNC<TYPE>::_Len].Object = newElement;
XObjArrayNC<TYPE>::_Data[XObjArrayNC<TYPE>::_Len].FreeIt = FreeIt;
XObjArrayNC<TYPE>::_Len += 1;
return XObjArrayNC<TYPE>::_Len-1;
}
/* Insert(TYPE &, xsize) */
template<class TYPE>
xsize XObjArray<TYPE>::InsertCopy(const TYPE &newElement, xsize pos)
{
if ( pos < XObjArray<TYPE>::_Len ) {
XObjArrayNC<TYPE>::CheckSize(XObjArray<TYPE>::_Len+1);
memmove(&XObjArray<TYPE>::_Data[pos+1], &XObjArray<TYPE>::_Data[pos], (XObjArray<TYPE>::_Len-pos)*sizeof(XObjArrayEntry<TYPE>));
XObjArray<TYPE>::_Data[pos].Object = new TYPE(newElement);
XObjArray<TYPE>::_Data[pos].FreeIt = true;
XObjArray<TYPE>::_Len += 1;
return pos;
}else{
return AddCopy(newElement);
}
}
/* Insert(TYPE &, xsize) */
template<class TYPE>
xsize XObjArrayNC<TYPE>::InsertRef(TYPE *newElement, xsize pos, bool FreeIt)
{
if ( pos < XObjArrayNC<TYPE>::_Len ) {
CheckSize(XObjArrayNC<TYPE>::_Len+1);
memmove(&XObjArrayNC<TYPE>::_Data[pos+1], &XObjArrayNC<TYPE>::_Data[pos], (XObjArrayNC<TYPE>::_Len-pos)*sizeof(XObjArrayEntry<TYPE>));
_Data[pos].Object = newElement;
_Data[pos].FreeIt = FreeIt;
XObjArrayNC<TYPE>::_Len += 1;
return pos;
}else{
return AddRef(newElement, FreeIt);
}
}
/* SetFreeIt(xsize, bool) */
template<class TYPE>
void XObjArrayNC<TYPE>::SetFreeIt(xsize nIndex, bool Flag)
{
if ( nIndex < XObjArrayNC<TYPE>::_Len )
{
XObjArrayNC<TYPE>::_Data[nIndex].FreeIt = Flag;
}
else{
#if defined(_DEBUG)
throw "XObjArray::SetFreeIt(xsize) -> nIndex >= _Len\n";
#endif
}
}
/* SetFreeIt(const TYPE *Element, bool) */
template<class TYPE>
void XObjArrayNC<TYPE>::SetFreeIt(const TYPE *Element, bool Flag)
{
xsize i;
for ( i=0 ; i < XObjArrayNC<TYPE>::_Len ; i+= 1) {
if ( ((TYPE *)XObjArrayNC<TYPE>::_Data[i].Object) == Element ) {
SetFreeIt(i, Flag);
return ;
}
}
#if defined(_DEBUG)
throw "XObjArray::SetFreeIt(const TYPE *) -> nIndex >= _Len\n";
#endif
}
/* Remove(xsize) */
template<class TYPE>
void XObjArrayNC<TYPE>::RemoveAtIndex(xsize nIndex)
{
if ( nIndex < XObjArrayNC<TYPE>::_Len )
{
if ( nIndex >= XObjArrayNC<TYPE>::_Len ) {
DebugLog(2, "void XObjArrayNC<TYPE>::RemoveAtIndex(xsize nIndex) : BUG nIndex (%d) is > Length(). System halted\n", nIndex);
CpuDeadLoop();
}
}
if ( _Data[nIndex].FreeIt )
{
TYPE *TmpObject; // BCB 4 oblige me to use a tmp var for doing the delete.
TmpObject = (TYPE *)(_Data[nIndex].Object);
delete TmpObject;
}
if ( nIndex<XObjArrayNC<TYPE>::_Len-1 ) Xmemcpy(&_Data[nIndex], &_Data[nIndex+1], (_Len-nIndex-1)*sizeof(XObjArrayEntry<TYPE>));
_Len -= 1;
return;
}
//-------------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------
/* RemoveWithoutFreeing(xsize) */
template<class TYPE>
void XObjArrayNC<TYPE>::RemoveWithoutFreeing(xsize nIndex)
{
if ( nIndex < _Len )
{
if ( nIndex<_Len-1 ) memcpy(&_Data[nIndex], &_Data[nIndex+1], (_Len-nIndex-1)*sizeof(XObjArrayEntry<TYPE>));
_Len -= 1;
return;
}
#if defined(_DEBUG)
throw "XObjArray::RemoveWithoutFreeing(xsize) -> nIndex > _Len\n";
#endif
}
//-------------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------
/* Remove(int) */
template<class TYPE>
void XObjArrayNC<TYPE>::RemoveAtIndex(int nIndex)
{
#if defined(__XTOOLS_INT_CHECK__)
if ( nIndex < 0 ) {
DebugLog(2, "XArray<TYPE>::RemoveAtIndex(int nIndex) : BUG nIndex (%d) is < 0. System halted\n", nIndex);
CpuDeadLoop();
}
#endif
RemoveAtIndex( (xsize)nIndex ); // Remove(xsize) will check that index is < _Len
}
/* Remove(const TYPE &) */
template<class TYPE>
void XObjArrayNC<TYPE>::Remove(const TYPE &Element)
{
xsize i;
for ( i=0 ; i<_Len ; i+= 1) {
if ( *((TYPE *)(_Data[i].Object)) == Element ) {
Remove(i);
return ;
}
}
#if defined(_DEBUG)
throw "XObjArray::Remove(TYPE &) -> Not found\n";
#endif
}
//-------------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------
/* Remove(const TYPE *) */
template<class TYPE>
void XObjArrayNC<TYPE>::Remove(const TYPE *Element)
{
xsize i;
for ( i=0 ; i<_Len ; i+= 1) {
if ( ((TYPE *)_Data[i].Object) == Element ) {
Remove(i);
return ;
}
}
#if defined(_DEBUG)
throw "XObjArray::Remove(TYPE *) -> not found\n";
#endif
}
//-------------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------
/* RemoveWithoutFreeing(const TYPE *) */
template<class TYPE>
void XObjArrayNC<TYPE>::RemoveWithoutFreeing(const TYPE *Element)
{
xsize i;
for ( i=0 ; i<_Len ; i+= 1) {
if ( ((TYPE *)_Data[i].Object) == Element ) {
RemoveWithoutFreeing(i);
return ;
}
}
#if defined(_DEBUG)
throw "XObjArray::RemoveWithoutFreeing(TYPE *) -> not found\n";
#endif
}
//-------------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------
template<class TYPE>
void XObjArrayNC<TYPE>::RemoveAllBut(const TYPE *Element)
{
xsize i;
for ( i=_Len ; i-- ; ) {
if ( ((TYPE *)_Data[i].Object) != Element ) {
Remove(i);
}
}
}
/* Empty() */
template<class TYPE>
void XObjArrayNC<TYPE>::Empty()
{
xsize i;
if ( _Len > 0 ) {
for ( i=0 ; i<_Len ; i+= 1) {
if ( _Data[i].FreeIt )
{
TYPE *TmpObject; // BCB 4 oblige me to use a tmp var for doing the delete.
TmpObject = (TYPE *)(_Data[i].Object);
delete TmpObject;
}
}
_Len = 0;
}
}
#endif

View File

@ -1,6 +1,13 @@
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//*************************************************************************************************
//*************************************************************************************************
//
// STRING
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//
// Developed by jief666, from 1997.
//
//*************************************************************************************************
//*************************************************************************************************
#if !defined(__XStringW_CPP__)
#define __XStringW_CPP__
@ -11,6 +18,7 @@
#define DBG(...)
#endif
#include "XToolsCommon.h"
#include "XStringW.h"
extern "C" {
@ -21,15 +29,15 @@ extern "C" {
#include "refit/IO.h"
UINTN XStringWGrowByDefault = 1024;
//const XStringW NullXStringW;
const XStringW NullXStringW;
void XStringW::Init(UINTN aSize)
{
//DBG("Init aSize=%d\n", aSize);
m_data = (wchar_t*)AllocatePool( (aSize+1)*sizeof(wchar_t) ); /* le 0 terminal n'est pas compté dans mSize */
m_data = (wchar_t*)Xalloc( (aSize+1)*sizeof(wchar_t) ); /* le 0 terminal n'est pas compté dans mSize */
if ( !m_data ) {
DBG("XStringW::Init(%d) : AllocatePool returned NULL. Cpu halted\n", (aSize+1)*sizeof(wchar_t));
DBG("XStringW::Init(%d) : Xalloc returned NULL. Cpu halted\n", (aSize+1)*sizeof(wchar_t));
CpuDeadLoop();
}
m_size = aSize;
@ -110,9 +118,9 @@ wchar_t *XStringW::CheckSize(UINTN nNewSize, UINTN nGrowBy)
if ( m_size < nNewSize )
{
nNewSize += nGrowBy;
m_data = (wchar_t*)ReallocatePool(m_size*sizeof(wchar_t), (nNewSize+1)*sizeof(wchar_t), m_data);
m_data = (wchar_t*)Xrealloc(m_size*sizeof(wchar_t), (nNewSize+1)*sizeof(wchar_t), m_data);
if ( !m_data ) {
DBG("XStringW::CheckSize(%d, %d) : ReallocatePool(%d, %d, %d) returned NULL. System halted\n", nNewSize, nGrowBy, m_size, (nNewSize+1)*sizeof(wchar_t), m_data);
DBG("XStringW::CheckSize(%d, %d) : Xrealloc(%d, %d, %d) returned NULL. System halted\n", nNewSize, nGrowBy, m_size, (nNewSize+1)*sizeof(wchar_t), m_data);
CpuDeadLoop();
}
m_size = nNewSize;
@ -244,14 +252,14 @@ void XStringW::SPrintf(const wchar_t *format, ...)
XStringW XStringW::basename() const
{
UINTN idx = RIdxOf(LPATH_SEPARATOR);
if ( idx == MAX_UINTN ) return XStringW();
if ( idx == MAX_UINTN ) return NullXStringW;
return SubString(idx+1, length()-idx-1);
}
XStringW XStringW::dirname() const
{
UINTN idx = RIdxOf(LPATH_SEPARATOR);
if ( idx == MAX_UINTN ) return XStringW();
if ( idx == MAX_UINTN ) return NullXStringW;
return SubString(0, idx);
}

View File

@ -0,0 +1,3 @@
#include "XToolsCommon.h"
xsize XArrayGrowByDefault;

View File

@ -0,0 +1,19 @@
#ifndef __XTOOLSCOMMON_H__
#define __XTOOLSCOMMON_H__
#define xsize UINTN
#define MAX_XSIZE MAX_UINTN
extern xsize XArrayGrowByDefault;
extern xsize XBufferGrowByDefault;
/* For convience, operator [] is define with int parameter.
* Defining __XTOOLS_INT_CHECK__ make a check that the parameter is >= 0
*/
#define __XTOOLS_INT_CHECK__
#define Xalloc(AllocationSize) AllocatePool(AllocationSize)
#define Xrealloc(OldSize, NewSize, OldBuffer) ReallocatePool(OldSize, NewSize, OldBuffer)
#define Xfree(Buffer) FreePool(Buffer)
#define Xmemcpy(dest,source,count) CopyMem(dest, (void*)(source), count)
#endif

View File

@ -0,0 +1,21 @@
#include "../cpp_foundation/XArray.h"
#include "../Platform/Platform.h"
int XArray_tests()
{
XArray<UINTN> array1;
array1.Add(12);
array1.Add(34);
array1.Add(56);
if ( array1[0] != 12 ) return 1;
if ( array1[1] != 34 ) return 2;
if ( array1[2] != 56 ) return 3;
array1.RemoveAtIndex(1);
if ( array1[1] != 56 ) return 4;
return 0;
}

View File

@ -0,0 +1,3 @@
int XArray_tests();

View File

@ -0,0 +1,63 @@
#include "../cpp_foundation/XObjArray.h"
#include "../Platform/Platform.h"
class TestObjInt
{
public:
UINTN m_v;
bool* m_destructor_called;
TestObjInt(UINTN v, bool* destructor_called) : m_v(v), m_destructor_called(destructor_called)
{
*m_destructor_called=false;
};
~TestObjInt()
{
*m_destructor_called = true;
};
};
int XObjArray_tests()
{
bool m_destructor_called11;
bool m_destructor_called12;
bool m_destructor_called13;
bool m_destructor_called14;
TestObjInt* obj14 = new TestObjInt(14, &m_destructor_called14);
{
TestObjInt obj11(11, &m_destructor_called11);
{
XObjArray<TestObjInt> array1;
array1.AddReference(&obj11, false); // Do not free this object when the array is freed, This object declared on the stack, so it'll be free by C++
array1.AddReference(new TestObjInt(12, &m_destructor_called12), true); // Free this object. C++ doesn't keep any reference of new allocated object. So it won't be freed by C++
array1.AddReference(new TestObjInt(13, &m_destructor_called13), true); // Free this object. C++ doesn't keep any reference of new allocated object. So it won't be freed by C++
array1.AddReference(obj14, false); // We keep a reference an obj14. Let's not ask the array to free it.
if ( array1[0].m_v != 11 ) return 1;
if ( array1[1].m_v != 12 ) return 2;
if ( array1[2].m_v != 13 ) return 3;
if ( array1[3].m_v != 14 ) return 4;
array1.RemoveAtIndex(1);
if ( array1[1].m_v != 13 ) return 5;
}
// Here the array and objects we added saying true as second parameter of AddReference.
if ( !m_destructor_called12 ) return 6;
if ( !m_destructor_called13 ) return 7;
// obj11 and obj14 should not be destroyed yet.
if ( m_destructor_called11 ) return 8;
if ( m_destructor_called14 ) return 9;
}
// obj11 must be destroyed by C++.
if ( !m_destructor_called11 ) return 10;
delete(obj14);
if ( !m_destructor_called14 ) return 11;
return 0;
}

View File

@ -0,0 +1,3 @@
int XObjArray_tests();

View File

@ -0,0 +1,35 @@
#ifdef DEBUG_CLOVER
DBG("g_str = %s\n", g_str.data());
DBG("g_str2 = %s\n", g_str2.data());
extern XStringW global_str1;
DBG("global_str1 = %s\n", global_str1.data());
extern XStringW global_str2;
DBG("global_str2 = %s\n", global_str2.data());
{
// XStringW str(L"local str value");
// DBG("str = %s\n", str.data());
// str.StrCat(L" appended text");
// DBG("str = %s, len=%d\n", str.data(), str.length());
//
// XStringW str2(str);
// DBG("str2 = %s\n", str2.data());
// str2.StrnCpy(str.data(), 2);
// DBG("str2 = %s\n", str2.data());
// str2.StrnCat(L"2ndtext", 2);
// DBG("str2 = %s\n", str2.data());
// str2.Insert(1, str);
// DBG("str2 = %s\n", str2.data());
// str2 += L"3rdtext";
// DBG("str2 = %s\n", str2.data());
//
// XStringW* str3 = new XStringW();
// *str3 = L"str3data";
// DBG("str3 = %s\n", str3->data());
// delete str3;
}
//
destruct_globals_objects(NULL); // That should be done just before quitting clover module. Now, it's just for test.
DBG("press");
PauseForKey(L"press");
#endif

View File

@ -0,0 +1,36 @@
#include "../cpp_foundation/XStringW.h"
#include "../cpp_foundation/XArray.h"
#include "../cpp_foundation/XObjArray.h"
#include "XArray_tests.h"
#include "XObjArray_tests.h"
#include "../Platform/Platform.h"
bool all_tests()
{
bool all_ok = true;
int ret;
ret = XArray_tests();
if ( ret != 0 ) {
DebugLog(2, "XArray_tests() failed at test %d\n", ret);
all_ok = false;
}
ret = XObjArray_tests();
if ( ret != 0 ) {
DebugLog(2, "XObjArray_tests() failed at test %d\n", ret);
all_ok = false;
}
if ( !all_ok ) {
DebugLog(2, "A test failed, module %d, test %d \n");
PauseForKey(L"press");
}else{
#ifdef JIEF_DEBUG
DebugLog(2, "All tests are ok\n");
PauseForKey(L"press");
#endif
}
return all_ok;
}

View File

@ -0,0 +1,2 @@
bool all_tests();

View File

@ -1,2 +1,2 @@
#include "XStringW.h"
#include "../cpp_foundation/XStringW.h"
XStringW global_str1(L"global_str1");

View File

@ -1,2 +1,2 @@
#include "XStringW.h"
#include "../cpp_foundation/XStringW.h"
XStringW global_str2(L"global_str2");

View File

@ -158,8 +158,6 @@
Platform/sse3_patcher.h
Platform/sse3_5_patcher.h
../Version.h
cpp_util/XStringW.h
cpp_util/XStringW.cpp
cpp_util/memory.cpp
cpp_util/global1.cpp
cpp_util/global2.cpp
@ -170,6 +168,10 @@
cpp_util/operatorNewDelete.cpp
cpp_util/operatorNewDelete.h
cpp_util/remove_ref.h
cpp_foundation/XStringW.h
cpp_foundation/XStringW.cpp
cpp_foundation/XArray.h
cpp_foundation/XObjArray.h
[Sources.IA32]
libeg/ftol.asm | MSFT
@ -317,7 +319,7 @@
[Pcd]
[BuildOptions]
XCODE:*_*_*_CC_FLAGS = -fsigned-char -O2 -fno-omit-frame-pointer -ffreestanding -fno-rtti -fno-exceptions -Wno-deprecated -Wno-writable-strings -Wno-unused-const-variable -DJCONST=const
XCODE:*_*_*_CC_FLAGS = -fsigned-char -O2 -fno-omit-frame-pointer -ffreestanding -fno-rtti -fno-exceptions -Wno-deprecated -Wno-writable-strings -Wno-unused-const-variable -DJCONST=const -Wno-incompatible-ms-struct
GCC:*_*_*_CC_FLAGS = -std=c99 -Os -fno-omit-frame-pointer -maccumulate-outgoing-args
GCC:*_*_*_CXX_FLAGS = -std=c++11 -Os -fno-omit-frame-pointer -maccumulate-outgoing-args -ffreestanding -fno-rtti -fno-exceptions -Wno-deprecated -Wno-write-strings -Wno-unused-const-variable -Wno-pointer-arith -DJCONST=const
MSFT:*_*_*_CC_FLAGS = /Os /wd4201 /D JCONST=const

View File

@ -35,9 +35,10 @@
*/
#include "../Platform/Platform.h"
#include "../cpp_util/XStringW.h"
#include "../cpp_util/globals_ctor.h"
#include "../cpp_util/globals_dtor.h"
#include "../cpp_foundation/XStringW.h"
#include "../cpp_unit_test/all_tests.h"
#include "entry_scan.h"
#include "nanosvg.h"
@ -2072,41 +2073,7 @@ RefitMain (IN EFI_HANDLE ImageHandle,
construct_globals_objects(); // do this after SelfLoadedImage is initialized
#ifdef JIEF_DEBUG
DBG("g_str = %s\n", g_str.data());
DBG("g_str2 = %s\n", g_str2.data());
extern XStringW global_str1;
DBG("global_str1 = %s\n", global_str1.data());
extern XStringW global_str2;
DBG("global_str2 = %s\n", global_str2.data());
{
// XStringW str(L"local str value");
// DBG("str = %s\n", str.data());
// str.StrCat(L" appended text");
// DBG("str = %s, len=%d\n", str.data(), str.length());
//
// XStringW str2(str);
// DBG("str2 = %s\n", str2.data());
// str2.StrnCpy(str.data(), 2);
// DBG("str2 = %s\n", str2.data());
// str2.StrnCat(L"2ndtext", 2);
// DBG("str2 = %s\n", str2.data());
// str2.Insert(1, str);
// DBG("str2 = %s\n", str2.data());
// str2 += L"3rdtext";
// DBG("str2 = %s\n", str2.data());
//
// XStringW* str3 = new XStringW();
// *str3 = L"str3data";
// DBG("str3 = %s\n", str3->data());
// delete str3;
}
//
destruct_globals_objects(NULL); // That should be done just before quitting clover module. Now, it's just for test.
DBG("press");
PauseForKey(L"press");
#endif
all_tests();
//dumping SETTING structure
// if you change something in Platform.h, please uncomment and test that all offsets

View File

@ -39,7 +39,7 @@
//#include "../include/scroll_images.h"
//#include "../Platform/Platform.h"
#include "../cpp_util/XStringW.h"
//#include "../cpp_foundation/XStringW.h"
#include "Version.h"
//#include "colors.h"