xparray.h
1/* Distributed under the Apache License, Version 2.0.
2 See accompanying NOTICE file for details.*/
3
4#ifndef __XPARRAY_H__
5#define __XPARRAY_H__
6
7
8template <typename TYPE>
10{
11 protected:
12
13 typedef void (* PFN_MoveCallback)(TYPE* pObject, const INT_PTR nCount);
14
15 static void* m_pfnMoveCallback;
16 TYPE* m_pData;
19 INT_PTR m_nGrowBy;
20
21 public:
22
23 inline CXpArray(const INT_PTR nGrowBy = 10);
24 inline CXpArray(const CXpArray& Array);
25 virtual ~CXpArray();
26
27 // The following callback can be used to track when an object
28 // moves in memory due to a reallocation. Note that this is called
29 // after the object has moved.
30 static void SetMoveCallback(void (* pfnMoveCallback)(TYPE* pObject, const INT_PTR nCount));
31
32 inline HRESULT Add(const TYPE& Element, INT_PTR* pNewIndex = NULL);
33 inline HRESULT Insert(const INT_PTR nIndex, const TYPE& Element);
34
35 inline HRESULT Remove(const INT_PTR nIndex, const INT_PTR nCount = 1);
36 inline void RemoveAll();
37
38 inline INT_PTR GetCount() const;
39 inline HRESULT SetCount(const INT_PTR nNewCount);
40
41 inline INT_PTR GetGrowBy() const;
42 inline void SetGrowBy(const INT_PTR nGrowBy);
43
44 inline HRESULT PreAlloc(const INT_PTR nCount);
45 inline HRESULT FreeExtra();
46
47 inline HRESULT Append(const CXpArray& Array);
48
49 // Similar to basic string functions. Out
50 // of range values are fine.
51 HRESULT Mid(CXpArray& Subset, const INT_PTR nFrom, const INT_PTR nCount = -1) const;
52 HRESULT Left(CXpArray& Subset, const INT_PTR nCount) const;
53 HRESULT Right(CXpArray& Subset, const INT_PTR nCount) const;
54
55 INT_PTR Find(const TYPE& Element,
56 const INT_PTR nStart = 0) const;
57
58 INT_PTR Find(const CXpArray& Subset,
59 const INT_PTR nStart = 0) const;
60
61 // Use these methods with care, they can return NULL! Also
62 // this is temporary and can change during reallocation.
63 TYPE* GetData();
64 const TYPE* GetData() const;
65
66 inline TYPE& operator[](const INT_PTR nIndex);
67 inline const TYPE& operator[](const INT_PTR nIndex) const;
68
69 inline bool operator==(const CXpArray& Array) const;
70 inline bool operator!=(const CXpArray& Array) const
71 {
72 return(!operator==(Array));
73 }
74
75 inline HRESULT Copy(const CXpArray& Array);
76 inline CXpArray& operator=(const CXpArray& Array) {Copy(Array); return(*this);}
77 protected:
78 HRESULT Allocate(const INT_PTR nCount, const bool bInitialize = true);
79
80 inline void ConstructElement(const INT_PTR nIndex, const INT_PTR nCount = 1);
81 inline void DestructElement(const INT_PTR nIndex, const INT_PTR nCount = 1);
82};
83
84template <typename TYPE>
85void XpFreePointerArray(CXpArray<TYPE>& Array)
86{
87 const INT_PTR nCount = Array.GetCount();
88
89 for(INT_PTR i = 0; i < nCount; i ++)
90 {
91 delete Array[i];
92 }
93
94 Array.RemoveAll();
95}
96
97#include "xparray.inl"
98
99#endif //__XP_ARRAY_H__
Definition: xparray.h:10
INT_PTR GetCount() const
const TYPE * GetData() const
bool operator!=(const CXpArray &Array) const
Definition: xparray.h:70
INT_PTR m_nElementCount
Definition: xparray.h:17
HRESULT Right(CXpArray &Subset, const INT_PTR nCount) const
INT_PTR m_nAllocedCount
Definition: xparray.h:18
HRESULT Left(CXpArray &Subset, const INT_PTR nCount) const
static void * m_pfnMoveCallback
Definition: xparray.h:15
CXpArray & operator=(const CXpArray &Array)
Definition: xparray.h:76
CXpArray(const INT_PTR nGrowBy=10)
const TYPE & operator[](const INT_PTR nIndex) const
HRESULT FreeExtra()
HRESULT PreAlloc(const INT_PTR nCount)
INT_PTR GetGrowBy() const
HRESULT Insert(const INT_PTR nIndex, const TYPE &Element)
TYPE * GetData()
HRESULT Copy(const CXpArray &Array)
void SetGrowBy(const INT_PTR nGrowBy)
bool operator==(const CXpArray &Array) const
INT_PTR Find(const TYPE &Element, const INT_PTR nStart=0) const
HRESULT Remove(const INT_PTR nIndex, const INT_PTR nCount=1)
HRESULT SetCount(const INT_PTR nNewCount)
HRESULT Append(const CXpArray &Array)
HRESULT Mid(CXpArray &Subset, const INT_PTR nFrom, const INT_PTR nCount=-1) const
TYPE & operator[](const INT_PTR nIndex)
HRESULT Allocate(const INT_PTR nCount, const bool bInitialize=true)
void RemoveAll()
virtual ~CXpArray()
INT_PTR m_nGrowBy
Definition: xparray.h:19
void DestructElement(const INT_PTR nIndex, const INT_PTR nCount=1)
static void SetMoveCallback(void(*pfnMoveCallback)(TYPE *pObject, const INT_PTR nCount))
void ConstructElement(const INT_PTR nIndex, const INT_PTR nCount=1)
TYPE * m_pData
Definition: xparray.h:16
HRESULT Add(const TYPE &Element, INT_PTR *pNewIndex=NULL)
CXpArray(const CXpArray &Array)
INT_PTR Find(const CXpArray &Subset, const INT_PTR nStart=0) const
void(* PFN_MoveCallback)(TYPE *pObject, const INT_PTR nCount)
Definition: xparray.h:13

Distributed under the Apache License, Version 2.0.

See accompanying NOTICE file for details.