2016-03-11 14:57:17 +01:00
|
|
|
/* Copyright (c) 2002-2012 Croteam Ltd. All rights reserved. */
|
|
|
|
|
|
|
|
#ifndef SE_INCL_STATICARRAY_H
|
|
|
|
#define SE_INCL_STATICARRAY_H
|
|
|
|
#ifdef PRAGMA_ONCE
|
|
|
|
#pragma once
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Template class for array with static allocation of objects.
|
|
|
|
*/
|
|
|
|
template<class Type>
|
|
|
|
class CStaticArray {
|
|
|
|
public:
|
|
|
|
INDEX sa_Count; // number of objects in array
|
|
|
|
Type *sa_Array; // objects
|
|
|
|
public:
|
|
|
|
/* Default constructor. */
|
2016-03-29 03:03:54 +02:00
|
|
|
inline CStaticArray(void)
|
|
|
|
{
|
|
|
|
sa_Count=0;
|
|
|
|
sa_Array=NULL;
|
|
|
|
}
|
|
|
|
|
2016-03-11 14:57:17 +01:00
|
|
|
/* Destructor. */
|
2016-03-29 03:03:54 +02:00
|
|
|
inline ~CStaticArray(void)
|
|
|
|
{
|
|
|
|
// if some objects were allocated
|
|
|
|
if (sa_Count!=0) {
|
|
|
|
// destroy them
|
|
|
|
Delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-11 14:57:17 +01:00
|
|
|
void operator=(const CStaticArray<Type> &arOriginal);
|
|
|
|
|
|
|
|
/* Create a given number of objects. */
|
|
|
|
inline void New(INDEX iCount);
|
|
|
|
/* Expand stack size but keep old objects. */
|
|
|
|
inline void Expand(INDEX iNewCount);
|
|
|
|
/* Destroy all objects. */
|
2016-03-29 03:03:54 +02:00
|
|
|
inline void Delete(void)
|
|
|
|
{
|
|
|
|
ASSERT(this!=NULL);
|
|
|
|
ASSERT(sa_Count!=0 && sa_Array!=NULL);
|
|
|
|
delete[] sa_Array;
|
|
|
|
sa_Count = 0;
|
|
|
|
sa_Array = NULL;
|
|
|
|
}
|
2016-03-11 14:57:17 +01:00
|
|
|
/* Destroy all objects, and reset the array to initial (empty) state. */
|
|
|
|
inline void Clear(void);
|
|
|
|
|
|
|
|
/* Random access operator. */
|
2016-03-29 03:03:54 +02:00
|
|
|
inline Type &operator[](INDEX iObject)
|
|
|
|
{
|
|
|
|
ASSERT(this!=NULL);
|
|
|
|
ASSERT(iObject>=0 && iObject<sa_Count); // check bounds
|
|
|
|
return sa_Array[iObject];
|
|
|
|
}
|
|
|
|
|
|
|
|
inline const Type &operator[](INDEX iObject) const
|
|
|
|
{
|
|
|
|
ASSERT(this!=NULL);
|
|
|
|
ASSERT(iObject>=0 && iObject<sa_Count); // check bounds
|
|
|
|
return sa_Array[iObject];
|
|
|
|
}
|
|
|
|
|
2016-03-11 14:57:17 +01:00
|
|
|
/* Get number of objects in array. */
|
|
|
|
INDEX Count(void) const;
|
|
|
|
/* Get index of a object from it's pointer. */
|
|
|
|
INDEX Index(Type *ptObject);
|
|
|
|
|
|
|
|
/* Copy all elements of another array into this one. */
|
|
|
|
void CopyArray(const CStaticArray<Type> &arOriginal);
|
|
|
|
/* Move all elements of another array into this one. */
|
|
|
|
void MoveArray(CStaticArray<Type> &arOther);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* include-once check. */
|
|
|
|
|