Home | History | Annotate | Download | only in crt
      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:
     35   explicit CFX_BaseArrayTemplate(int32_t iGrowSize)
     36       : CFX_BaseArray(iGrowSize, sizeof(baseType)) {}
     37   CFX_BaseArrayTemplate(int32_t iGrowSize, int32_t iBlockSize)
     38       : CFX_BaseArray(iGrowSize, iBlockSize) {}
     39 
     40   int32_t GetSize() const { return CFX_BaseArray::GetSize(); }
     41   int32_t GetBlockSize() const { return CFX_BaseArray::GetBlockSize(); }
     42   baseType* AddSpace() {
     43     return (baseType*)CFX_BaseArray::AddSpaceTo(CFX_BaseArray::GetSize());
     44   }
     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   }
     50   baseType* GetBuffer() const { return (baseType*)CFX_BaseArray::GetBuffer(); }
     51   baseType& GetAt(int32_t index) const {
     52     return *(baseType*)CFX_BaseArray::GetAt(index);
     53   }
     54   baseType* GetPtrAt(int32_t index) const {
     55     return (baseType*)CFX_BaseArray::GetAt(index);
     56   }
     57   void SetAt(int32_t index, const baseType& element) {
     58     *(baseType*)CFX_BaseArray::GetAt(index) = element;
     59   }
     60   void SetAtGrow(int32_t index, const baseType& element) {
     61     *(baseType*)CFX_BaseArray::AddSpaceTo(index) = element;
     62   }
     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   }
     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   }
     73   int32_t RemoveLast(int32_t iCount) {
     74     return CFX_BaseArray::RemoveLast(iCount);
     75   }
     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 
     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:
    121   explicit CFX_DiscreteArrayTemplate(int32_t iChunkSize)
    122       : CFX_BaseDiscreteArray(iChunkSize, sizeof(baseType)) {}
    123 
    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   }
    128   baseType* GetPtrAt(int32_t index) const {
    129     return (baseType*)CFX_BaseDiscreteArray::GetAt(index);
    130   }
    131   void SetAtGrow(int32_t index, const baseType& element) {
    132     *(baseType*)CFX_BaseDiscreteArray::AddSpaceTo(index) = element;
    133   }
    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:
    154   explicit CFX_StackTemplate(int32_t iChunkSize)
    155       : CFX_BaseStack(iChunkSize, sizeof(baseType)) {}
    156 
    157   int32_t Push(const baseType& element) {
    158     int32_t index = CFX_BaseStack::GetSize();
    159     *(baseType*)CFX_BaseStack::Push() = element;
    160     return index;
    161   }
    162   void Pop() { CFX_BaseStack::Pop(); }
    163   baseType* GetTopElement() const {
    164     return (baseType*)CFX_BaseStack::GetTopElement();
    165   }
    166   int32_t GetSize() const { return CFX_BaseStack::GetSize(); }
    167   baseType* GetAt(int32_t index) const {
    168     return (baseType*)CFX_BaseStack::GetAt(index);
    169   }
    170   void RemoveAll(bool bLeaveMemory) { CFX_BaseStack::RemoveAll(bLeaveMemory); }
    171 };
    172 
    173 #endif  // XFA_FGAS_CRT_FGAS_UTILS_H_
    174