Mobile API Reference  MicroStrategy 2019
FragmentedString.h
Go to the documentation of this file.
1 //==============================================================================================
2 // FILENAME : FragmentedString.h
3 // AUTHOR : Xingyan Fan
4 // CREATION : 2008-12-29
5 // Copyright (C) MicroStrategy, Inc. 2008
6 // All rights reserved
7 //==============================================================================================
8 
9 #ifndef MBase_FragmentedString_h
10 #define MBase_FragmentedString_h
11 
12 #include "PDCHeader/PDCwchar.h"
13 //#include "Base/Base/Buffer.h"
14 #include "Base/Base/Allocator.h"
16 #include "Base.h"
17 #include "Base/Defines/StrongPtr.h"
18 //#include "PDCHeader/PDCwtypes.h"
19 
20 typedef long HRESULT;
21 
22 namespace MBase
23 {
24  template<class T>
25  class DeleteFS
26  {
27  public:
28  void operator()(T* ipData) throw()
29  {
30  if (ipData!=NULL)
31  {
32  ipData->Delete();
33  }
34  }
35  };
36 
38  {
39  public:
40  //Constructors
41 
42  // construct a fragmented string based on the input string ipStr
43  // iSize decides the initial block size, it will copy the whole ipStr whatever the iSize is.
44  // use MBase::Allocator<wchar_t> as default allocator
45  FragmentedString(WCHAR* ipStr, size_t iSize, const MBase::Allocator<WCHAR> &iAlloc = MBase::Allocator<WCHAR>());
46 
47  // if there is no input paremeter iSize, it will allocate memory on page boundary and copy the whole ipStr
49 
50  // destructor
52 
63  // return S_OK if success, return S_FALSE if error, return DSSCOM_E_XML_BUFFER_EXCEPTION if allocate throw
64  HRESULT AppendChars(WCHAR* ipChars, unsigned Int32 iLength);
65 
70  HRESULT AppendChar(WCHAR iChar);
71 
78  HRESULT AppendString(WCHAR* ipString);
79 
85  HRESULT get_Length(unsigned Int32* opLength);
86 
95  HRESULT CopyString(unsigned Int32 iLength, WCHAR* opString);
96 
97  // return total memory allocated size in bytes
98  HRESULT get_TotalAllocatedSize(unsigned Int32* opSize);
99 
100  // return the total number of allocations to construct a whole string
101  HRESULT get_TotalAllocNumer(unsigned Int32* opNum);
102 
103  // return the initial size passed-in to the constrcutor (in wide characters)
104  // return 0 if there is no passed-in initial size
105  HRESULT get_InitSize(unsigned Int32* opInit);
106 
107  void Delete();
108 
109  private:
110  HRESULT hAppendChars(WCHAR* ipChars, unsigned Int32 iLength, bool ibThrow);
111 
112  // please check if HEADER_SIZE is multiple of sizeof(wchar_t) if you modify BlockHeader
113  struct BlockHeader
114  {
115  unsigned Int32 mBlockSize; //block size, include the header
116  void *mNext; // to point to the next block
117  };
118 
119  // HEADER_SIZE should be in multiple of sizeof(wchar_t) because the header is allocated with allocator<wchar_t>.
120  // This is not an issue right now.
121  // But if we make changes of BlockHeader, we need to check HEADER_SIZE.
122  #define HEADER_SIZE sizeof(BlockHeader)
123 
124  void *mHeader; //pointer of the fragmented string header
126  // size_t mPageSize; // system page size
127  size_t mAdjustSize; // block size needs to be on boundary of mAdjustSize
128 
129  BlockHeader *mCurHdr; //current block
130  unsigned Int32 mSpaceUsed; // Bytes used in the current block, include the header
131  unsigned Int32 mTotalLength; // total length of the string
132  unsigned Int32 mTotalAllocatedSize; // total allocated size
133  unsigned Int32 mTotalAllocNum;
134  unsigned Int32 mInitSize;
135 
136  // helper functions
137  WCHAR* GetCurrentPosition();
138  size_t AdjustBoundary(size_t iSize);
139  };
140 
141  // return NULL if exception happens. (for example, allocate throw)
142  DLL_BASE_EXIM FragmentedString* CreateFragmentedString(WCHAR* ipStr, size_t iSize, const MBase::Allocator<WCHAR> &iAlloc = MBase::Allocator<WCHAR>());
144 
146 
147 }
148 
149 #endif
wchar_t WCHAR
Definition: PDCwchar.h:23
HRESULT AppendChar(WCHAR iChar)
HRESULT get_TotalAllocatedSize(unsigned Int32 *opSize)
long HRESULT
Definition: FragmentedString.h:20
#define DLL_BASE_EXIM
Definition: Base.h:20
void operator()(T *ipData)
Definition: FragmentedString.h:28
HRESULT AppendChars(WCHAR *ipChars, unsigned Int32 iLength)
Definition: FragmentedString.h:37
DLL_BASE_EXIM FragmentedString * CreateFragmentedString(WCHAR *ipStr, size_t iSize, const MBase::Allocator< WCHAR > &iAlloc=MBase::Allocator< WCHAR >())
#define Int32
Definition: BasicTypes.h:20
HRESULT CopyString(unsigned Int32 iLength, WCHAR *opString)
HRESULT get_TotalAllocNumer(unsigned Int32 *opNum)
FragmentedString(WCHAR *ipStr, size_t iSize, const MBase::Allocator< WCHAR > &iAlloc=MBase::Allocator< WCHAR >())
Definition: Allocator.h:47
HRESULT get_Length(unsigned Int32 *opLength)
Definition: StrongPtr.h:50
Definition: FragmentedString.h:25
HRESULT get_InitSize(unsigned Int32 *opInit)
MBase::StrongPtr< FragmentedString, DeleteFS< FragmentedString > > FragmentedStringPtr
Definition: FragmentedString.h:145
HRESULT AppendString(WCHAR *ipString)
#define NULL
Definition: Null.h:10