1 // Copyright 2014 PDFium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 6 7 #ifndef XFA_FGAS_CRT_FGAS_UTILS_H_ 8 #define XFA_FGAS_CRT_FGAS_UTILS_H_ 9 10 #include "core/fxcrt/fx_coordinates.h" 11 12 class FX_BASEARRAYDATA; 13 14 class CFX_BaseArray { 15 protected: 16 CFX_BaseArray(int32_t iGrowSize, int32_t iBlockSize); 17 ~CFX_BaseArray(); 18 19 int32_t GetSize() const; 20 int32_t GetBlockSize() const; 21 uint8_t* AddSpaceTo(int32_t index); 22 uint8_t* GetAt(int32_t index) const; 23 uint8_t* GetBuffer() const; 24 int32_t Append(const CFX_BaseArray& src, int32_t iStart, int32_t iCount); 25 int32_t Copy(const CFX_BaseArray& src, int32_t iStart, int32_t iCount); 26 int32_t RemoveLast(int32_t iCount); 27 void RemoveAll(bool bLeaveMemory); 28 29 FX_BASEARRAYDATA* m_pData; 30 }; 31 32 template <class baseType> 33 class CFX_BaseArrayTemplate : public CFX_BaseArray { 34 public: CFX_BaseArrayTemplate(int32_t iGrowSize)35 explicit CFX_BaseArrayTemplate(int32_t iGrowSize) 36 : CFX_BaseArray(iGrowSize, sizeof(baseType)) {} CFX_BaseArrayTemplate(int32_t iGrowSize,int32_t iBlockSize)37 CFX_BaseArrayTemplate(int32_t iGrowSize, int32_t iBlockSize) 38 : CFX_BaseArray(iGrowSize, iBlockSize) {} 39 GetSize()40 int32_t GetSize() const { return CFX_BaseArray::GetSize(); } GetBlockSize()41 int32_t GetBlockSize() const { return CFX_BaseArray::GetBlockSize(); } AddSpace()42 baseType* AddSpace() { 43 return (baseType*)CFX_BaseArray::AddSpaceTo(CFX_BaseArray::GetSize()); 44 } Add(const baseType & element)45 int32_t Add(const baseType& element) { 46 int32_t index = CFX_BaseArray::GetSize(); 47 *(baseType*)CFX_BaseArray::AddSpaceTo(index) = element; 48 return index; 49 } GetBuffer()50 baseType* GetBuffer() const { return (baseType*)CFX_BaseArray::GetBuffer(); } GetAt(int32_t index)51 baseType& GetAt(int32_t index) const { 52 return *(baseType*)CFX_BaseArray::GetAt(index); 53 } GetPtrAt(int32_t index)54 baseType* GetPtrAt(int32_t index) const { 55 return (baseType*)CFX_BaseArray::GetAt(index); 56 } SetAt(int32_t index,const baseType & element)57 void SetAt(int32_t index, const baseType& element) { 58 *(baseType*)CFX_BaseArray::GetAt(index) = element; 59 } SetAtGrow(int32_t index,const baseType & element)60 void SetAtGrow(int32_t index, const baseType& element) { 61 *(baseType*)CFX_BaseArray::AddSpaceTo(index) = element; 62 } Append(const CFX_BaseArrayTemplate & src,int32_t iStart,int32_t iCount)63 int32_t Append(const CFX_BaseArrayTemplate& src, 64 int32_t iStart, 65 int32_t iCount) { 66 return CFX_BaseArray::Append(src, iStart, iCount); 67 } Copy(const CFX_BaseArrayTemplate & src,int32_t iStart,int32_t iCount)68 int32_t Copy(const CFX_BaseArrayTemplate& src, 69 int32_t iStart, 70 int32_t iCount) { 71 return CFX_BaseArray::Copy(src, iStart, iCount); 72 } RemoveLast(int32_t iCount)73 int32_t RemoveLast(int32_t iCount) { 74 return CFX_BaseArray::RemoveLast(iCount); 75 } RemoveAll(bool bLeaveMemory)76 void RemoveAll(bool bLeaveMemory) { CFX_BaseArray::RemoveAll(bLeaveMemory); } 77 }; 78 79 class CFX_BaseMassArrayImp { 80 public: 81 CFX_BaseMassArrayImp(int32_t iChunkSize, int32_t iBlockSize); 82 ~CFX_BaseMassArrayImp(); 83 AddSpace()84 uint8_t* AddSpace() { return AddSpaceTo(m_iBlockCount); } 85 uint8_t* AddSpaceTo(int32_t index); 86 uint8_t* GetAt(int32_t index) const; 87 int32_t Append(const CFX_BaseMassArrayImp& src, 88 int32_t iStart, 89 int32_t iCount); 90 int32_t Copy(const CFX_BaseMassArrayImp& src, int32_t iStart, int32_t iCount); 91 int32_t RemoveLast(int32_t iCount); 92 void RemoveAll(bool bLeaveMemory); 93 94 int32_t m_iChunkSize; 95 int32_t m_iBlockSize; 96 int32_t m_iChunkCount; 97 int32_t m_iBlockCount; 98 CFX_ArrayTemplate<void*>* m_pData; 99 100 protected: 101 void Append(int32_t iDstStart, 102 const CFX_BaseMassArrayImp& src, 103 int32_t iSrcStart, 104 int32_t iSrcCount); 105 }; 106 107 class CFX_BaseDiscreteArray { 108 protected: 109 CFX_BaseDiscreteArray(int32_t iChunkSize, int32_t iBlockSize); 110 ~CFX_BaseDiscreteArray(); 111 112 uint8_t* AddSpaceTo(int32_t index); 113 uint8_t* GetAt(int32_t index) const; 114 void RemoveAll(); 115 void* m_pData; 116 }; 117 118 template <class baseType> 119 class CFX_DiscreteArrayTemplate : public CFX_BaseDiscreteArray { 120 public: CFX_DiscreteArrayTemplate(int32_t iChunkSize)121 explicit CFX_DiscreteArrayTemplate(int32_t iChunkSize) 122 : CFX_BaseDiscreteArray(iChunkSize, sizeof(baseType)) {} 123 GetAt(int32_t index,const baseType & defValue)124 baseType& GetAt(int32_t index, const baseType& defValue) const { 125 baseType* p = (baseType*)CFX_BaseDiscreteArray::GetAt(index); 126 return p ? *p : (baseType&)defValue; 127 } GetPtrAt(int32_t index)128 baseType* GetPtrAt(int32_t index) const { 129 return (baseType*)CFX_BaseDiscreteArray::GetAt(index); 130 } SetAtGrow(int32_t index,const baseType & element)131 void SetAtGrow(int32_t index, const baseType& element) { 132 *(baseType*)CFX_BaseDiscreteArray::AddSpaceTo(index) = element; 133 } RemoveAll()134 void RemoveAll() { CFX_BaseDiscreteArray::RemoveAll(); } 135 }; 136 137 class CFX_BaseStack { 138 protected: 139 CFX_BaseStack(int32_t iChunkSize, int32_t iBlockSize); 140 ~CFX_BaseStack(); 141 142 uint8_t* Push(); 143 void Pop(); 144 uint8_t* GetTopElement() const; 145 int32_t GetSize() const; 146 uint8_t* GetAt(int32_t index) const; 147 void RemoveAll(bool bLeaveMemory); 148 CFX_BaseMassArrayImp* m_pData; 149 }; 150 151 template <class baseType> 152 class CFX_StackTemplate : public CFX_BaseStack { 153 public: CFX_StackTemplate(int32_t iChunkSize)154 explicit CFX_StackTemplate(int32_t iChunkSize) 155 : CFX_BaseStack(iChunkSize, sizeof(baseType)) {} 156 Push(const baseType & element)157 int32_t Push(const baseType& element) { 158 int32_t index = CFX_BaseStack::GetSize(); 159 *(baseType*)CFX_BaseStack::Push() = element; 160 return index; 161 } Pop()162 void Pop() { CFX_BaseStack::Pop(); } GetTopElement()163 baseType* GetTopElement() const { 164 return (baseType*)CFX_BaseStack::GetTopElement(); 165 } GetSize()166 int32_t GetSize() const { return CFX_BaseStack::GetSize(); } GetAt(int32_t index)167 baseType* GetAt(int32_t index) const { 168 return (baseType*)CFX_BaseStack::GetAt(index); 169 } RemoveAll(bool bLeaveMemory)170 void RemoveAll(bool bLeaveMemory) { CFX_BaseStack::RemoveAll(bLeaveMemory); } 171 }; 172 173 #endif // XFA_FGAS_CRT_FGAS_UTILS_H_ 174