Polygon Crucher SDK - Documentation
Documentation
Loading...
Searching...
No Matches
ElementArray.h
Go to the documentation of this file.
1//! @file ElementArray.h
2//! @brief An advanced array class containing data that needs to be construct / destruct
3//!
4//////////////////////////////////////////////////////////////////////
5
6#if !defined(AFX_ELEMENTARRAY_H__04DC4B96_93F6_11D2_A1B6_000000000000__INCLUDED_)
7#define AFX_ELEMENTARRAY_H__04DC4B96_93F6_11D2_A1B6_000000000000__INCLUDED_
8
9#ifdef _MSC_VER
10#pragma once
11#endif // _MSC_VER
12
13#include "InstanciatedObject.h"
14
15BEGIN_MOOTOOLS_NAMESPACE
16
17typedef unsigned int ElementType;
18
19//! @class CElementMethods
20//! @brief CElementMethods is provided to CElementArray and give some information on the data as well as the methods to construct or destruct data in-place.
21class DLL_TOOLSFUNCTION CElementMethods : public CXObject, public CInstanciatedObject
22{
23protected:
24 virtual ~CElementMethods(); // Call ->Delete()
25
26public:
27 //! Return false is element array are object that need to be destructed, copied or serialized\n
28 //!in this case, element are memory struct and array used memcpy\n
29 //! when true is returned, DestructElement/CopyElement/SerializeElement is called anytime it is needed\n
30 //
31 //! @Note for class should return true if the class has virtual method and if we make copy between inherited object\n
32 //! otherwise memcpy overwrite the virtual table, and the inheritance is lost
33 virtual bool IsObject() const;
34
35 //! This is the default object comparison method use when we store raw memory structure
36 //! If we compare this and B:
37 //! * We have this->GetBaseType == B->GetType, we make a memcpy with the size of the structure
38 //! * We have this->GetBaseType = B->GetType, we memcpy with the sizeof(A)
39 //! * We have this->GetType = B->GetBaseType, we memcpy with the sizeof(B)
40 //! @Note If we have this->GetBaseType == this->GetBaseType we cannot do anything even if the structure are in relation, because\n
41 //! we don't know the sizeof the BaseType. In such case IsObject should return true and CopyElement can handle this\n
42 //! kind copy\n
43 //
44 //! For class we if this->GetBaseType() == B->GetType() that means that this knows how to copy from B
45 virtual bool IsKindOf(const CElementMethods *methods) const;
46
47 virtual SIZET GetSizeof() const = 0;
48 virtual ElementType GetType() const = 0;
49 virtual ElementType GetBaseType() const = 0;
50 virtual void ConstructElement(void* pNewData) = 0; // Always initialize array elements
51 virtual void DestructElement(void* pNewData);
52 virtual void CopyElement(void* pDest, const void* pSrc);
53 virtual void CopyElement(void* pDest, const CElementMethods *pSrcMethods, const void* pSrc, const CElementMethods *pDstMethods);
54 virtual void SerializeElement(CXArchive& ar, void* pSrc);
55
56 virtual CXRuntimeClassPtr GetRuntimeClassDescriptor() const { XASSERT(0); return NULL; }; //!< **Important** DECLARE_SERIAL_XOBJECT // IMPLEMENT_SERIAL_XOBJECT must be declared if the array need to be serialized
57
58#ifdef _DEBUG
59 using CXObject::Dump;
60 virtual void Dump(const void *data) const;
61#endif
62};
63
64//! @class CElementArray
65//! @brief An advanced array class containing data that needs to be construct / destruct
66//! @details CElementMethods provides some information about the data contained in the array.
67//!
68class DLL_TOOLSFUNCTION CElementArray : public CXObject
69{
70public:
71 //! @enum GrowMode
72 //! GrowMode control the way the array inflate while adding data.
73 typedef enum GrowMode
74 {
75 GROW_DEFAULT = 0, //!< A default grow mode which increase the array size by 1024 bytes each times its needed
76 GROW_KEEP_MODE = -1, //!< Keep the default grow size (SetAtGrow, SetSize)
77 GROW_DOUBLE_SIZE = -2, //!< Double size the array for fast element growing (required when the final element number is not known, and is probably big)
78 } GrowMode;
79
80 //! Create an array. methods can be set to NULL when serializing (CElementMethods are created on the fly) or if SetMethods is called just after creation
82 virtual ~CElementArray();
83
84 void SetMethods(CElementMethods *methods);
85
86 ElementType GetBaseTypeOfElement() const;
87 ElementType GetTypeOfElement() const;
88 SIZET GetSizeOfElement() const;
89
90 void FreeExtra();
91 int SetGrowFactor(int nGrowBy);
92 void PreAllocate(int estimatedSize); //!< Preallocate array. GetSize() return 0. When filling the array, SetSize must be called with growOnly = true, otherwise array will be resized loosing the preallocation advantage
93 //! Can be use after Preallocate(XX) or SetSize(XX).
94 //! This allow to construct all XX objects only once. Calling SetSize(YY, true) allow to resized down or up (to XX) without construction / destruction of elements.
95 //! As element are construct once, elements can be leaved in an unknown state so this is to be used carefully.
96 //! The mode remains valid until FreeExtra or ConstructMode(false) to go back to a normal mode
97 void ConstructMode(bool constructAllOnce);
98
99 //! Set the new size of the array
100 //! If the array is to small, the array size if increased by a factor depending on nGrowBy mode.
101 //! If the array is too big, the array is resized unless growOnly = true
102 //! if newSize = 0, the array is freed whatever growOnly value.
103 virtual void SetSize(int nNewSize, bool growOnly = false, int nGrowBy = GROW_KEEP_MODE);
104
105 virtual void Serialize(CXArchive& ar);
106
107 int GetSize() const;
108 int GetUpperBound() const;
109 virtual void RemoveAll();
110 void SetAt(int nIndex, const void *newElement);
111 void *ElementAt(int nIndex);
112 const void *ElementAt(int nIndex) const;
113 const void *GetFirst() const;
114 const void *GetNext(const void *ptrObject) const;
115 void *GetFirst();
116 void *GetNext(void *ptrObject);
117 int Add(const void *newElement, int nGrowBy = GROW_KEEP_MODE);
118 void SetAtGrow(int nIndex, const void *newElement, int nGrowBy = GROW_KEEP_MODE);
119 void *operator[](int nIndex);
120 const void *operator[](int nIndex) const;
121
122 // Potentially growing the array
123 int Append(const CElementArray& src, bool growOnly = false);
124 bool Copy(const CElementArray& src, bool growOnly = false);
125 bool Copy(int dstindex, int srcindex, const CElementArray& src);
126 virtual CElementArray *Duplicate(bool copydata) const;
127
128 // Operations that move elements around
129 void InsertAt(int nIndex, void *newElement, int nCount = 1);
130 void RemoveAt(int nIndex, int nCount = 1);
131 void InsertAt(int nStartIndex, CElementArray* pNewArray);
132
133 bool Reorder(const unsigned int *reforder, unsigned int size, bool moveToDestinationIndexes);
134 unsigned int GetChecksum() const; //!< Return a checksum value made by bitwise operator that is a simple way to check if content changed between 2 calls
135
136#ifdef _DEBUG
137 void Dump(int index) const;
138 void Dump() const;
139#endif
140
141protected:
142 CElementMethods *pElementMethods;
143
144 // Implementation
145private:
146 unsigned char *buffer; // the actual array of data
147 SIZET sizeofElement; // Size of one element (28/06/2013) : use size_t type, so elementCount*sizeofElement has type size_t which avoid 32 bits mult overflow)
148 int elementCount; // number of elements
149 int maxElementCount; // max allocated elements
150 int growElementCount; // number of element to grow the array
151 bool allConstructed;
152
153 void InitElement(SIZET);
154 const CElementArray& operator=(const CElementArray& Rhs) {return *this;}; // Use Copy instead, to avoid unvoluntary copy
155
156protected:
157 #ifdef __WINDOWS__
158 #pragma warning(push)
159 #pragma warning(disable: 4995)
160 #endif
161 void ConstructElements(unsigned char *pNewData, int nCount);
162 void DestructElements(unsigned char *pOldData, int nCount);
163 bool CopyElements(int dstindex, int srcindex, int nCount, const CElementArray& src);
164 bool CanCopyElement(const CElementMethods *methods) const;
165 #ifdef __WINDOWS__
166 #pragma warning(pop)
167 #endif
168
169 void CopyElements(unsigned char *pDest, const unsigned char *pSrc, int nCount);
170};
171
172inline int CElementArray::GetSize() const
173{
174 return elementCount;
175}
176
177inline int CElementArray::GetUpperBound() const
178{
179 return elementCount - 1;
180}
181
182inline void CElementArray::SetAt(int nIndex, const void *newElement)
183{
184 if (nIndex < elementCount)
185 CopyElements((buffer + (nIndex*sizeofElement)), (unsigned char *)newElement, 1);
186}
187
188inline void *CElementArray::ElementAt(int nIndex)
189{
190 if (nIndex < elementCount)
191 return (void *)(buffer + (nIndex*sizeofElement));
192
193 XASSERT(0);
194 return NULL;
195}
196
197inline const void *CElementArray::ElementAt(int nIndex) const
198{
199 if (nIndex < elementCount)
200 return (const void *)(buffer + (nIndex*sizeofElement));
201
202 XASSERT(0);
203 return NULL;
204}
205
206inline const void *CElementArray::GetFirst() const
207{
208 return (const void *)buffer;
209}
210
211inline const void *CElementArray::GetNext(const void *ptrObject) const
212{
213 return (const void *)(((unsigned char *)ptrObject) + sizeofElement);
214}
215
216inline void *CElementArray::GetFirst()
217{
218 return (void *)buffer;
219}
220
221inline void *CElementArray::GetNext(void *ptrObject)
222{
223 return (void *)(((unsigned char *)ptrObject) + sizeofElement);
224}
225
226inline int CElementArray::Add(const void *newElement, int nGrowBy)
227{
228 int nIndex = elementCount;
229 SetAtGrow(nIndex, newElement, nGrowBy);
230 return nIndex;
231}
232
233inline void CElementArray::SetAtGrow(int nIndex, const void *newElement, int nGrowBy)
234{
235 XASSERT(nIndex >= 0);
236 XASSERT(nIndex < elementCount || newElement < buffer || newElement >= buffer + (maxElementCount * sizeofElement)); // Set size might reallocate memory. newElement should not be inside the block
237
238 if (nIndex >= elementCount)
239 SetSize(nIndex + 1, true, nGrowBy);
240
241 CopyElements((buffer + (nIndex*sizeofElement)), (const unsigned char *)newElement, 1);
242}
243
244inline void *CElementArray::operator[](int nIndex)
245{
246 if (nIndex < elementCount)
247 return (void *)(buffer + (nIndex*sizeofElement));
248
249 XASSERT(0);
250 return NULL;
251}
252
253inline const void *CElementArray::operator[](int nIndex) const
254{
255 if (nIndex < elementCount)
256 return (const void *)(buffer + (nIndex*sizeofElement));
257
258 XASSERT(0);
259 return NULL;
260}
261
262inline void CElementArray::CopyElements(unsigned char *pDest, const unsigned char *pSrc, int nCount)
263{
264 if (!pElementMethods->IsObject())
265 {
266 XASSERT(pDest >= buffer);
267 XASSERT((SIZET)(((pDest - buffer) / sizeofElement)*sizeofElement) == (SIZET)(pDest - buffer)); // Check pDest match a a multiple of sizeofElement from buffer
268 xmemcpy_s(pDest, nCount*sizeofElement, pSrc, nCount*sizeofElement);
269 return;
270 }
271
272 XASSERT(nCount >= 0);
273 XASSERT(sizeofElement > 0);
274
275 while (nCount--)
276 {
277 pElementMethods->CopyElement(pDest, pSrc);
278 pDest += sizeofElement;
279 pSrc += sizeofElement;
280 }
281}
282
283END_MOOTOOLS_NAMESPACE
284
285#endif // !defined(AFX_ELEMENTARRAY_H__04DC4B96_93F6_11D2_A1B6_000000000000__INCLUDED_)
The class defines an x, y, z 3D point which can use int, float or double.
Definition 3DPoint.h:27
An advanced array class containing data that needs to be construct / destruct.
Definition ElementArray.h:69
void ConstructMode(bool constructAllOnce)
virtual void SetSize(int nNewSize, bool growOnly=false, int nGrowBy=GROW_KEEP_MODE)
unsigned int GetChecksum() const
Return a checksum value made by bitwise operator that is a simple way to check if content changed bet...
CElementArray(CElementMethods *methods)
Create an array. methods can be set to NULL when serializing (CElementMethods are created on the fly)...
void PreAllocate(int estimatedSize)
GrowMode
Definition ElementArray.h:74
CElementMethods is provided to CElementArray and give some information on the data as well as the met...
Definition ElementArray.h:22
virtual bool IsKindOf(const CElementMethods *methods) const
For class we if this->GetBaseType() == B->GetType() that means that this knows how to copy from B.
virtual bool IsObject() const
Definition InstanciatedObject.h:16
Definition XArchive.h:17