Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

760 lines
23 KiB

  1. //
  2. // FauxMFC.h
  3. //
  4. #pragma once
  5. #define _FAUXMFC // so we can distinguish between MFC and Faux MFC
  6. #ifndef _FAUXMFC_NO_SYNCOBJ
  7. #include "SyncObj.h"
  8. #endif
  9. #ifndef PRINTACTION_NETINSTALL
  10. #define PRINTACTION_NETINSTALL 2
  11. #endif
  12. #ifndef _MAX_PATH
  13. #define _MAX_PATH 260 /* max. length of full pathname */
  14. #endif
  15. #ifndef AFXAPI
  16. #define AFXAPI __stdcall
  17. #endif
  18. // AFX_CDECL is used for rare functions taking variable arguments
  19. #ifndef AFX_CDECL
  20. #define AFX_CDECL __cdecl
  21. #endif
  22. // FASTCALL is used for static member functions with little or no params
  23. #ifndef FASTCALL
  24. #define FASTCALL __fastcall
  25. #endif
  26. #ifndef AFX_STATIC
  27. #define AFX_STATIC extern
  28. #define AFX_STATIC_DATA extern __declspec(selectany)
  29. #endif
  30. #ifndef _AFX
  31. #define _AFX
  32. #endif
  33. #ifndef _AFX_INLINE
  34. #define _AFX_INLINE inline
  35. #endif
  36. extern const LPCTSTR _afxPchNil;
  37. #define afxEmptyString ((CString&)*(CString*)&_afxPchNil)
  38. HINSTANCE AFXAPI AfxGetResourceHandle(void);
  39. struct CStringData
  40. {
  41. long nRefs; // reference count
  42. int nDataLength; // length of data (including terminator)
  43. int nAllocLength; // length of allocation
  44. // TCHAR data[nAllocLength]
  45. TCHAR* data() // TCHAR* to managed data
  46. { return (TCHAR*)(this+1); }
  47. };
  48. class CString
  49. {
  50. public:
  51. // Constructors
  52. // constructs empty CString
  53. CString();
  54. // copy constructor
  55. CString(const CString& stringSrc);
  56. // from a single character
  57. CString(TCHAR ch, int nRepeat = 1);
  58. // from an ANSI string (converts to TCHAR)
  59. CString(LPCSTR lpsz);
  60. // from a UNICODE string (converts to TCHAR)
  61. CString(LPCWSTR lpsz);
  62. // subset of characters from an ANSI string (converts to TCHAR)
  63. CString(LPCSTR lpch, int nLength);
  64. // subset of characters from a UNICODE string (converts to TCHAR)
  65. CString(LPCWSTR lpch, int nLength);
  66. // from unsigned characters
  67. CString(const unsigned char* psz);
  68. // Attributes & Operations
  69. // get data length
  70. int GetLength() const;
  71. // TRUE if zero length
  72. BOOL IsEmpty() const;
  73. // clear contents to empty
  74. void Empty();
  75. // return single character at zero-based index
  76. TCHAR GetAt(int nIndex) const;
  77. // return single character at zero-based index
  78. TCHAR operator[](int nIndex) const;
  79. // set a single character at zero-based index
  80. void SetAt(int nIndex, TCHAR ch);
  81. // return pointer to const string
  82. operator LPCTSTR() const;
  83. // overloaded assignment
  84. // ref-counted copy from another CString
  85. const CString& operator=(const CString& stringSrc);
  86. // set string content to single character
  87. // const CString& operator=(TCHAR ch);
  88. #ifdef _UNICODE
  89. const CString& operator=(char ch);
  90. #endif
  91. // copy string content from ANSI string (converts to TCHAR)
  92. const CString& operator=(LPCSTR lpsz);
  93. // copy string content from UNICODE string (converts to TCHAR)
  94. const CString& operator=(LPCWSTR lpsz);
  95. // copy string content from unsigned chars
  96. const CString& operator=(const unsigned char* psz);
  97. // string concatenation
  98. // concatenate from another CString
  99. const CString& operator+=(const CString& string);
  100. // concatenate a single character
  101. const CString& operator+=(TCHAR ch);
  102. #ifdef _UNICODE
  103. // concatenate an ANSI character after converting it to TCHAR
  104. const CString& operator+=(char ch);
  105. #endif
  106. // concatenate a UNICODE character after converting it to TCHAR
  107. const CString& operator+=(LPCTSTR lpsz);
  108. friend CString AFXAPI operator+(const CString& string1,
  109. const CString& string2);
  110. friend CString AFXAPI operator+(const CString& string, TCHAR ch);
  111. friend CString AFXAPI operator+(TCHAR ch, const CString& string);
  112. #ifdef _UNICODE
  113. friend CString AFXAPI operator+(const CString& string, char ch);
  114. friend CString AFXAPI operator+(char ch, const CString& string);
  115. #endif
  116. friend CString AFXAPI operator+(const CString& string, LPCTSTR lpsz);
  117. friend CString AFXAPI operator+(LPCTSTR lpsz, const CString& string);
  118. // string comparison
  119. // straight character comparison
  120. int Compare(LPCTSTR lpsz) const;
  121. // compare ignoring case
  122. int CompareNoCase(LPCTSTR lpsz) const;
  123. // NLS aware comparison, case sensitive
  124. int Collate(LPCTSTR lpsz) const;
  125. // NLS aware comparison, case insensitive
  126. int CollateNoCase(LPCTSTR lpsz) const;
  127. // simple sub-string extraction
  128. // return nCount characters starting at zero-based nFirst
  129. CString Mid(int nFirst, int nCount) const;
  130. // return all characters starting at zero-based nFirst
  131. CString Mid(int nFirst) const;
  132. // return first nCount characters in string
  133. CString Left(int nCount) const;
  134. // return nCount characters from end of string
  135. CString Right(int nCount) const;
  136. // characters from beginning that are also in passed string
  137. CString SpanIncluding(LPCTSTR lpszCharSet) const;
  138. // characters from beginning that are not also in passed string
  139. CString SpanExcluding(LPCTSTR lpszCharSet) const;
  140. // upper/lower/reverse conversion
  141. // NLS aware conversion to uppercase
  142. void MakeUpper();
  143. // NLS aware conversion to lowercase
  144. void MakeLower();
  145. // reverse string right-to-left
  146. void MakeReverse();
  147. // trimming whitespace (either side)
  148. // remove whitespace starting from right edge
  149. void TrimRight();
  150. // remove whitespace starting from left side
  151. void TrimLeft();
  152. // trimming anything (either side)
  153. // remove continuous occurrences of chTarget starting from right
  154. void TrimRight(TCHAR chTarget);
  155. // remove continuous occcurrences of characters in passed string,
  156. // starting from right
  157. void TrimRight(LPCTSTR lpszTargets);
  158. // remove continuous occurrences of chTarget starting from left
  159. void TrimLeft(TCHAR chTarget);
  160. // remove continuous occcurrences of characters in
  161. // passed string, starting from left
  162. void TrimLeft(LPCTSTR lpszTargets);
  163. // advanced manipulation
  164. // replace occurrences of chOld with chNew
  165. int Replace(TCHAR chOld, TCHAR chNew);
  166. // replace occurrences of substring lpszOld with lpszNew;
  167. // empty lpszNew removes instances of lpszOld
  168. int Replace(LPCTSTR lpszOld, LPCTSTR lpszNew);
  169. // remove occurrences of chRemove
  170. int Remove(TCHAR chRemove);
  171. // insert character at zero-based index; concatenates
  172. // if index is past end of string
  173. int Insert(int nIndex, TCHAR ch);
  174. // insert substring at zero-based index; concatenates
  175. // if index is past end of string
  176. int Insert(int nIndex, LPCTSTR pstr);
  177. // delete nCount characters starting at zero-based index
  178. int Delete(int nIndex, int nCount = 1);
  179. // searching
  180. // find character starting at left, -1 if not found
  181. int Find(TCHAR ch) const;
  182. // find character starting at right
  183. int ReverseFind(TCHAR ch) const;
  184. // find character starting at zero-based index and going right
  185. int Find(TCHAR ch, int nStart) const;
  186. // find first instance of any character in passed string
  187. int FindOneOf(LPCTSTR lpszCharSet) const;
  188. // find first instance of substring
  189. int Find(LPCTSTR lpszSub) const;
  190. // find first instance of substring starting at zero-based index
  191. int Find(LPCTSTR lpszSub, int nStart) const;
  192. // simple formatting
  193. // printf-like formatting using passed string
  194. void AFX_CDECL Format(LPCTSTR lpszFormat, ...);
  195. // printf-like formatting using referenced string resource
  196. void AFX_CDECL Format(UINT nFormatID, ...);
  197. // printf-like formatting using variable arguments parameter
  198. void FormatV(LPCTSTR lpszFormat, va_list argList);
  199. // formatting for localization (uses FormatMessage API)
  200. // format using FormatMessage API on passed string
  201. void AFX_CDECL FormatMessage(LPCTSTR lpszFormat, ...);
  202. // format using FormatMessage API on referenced string resource
  203. void AFX_CDECL FormatMessage(UINT nFormatID, ...);
  204. // input and output
  205. //#ifdef _DEBUG
  206. // friend CDumpContext& AFXAPI operator<<(CDumpContext& dc,
  207. // const CString& string);
  208. //#endif
  209. // friend CArchive& AFXAPI operator<<(CArchive& ar, const CString& string);
  210. // friend CArchive& AFXAPI operator>>(CArchive& ar, CString& string);
  211. // load from string resource
  212. BOOL LoadString(UINT nID);
  213. #ifndef _UNICODE
  214. // ANSI <-> OEM support (convert string in place)
  215. // convert string from ANSI to OEM in-place
  216. void AnsiToOem();
  217. // convert string from OEM to ANSI in-place
  218. void OemToAnsi();
  219. #endif
  220. //#ifndef _AFX_NO_BSTR_SUPPORT
  221. // OLE BSTR support (use for OLE automation)
  222. // return a BSTR initialized with this CString's data
  223. // BSTR AllocSysString() const;
  224. // reallocates the passed BSTR, copies content of this CString to it
  225. // BSTR SetSysString(BSTR* pbstr) const;
  226. //#endif
  227. // Access to string implementation buffer as "C" character array
  228. // get pointer to modifiable buffer at least as long as nMinBufLength
  229. LPTSTR GetBuffer(int nMinBufLength);
  230. // release buffer, setting length to nNewLength (or to first nul if -1)
  231. void ReleaseBuffer(int nNewLength = -1);
  232. // get pointer to modifiable buffer exactly as long as nNewLength
  233. LPTSTR GetBufferSetLength(int nNewLength);
  234. // release memory allocated to but unused by string
  235. void FreeExtra();
  236. // Use LockBuffer/UnlockBuffer to turn refcounting off
  237. // turn refcounting back on
  238. LPTSTR LockBuffer();
  239. // turn refcounting off
  240. void UnlockBuffer();
  241. // Implementation
  242. public:
  243. ~CString();
  244. int GetAllocLength() const;
  245. protected:
  246. LPTSTR m_pchData; // pointer to ref counted string data
  247. // implementation helpers
  248. CStringData* GetData() const;
  249. void Init();
  250. void AllocCopy(CString& dest, int nCopyLen, int nCopyIndex, int nExtraLen) const;
  251. void AllocBuffer(int nLen);
  252. void AssignCopy(int nSrcLen, LPCTSTR lpszSrcData);
  253. void ConcatCopy(int nSrc1Len, LPCTSTR lpszSrc1Data, int nSrc2Len, LPCTSTR lpszSrc2Data);
  254. void ConcatInPlace(int nSrcLen, LPCTSTR lpszSrcData);
  255. void CopyBeforeWrite();
  256. void AllocBeforeWrite(int nLen);
  257. void Release();
  258. static void PASCAL Release(CStringData* pData);
  259. static int PASCAL SafeStrlen(LPCTSTR lpsz);
  260. static void FASTCALL FreeData(CStringData* pData);
  261. };
  262. // Compare helpers
  263. bool AFXAPI operator==(const CString& s1, const CString& s2);
  264. bool AFXAPI operator==(const CString& s1, LPCTSTR s2);
  265. bool AFXAPI operator==(LPCTSTR s1, const CString& s2);
  266. bool AFXAPI operator!=(const CString& s1, const CString& s2);
  267. bool AFXAPI operator!=(const CString& s1, LPCTSTR s2);
  268. bool AFXAPI operator!=(LPCTSTR s1, const CString& s2);
  269. bool AFXAPI operator<(const CString& s1, const CString& s2);
  270. bool AFXAPI operator<(const CString& s1, LPCTSTR s2);
  271. bool AFXAPI operator<(LPCTSTR s1, const CString& s2);
  272. bool AFXAPI operator>(const CString& s1, const CString& s2);
  273. bool AFXAPI operator>(const CString& s1, LPCTSTR s2);
  274. bool AFXAPI operator>(LPCTSTR s1, const CString& s2);
  275. bool AFXAPI operator<=(const CString& s1, const CString& s2);
  276. bool AFXAPI operator<=(const CString& s1, LPCTSTR s2);
  277. bool AFXAPI operator<=(LPCTSTR s1, const CString& s2);
  278. bool AFXAPI operator>=(const CString& s1, const CString& s2);
  279. bool AFXAPI operator>=(const CString& s1, LPCTSTR s2);
  280. bool AFXAPI operator>=(LPCTSTR s1, const CString& s2);
  281. //////////////////////////////////////////////////////////////////////////////
  282. // CString inline functions
  283. _AFX_INLINE CStringData* CString::GetData() const
  284. { ASSERT(m_pchData != NULL); return ((CStringData*)m_pchData)-1; }
  285. _AFX_INLINE void CString::Init()
  286. { m_pchData = afxEmptyString.m_pchData; }
  287. _AFX_INLINE CString::CString()
  288. { m_pchData = afxEmptyString.m_pchData; }
  289. _AFX_INLINE CString::CString(const unsigned char* lpsz)
  290. { Init(); *this = (LPCSTR)lpsz; }
  291. _AFX_INLINE const CString& CString::operator=(const unsigned char* lpsz)
  292. { *this = (LPCSTR)lpsz; return *this; }
  293. _AFX_INLINE int CString::GetLength() const
  294. { return GetData()->nDataLength; }
  295. _AFX_INLINE int CString::GetAllocLength() const
  296. { return GetData()->nAllocLength; }
  297. _AFX_INLINE BOOL CString::IsEmpty() const
  298. { return GetData()->nDataLength == 0; }
  299. _AFX_INLINE CString::operator LPCTSTR() const
  300. { return m_pchData; }
  301. _AFX_INLINE int PASCAL CString::SafeStrlen(LPCTSTR lpsz)
  302. { return (lpsz == NULL) ? 0 : lstrlen(lpsz); }
  303. _AFX_INLINE int CString::Compare(LPCTSTR lpsz) const
  304. { return lstrcmp(m_pchData, lpsz); } // MBCS/Unicode aware
  305. _AFX_INLINE int CString::CompareNoCase(LPCTSTR lpsz) const
  306. { return lstrcmpi(m_pchData, lpsz); } // MBCS/Unicode aware
  307. _AFX_INLINE TCHAR CString::GetAt(int nIndex) const
  308. {
  309. ASSERT(nIndex >= 0);
  310. ASSERT(nIndex < GetData()->nDataLength);
  311. return m_pchData[nIndex];
  312. }
  313. _AFX_INLINE TCHAR CString::operator[](int nIndex) const
  314. {
  315. // same as GetAt
  316. ASSERT(nIndex >= 0);
  317. ASSERT(nIndex < GetData()->nDataLength);
  318. return m_pchData[nIndex];
  319. }
  320. _AFX_INLINE bool AFXAPI operator==(const CString& s1, const CString& s2)
  321. { return s1.Compare(s2) == 0; }
  322. _AFX_INLINE bool AFXAPI operator==(const CString& s1, LPCTSTR s2)
  323. { return s1.Compare(s2) == 0; }
  324. _AFX_INLINE bool AFXAPI operator==(LPCTSTR s1, const CString& s2)
  325. { return s2.Compare(s1) == 0; }
  326. _AFX_INLINE bool AFXAPI operator!=(const CString& s1, const CString& s2)
  327. { return s1.Compare(s2) != 0; }
  328. _AFX_INLINE bool AFXAPI operator!=(const CString& s1, LPCTSTR s2)
  329. { return s1.Compare(s2) != 0; }
  330. _AFX_INLINE bool AFXAPI operator!=(LPCTSTR s1, const CString& s2)
  331. { return s2.Compare(s1) != 0; }
  332. _AFX_INLINE bool AFXAPI operator<(const CString& s1, const CString& s2)
  333. { return s1.Compare(s2) < 0; }
  334. _AFX_INLINE bool AFXAPI operator<(const CString& s1, LPCTSTR s2)
  335. { return s1.Compare(s2) < 0; }
  336. _AFX_INLINE bool AFXAPI operator<(LPCTSTR s1, const CString& s2)
  337. { return s2.Compare(s1) > 0; }
  338. _AFX_INLINE bool AFXAPI operator>(const CString& s1, const CString& s2)
  339. { return s1.Compare(s2) > 0; }
  340. _AFX_INLINE bool AFXAPI operator>(const CString& s1, LPCTSTR s2)
  341. { return s1.Compare(s2) > 0; }
  342. _AFX_INLINE bool AFXAPI operator>(LPCTSTR s1, const CString& s2)
  343. { return s2.Compare(s1) < 0; }
  344. _AFX_INLINE bool AFXAPI operator<=(const CString& s1, const CString& s2)
  345. { return s1.Compare(s2) <= 0; }
  346. _AFX_INLINE bool AFXAPI operator<=(const CString& s1, LPCTSTR s2)
  347. { return s1.Compare(s2) <= 0; }
  348. _AFX_INLINE bool AFXAPI operator<=(LPCTSTR s1, const CString& s2)
  349. { return s2.Compare(s1) >= 0; }
  350. _AFX_INLINE bool AFXAPI operator>=(const CString& s1, const CString& s2)
  351. { return s1.Compare(s2) >= 0; }
  352. _AFX_INLINE bool AFXAPI operator>=(const CString& s1, LPCTSTR s2)
  353. { return s1.Compare(s2) >= 0; }
  354. _AFX_INLINE bool AFXAPI operator>=(LPCTSTR s1, const CString& s2)
  355. { return s2.Compare(s1) <= 0; }
  356. //////////////////////////////////////////////////////////////////////////////
  357. // CWinThread
  358. typedef DWORD (WINAPI *AFX_THREADPROC)(LPVOID);
  359. class CWinThread
  360. {
  361. // DECLARE_DYNAMIC(CWinThread)
  362. public:
  363. // Constructors
  364. CWinThread();
  365. BOOL CreateThread(DWORD dwCreateFlags = 0, UINT nStackSize = 0,
  366. LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL);
  367. // Attributes
  368. // only valid while running
  369. HANDLE m_hThread; // this thread's HANDLE
  370. operator HANDLE() const;
  371. DWORD m_nThreadID; // this thread's ID
  372. int GetThreadPriority();
  373. BOOL SetThreadPriority(int nPriority);
  374. // Operations
  375. DWORD SuspendThread();
  376. DWORD ResumeThread();
  377. BOOL PostThreadMessage(UINT message, WPARAM wParam, LPARAM lParam);
  378. // Overridables
  379. // thread initialization
  380. virtual BOOL InitInstance();
  381. // thread termination
  382. virtual int ExitInstance(); // default will 'delete this'
  383. // Implementation
  384. public:
  385. virtual ~CWinThread();
  386. void CommonConstruct();
  387. virtual void Delete();
  388. public:
  389. // constructor used by implementation of AfxBeginThread
  390. CWinThread(AFX_THREADPROC pfnThreadProc, LPVOID pParam);
  391. // valid after construction
  392. LPVOID m_pThreadParams; // generic parameters passed to starting function
  393. AFX_THREADPROC m_pfnThreadProc;
  394. };
  395. // global helpers for threads
  396. CWinThread* AFXAPI AfxBeginThread(AFX_THREADPROC pfnThreadProc, LPVOID pParam,
  397. int nPriority = THREAD_PRIORITY_NORMAL, UINT nStackSize = 0,
  398. DWORD dwCreateFlags = 0, LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL);
  399. //CWinThread* AFXAPI AfxBeginThread(CRuntimeClass* pThreadClass,
  400. // int nPriority = THREAD_PRIORITY_NORMAL, UINT nStackSize = 0,
  401. // DWORD dwCreateFlags = 0, LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL);
  402. CWinThread* AFXAPI AfxGetThread();
  403. void AFXAPI AfxEndThread(UINT nExitCode, BOOL bDelete = TRUE);
  404. void AFXAPI AfxInitThread();
  405. void AFXAPI AfxTermThread(HINSTANCE hInstTerm = NULL);
  406. //////////////////////////////////////////////////////////////////////////////
  407. // CStringArray
  408. class CStringArray // : public CObject
  409. {
  410. // DECLARE_SERIAL(CStringArray)
  411. public:
  412. // Construction
  413. CStringArray();
  414. // Attributes
  415. int GetSize() const;
  416. int GetUpperBound() const;
  417. void SetSize(int nNewSize, int nGrowBy = -1);
  418. // Operations
  419. // Clean up
  420. void FreeExtra();
  421. void RemoveAll();
  422. // Accessing elements
  423. CString GetAt(int nIndex) const;
  424. void SetAt(int nIndex, LPCTSTR newElement);
  425. void SetAt(int nIndex, const CString& newElement);
  426. CString& ElementAt(int nIndex);
  427. // Direct Access to the element data (may return NULL)
  428. const CString* GetData() const;
  429. CString* GetData();
  430. // Potentially growing the array
  431. void SetAtGrow(int nIndex, LPCTSTR newElement);
  432. void SetAtGrow(int nIndex, const CString& newElement);
  433. int Add(LPCTSTR newElement);
  434. int Add(const CString& newElement);
  435. int Append(const CStringArray& src);
  436. void Copy(const CStringArray& src);
  437. // overloaded operator helpers
  438. CString operator[](int nIndex) const;
  439. CString& operator[](int nIndex);
  440. // Operations that move elements around
  441. void InsertAt(int nIndex, LPCTSTR newElement, int nCount = 1);
  442. void InsertAt(int nIndex, const CString& newElement, int nCount = 1);
  443. void RemoveAt(int nIndex, int nCount = 1);
  444. void InsertAt(int nStartIndex, CStringArray* pNewArray);
  445. // Implementation
  446. protected:
  447. CString* m_pData; // the actual array of data
  448. int m_nSize; // # of elements (upperBound - 1)
  449. int m_nMaxSize; // max allocated
  450. int m_nGrowBy; // grow amount
  451. void InsertEmpty(int nIndex, int nCount);
  452. public:
  453. ~CStringArray();
  454. // void Serialize(CArchive&);
  455. #ifdef _DEBUG
  456. // void Dump(CDumpContext&) const;
  457. // void AssertValid() const;
  458. #endif
  459. protected:
  460. // local typedefs for class templates
  461. typedef CString BASE_TYPE;
  462. typedef LPCTSTR BASE_ARG_TYPE;
  463. };
  464. ////////////////////////////////////////////////////////////////////////////
  465. #ifndef _AFXCOLL_INLINE
  466. #define _AFXCOLL_INLINE inline
  467. #endif
  468. _AFXCOLL_INLINE int CStringArray::GetSize() const
  469. { return m_nSize; }
  470. _AFXCOLL_INLINE int CStringArray::GetUpperBound() const
  471. { return m_nSize-1; }
  472. _AFXCOLL_INLINE void CStringArray::RemoveAll()
  473. { SetSize(0); }
  474. _AFXCOLL_INLINE CString CStringArray::GetAt(int nIndex) const
  475. { ASSERT(nIndex >= 0 && nIndex < m_nSize);
  476. return m_pData[nIndex]; }
  477. _AFXCOLL_INLINE void CStringArray::SetAt(int nIndex, LPCTSTR newElement)
  478. { ASSERT(nIndex >= 0 && nIndex < m_nSize);
  479. m_pData[nIndex] = newElement; }
  480. _AFXCOLL_INLINE void CStringArray::SetAt(int nIndex, const CString& newElement)
  481. { ASSERT(nIndex >= 0 && nIndex < m_nSize);
  482. m_pData[nIndex] = newElement; }
  483. _AFXCOLL_INLINE CString& CStringArray::ElementAt(int nIndex)
  484. { ASSERT(nIndex >= 0 && nIndex < m_nSize);
  485. return m_pData[nIndex]; }
  486. _AFXCOLL_INLINE const CString* CStringArray::GetData() const
  487. { return (const CString*)m_pData; }
  488. _AFXCOLL_INLINE CString* CStringArray::GetData()
  489. { return (CString*)m_pData; }
  490. _AFXCOLL_INLINE int CStringArray::Add(LPCTSTR newElement)
  491. { int nIndex = m_nSize;
  492. SetAtGrow(nIndex, newElement);
  493. return nIndex; }
  494. _AFXCOLL_INLINE int CStringArray::Add(const CString& newElement)
  495. { int nIndex = m_nSize;
  496. SetAtGrow(nIndex, newElement);
  497. return nIndex; }
  498. _AFXCOLL_INLINE CString CStringArray::operator[](int nIndex) const
  499. { return GetAt(nIndex); }
  500. _AFXCOLL_INLINE CString& CStringArray::operator[](int nIndex)
  501. { return ElementAt(nIndex); }
  502. /////////////////////////////////////////////////////////////////////////////
  503. // CPtrArray
  504. class CPtrArray // : public CObject
  505. {
  506. // DECLARE_DYNAMIC(CPtrArray)
  507. public:
  508. // Construction
  509. CPtrArray();
  510. // Attributes
  511. int GetSize() const;
  512. int GetUpperBound() const;
  513. void SetSize(int nNewSize, int nGrowBy = -1);
  514. // Operations
  515. // Clean up
  516. void FreeExtra();
  517. void RemoveAll();
  518. // Accessing elements
  519. void* GetAt(int nIndex) const;
  520. void SetAt(int nIndex, void* newElement);
  521. void*& ElementAt(int nIndex);
  522. // Direct Access to the element data (may return NULL)
  523. const void** GetData() const;
  524. void** GetData();
  525. // Potentially growing the array
  526. void SetAtGrow(int nIndex, void* newElement);
  527. int Add(void* newElement);
  528. int Append(const CPtrArray& src);
  529. void Copy(const CPtrArray& src);
  530. // overloaded operator helpers
  531. void* operator[](int nIndex) const;
  532. void*& operator[](int nIndex);
  533. // Operations that move elements around
  534. void InsertAt(int nIndex, void* newElement, int nCount = 1);
  535. void RemoveAt(int nIndex, int nCount = 1);
  536. void InsertAt(int nStartIndex, CPtrArray* pNewArray);
  537. // Implementation
  538. protected:
  539. void** m_pData; // the actual array of data
  540. int m_nSize; // # of elements (upperBound - 1)
  541. int m_nMaxSize; // max allocated
  542. int m_nGrowBy; // grow amount
  543. public:
  544. ~CPtrArray();
  545. #ifdef _DEBUG
  546. // void Dump(CDumpContext&) const;
  547. // void AssertValid() const;
  548. #endif
  549. protected:
  550. // local typedefs for class templates
  551. typedef void* BASE_TYPE;
  552. typedef void* BASE_ARG_TYPE;
  553. };
  554. ////////////////////////////////////////////////////////////////////////////
  555. _AFXCOLL_INLINE int CPtrArray::GetSize() const
  556. { return m_nSize; }
  557. _AFXCOLL_INLINE int CPtrArray::GetUpperBound() const
  558. { return m_nSize-1; }
  559. _AFXCOLL_INLINE void CPtrArray::RemoveAll()
  560. { SetSize(0); }
  561. _AFXCOLL_INLINE void* CPtrArray::GetAt(int nIndex) const
  562. { ASSERT(nIndex >= 0 && nIndex < m_nSize);
  563. return m_pData[nIndex]; }
  564. _AFXCOLL_INLINE void CPtrArray::SetAt(int nIndex, void* newElement)
  565. { ASSERT(nIndex >= 0 && nIndex < m_nSize);
  566. m_pData[nIndex] = newElement; }
  567. _AFXCOLL_INLINE void*& CPtrArray::ElementAt(int nIndex)
  568. { ASSERT(nIndex >= 0 && nIndex < m_nSize);
  569. return m_pData[nIndex]; }
  570. _AFXCOLL_INLINE const void** CPtrArray::GetData() const
  571. { return (const void**)m_pData; }
  572. _AFXCOLL_INLINE void** CPtrArray::GetData()
  573. { return (void**)m_pData; }
  574. _AFXCOLL_INLINE int CPtrArray::Add(void* newElement)
  575. { int nIndex = m_nSize;
  576. SetAtGrow(nIndex, newElement);
  577. return nIndex; }
  578. _AFXCOLL_INLINE void* CPtrArray::operator[](int nIndex) const
  579. { return GetAt(nIndex); }
  580. _AFXCOLL_INLINE void*& CPtrArray::operator[](int nIndex)
  581. { return ElementAt(nIndex); }
  582. /////////////////////////////////////////////////////////////////////////////
  583. // CTypedPtrArray<BASE_CLASS, TYPE>
  584. template<class BASE_CLASS, class TYPE>
  585. class CTypedPtrArray : public BASE_CLASS
  586. {
  587. public:
  588. // Accessing elements
  589. TYPE GetAt(int nIndex) const
  590. { return (TYPE)BASE_CLASS::GetAt(nIndex); }
  591. TYPE& ElementAt(int nIndex)
  592. { return (TYPE&)BASE_CLASS::ElementAt(nIndex); }
  593. void SetAt(int nIndex, TYPE ptr)
  594. { BASE_CLASS::SetAt(nIndex, ptr); }
  595. // Potentially growing the array
  596. void SetAtGrow(int nIndex, TYPE newElement)
  597. { BASE_CLASS::SetAtGrow(nIndex, newElement); }
  598. int Add(TYPE newElement)
  599. { return BASE_CLASS::Add(newElement); }
  600. int Append(const CTypedPtrArray<BASE_CLASS, TYPE>& src)
  601. { return BASE_CLASS::Append(src); }
  602. void Copy(const CTypedPtrArray<BASE_CLASS, TYPE>& src)
  603. { BASE_CLASS::Copy(src); }
  604. // Operations that move elements around
  605. void InsertAt(int nIndex, TYPE newElement, int nCount = 1)
  606. { BASE_CLASS::InsertAt(nIndex, newElement, nCount); }
  607. void InsertAt(int nStartIndex, CTypedPtrArray<BASE_CLASS, TYPE>* pNewArray)
  608. { BASE_CLASS::InsertAt(nStartIndex, pNewArray); }
  609. // overloaded operator helpers
  610. TYPE operator[](int nIndex) const
  611. { return (TYPE)BASE_CLASS::operator[](nIndex); }
  612. TYPE& operator[](int nIndex)
  613. { return (TYPE&)BASE_CLASS::operator[](nIndex); }
  614. };
  615. //////////////////////////////////////////////////////////////////////////////