XTPCalendarPtrCollectionT.h
上传用户:szled88
上传日期:2015-04-09
资源大小:43957k
文件大小:22k
源码类别:

对话框与窗口

开发平台:

Visual C++

  1. // XTPCalendarPtrCollectionT.h: CXTPCalendarPtrCollectionT template.
  2. //
  3. // This file is a part of the XTREME CALENDAR MFC class library.
  4. // (c)1998-2008 Codejock Software, All Rights Reserved.
  5. //
  6. // THIS SOURCE FILE IS THE PROPERTY OF CODEJOCK SOFTWARE AND IS NOT TO BE
  7. // RE-DISTRIBUTED BY ANY MEANS WHATSOEVER WITHOUT THE EXPRESSED WRITTEN
  8. // CONSENT OF CODEJOCK SOFTWARE.
  9. //
  10. // THIS SOURCE CODE CAN ONLY BE USED UNDER THE TERMS AND CONDITIONS OUTLINED
  11. // IN THE XTREME TOOLKIT PRO LICENSE AGREEMENT. CODEJOCK SOFTWARE GRANTS TO
  12. // YOU (ONE SOFTWARE DEVELOPER) THE LIMITED RIGHT TO USE THIS SOFTWARE ON A
  13. // SINGLE COMPUTER.
  14. //
  15. // CONTACT INFORMATION:
  16. // support@codejock.com
  17. // http://www.codejock.com
  18. //
  19. /////////////////////////////////////////////////////////////////////////////
  20. //{{AFX_CODEJOCK_PRIVATE
  21. #if !defined(_XTPCALENDARCOLLECTIONT_H__)
  22. #define _XTPCALENDARCOLLECTIONT_H__
  23. //}}AFX_CODEJOCK_PRIVATE
  24. #if _MSC_VER > 1000
  25. #pragma once
  26. #endif // _MSC_VER > 1000
  27. #pragma warning(disable: 4097)
  28. #include "Common/XTPSmartPtrInternalT.h"
  29. //===========================================================================
  30. // Summary:
  31. //     This class represents a simple array collection of objects.
  32. // Remarks:
  33. //     Array indexes always start at position 0.
  34. //
  35. //          As with a C array, the access time for indexed element of this
  36. //          array is constant and is independent of the array size.
  37. // See Also:  overview, CArray overview
  38. //===========================================================================
  39. template<class _TObject>
  40. class CXTPCalendarPtrCollectionT : public CXTPCmdTarget
  41. {
  42. public:
  43. //-----------------------------------------------------------------------
  44. // Summary:
  45. //     Default collection constructor.
  46. // See Also: ~CXTPCalendarPtrCollectionT()
  47. //-----------------------------------------------------------------------
  48. CXTPCalendarPtrCollectionT();
  49. //-----------------------------------------------------------------------
  50. // Summary:
  51. //     Default collection destructor.
  52. // Remarks:
  53. //     Handles member items deallocation. Decreases reference of all
  54. //     stored  objects.
  55. // See Also: RemoveAll()
  56. //-----------------------------------------------------------------------
  57. virtual ~CXTPCalendarPtrCollectionT();
  58. //-----------------------------------------------------------------------
  59. // Summary:
  60. //     This member function is used to obtain the number of elements
  61. //     in the collection.
  62. // Remarks:
  63. //     Call this method to retrieve the number of elements in the array.
  64. //     Because indexes are zero-based, the size is 1 greater than
  65. //     the largest index.
  66. // Example: See example for CXTPCalendarEvents::Add method.
  67. // Returns:
  68. //     The number of items in the collection.
  69. //-----------------------------------------------------------------------
  70. virtual int GetCount() const;
  71. //-----------------------------------------------------------------------
  72. // Summary:
  73. //     This member function adds a new element to the end of the array.
  74. // Parameters:
  75. //     pNewElement - A pointer to a _TObjectThe object. Element to add to the array.
  76. //     bWithAddRef - A BOOL.  If this parameter is TRUE then InternalAddRef()
  77. //                   is called for pNewElement, otherwise it
  78. //                   is not called.
  79. // Remarks:
  80. //     Use this method to add the specified pointer to the end
  81. //     of the events collection. Reference to the added object is
  82. //     increased.
  83. // Example:
  84. // <code>
  85. // CXTPCalendarPtrCollectionT<CXTPCalendarEvent> arList;
  86. // CXTPCalendarEventPtr ptrEvent1 = new CXTPCalendarEvent()
  87. //
  88. // // Add() will call InernalAddRef()
  89. // arList.Add(ptrEvent1);
  90. //
  91. // // Add() will not call InernalAddRef()
  92. // arList.Add(new CXTPCalendarEvent(), FALSE);
  93. //
  94. // // GetAt() will call InernalAddRef()
  95. // CXTPCalendarEventPtr ptrEvent0 = arList.GetAt(0);
  96. //
  97. // // GetAt() will not call InernalAddRef()
  98. // CXTPCalendarEvent* pEvent1 = arList.GetAt(1, FALSE);
  99. //
  100. // ASSERT(2 == arList.GetCount());
  101. //
  102. // //RemoveAll() will call InernalRelease() for all objects
  103. // arList.RemoveAll();
  104. //
  105. // ASSERT(0 == arList.GetCount());
  106. //
  107. // Version 2
  108. //
  109. // CXTPCalendarPtrCollectionT<CXTPCalendarEvent> arList;
  110. // CXTPCalendarEventPtr ptrEvent1 = new CXTPCalendarEvent()
  111. //
  112. // // Add() will call InernalAddRef()
  113. // arList.Add(ptrEvent1);
  114. //
  115. // // Add() will not call InernalAddRef()
  116. // arList.Add(new CXTPCalendarEvent(), FALSE);
  117. //
  118. // // GetAt() will call InernalAddRef()
  119. // CXTPCalendarEventPtr ptrEvent0 = arList.GetAt(0);
  120. //
  121. // // GetAt() will not call InernalAddRef()
  122. // CXTPCalendarEvent* pEvent1 = arList.GetAt(1, FALSE);
  123. //
  124. // ASSERT(2 == arList.GetCount());
  125. //
  126. // //RemoveAll() will call InernalRelease() for all objects
  127. // arList.RemoveAll();
  128. //
  129. // ASSERT(0 == arList.GetCount());
  130. // </code>
  131. // See Also: CXTPCalendarPtrCollectionT overview, GetAt, RemoveAll,
  132. //           GetCount
  133. //-----------------------------------------------------------------------
  134. virtual void Add(_TObject* pNewElement);
  135. virtual void Add(_TObject* pNewElement, BOOL bWithAddRef); // <combine CXTPCalendarPtrCollectionT::Add@_TObject*>
  136. //-----------------------------------------------------------------------
  137. // Summary:
  138. //     This member function is used to set a new element to the position
  139. //     specified in the nIndex parameter.
  140. // Parameters:
  141. //     nIndex      - An integer index that is greater than or equal
  142. //                   to 0 and less than the value returned by
  143. //                   GetCount.
  144. //     pElement    - A pointer to a _TObject object. The element to add to the array.
  145. // Remarks:
  146. //     Use this method to set the specified element to the
  147. //     position specified in nIndex parameter.
  148. //     Reference to the previous object is decreased.
  149. //     Reference to the new object is increased.
  150. // Example:
  151. // <code>
  152. // CXTPCalendarPtrCollectionT<CXTPCalendarEvent> arList;
  153. // CXTPCalendarEventPtr ptrEvent1 = new CXTPCalendarEvent()
  154. //
  155. // arList.Add(ptrEvent1);
  156. // arList.SetAt(0, new CXTPCalendarEvent());
  157. // </code>
  158. // See Also: GetAt, Add, InsertAt
  159. //-----------------------------------------------------------------------
  160. virtual void SetAt(int nIndex, _TObject* pElement);
  161. //-----------------------------------------------------------------------
  162. // Summary:
  163. //      This member function finds element index in the collection using
  164. //      its pointer.
  165. // Parameters:
  166. //      pElement - A pointer to a _TObject object. The element to find in
  167. //                 the array.
  168. // Returns:
  169. //      -1 if element is not found, otherwise an integer index that is
  170. //      greater than or equal to 0 and less than the value returned
  171. //      by GetCount. The specified element currently at this index.
  172. //-----------------------------------------------------------------------
  173. virtual int Find(const _TObject* pElement);
  174. //-----------------------------------------------------------------------
  175. // Summary:
  176. //     This member function is used to insert a new element at the
  177. //     position specified in the nIndex parameter.
  178. // Parameters:
  179. //     nIndex      - An integer index that is greater than or equal
  180. //                   to 0 and less than the value returned by
  181. //                   GetCount().
  182. //     pElement    - A pointer to a _TObject object. The element to add to the array.
  183. // Remarks:
  184. //     Use this method to insert the specified element t the
  185. //     position specified in the nIndex parameter.
  186. //     Reference to the new object is increased.
  187. // Example:
  188. // <code>
  189. // CXTPCalendarPtrCollectionT<CXTPCalendarEvent> arList;
  190. // CXTPCalendarEventPtr ptrEvent1 = new CXTPCalendarEvent()
  191. //
  192. // arList.Add(ptrEvent1);
  193. // arList.InsertAt(0, new CXTPCalendarEvent());
  194. // </code>
  195. // See Also: GetAt, Add, InsertAt
  196. //-----------------------------------------------------------------------
  197. virtual void InsertAt(int nIndex, _TObject* pElement);
  198. //-----------------------------------------------------------------------
  199. // Summary:
  200. //     This member function is used to add a new elements to the end
  201. //     of the array.
  202. // Parameters:
  203. //     pElementsArray - A pointer to an array of _TObject objects.
  204. //                      A pointer to the collection to add to the array.
  205. // Remarks:
  206. //     Use this method to add the elements from the specified
  207. //     array to the end of the collection. Reference to the
  208. //     new object is increased.
  209. // Example:
  210. // <code>
  211. // CXTPCalendarPtrCollectionT<CXTPCalendarEvent> arList1;
  212. // CXTPCalendarPtrCollectionT<CXTPCalendarEvent> arList2;
  213. // ...
  214. // arList1.Append(&arList2);
  215. // </code>
  216. // See Also: GetAt, Add, InsertAt, SetAt
  217. //-----------------------------------------------------------------------
  218. virtual void Append(CXTPCalendarPtrCollectionT<_TObject>* pElementsArray);
  219. //-----------------------------------------------------------------------
  220. // Summary:
  221. //     This member function is used to change the array size.
  222. // Parameters:
  223. //     nNewSize - An int that contains the new array size.
  224. // Remarks:
  225. //     Argument nNewSize contains the new array size (number of elements).
  226. //     Must be greater than or equal to 0.
  227. //-----------------------------------------------------------------------
  228. virtual void SetSize(int nNewSize);
  229. //-----------------------------------------------------------------------
  230. // Summary:
  231. //     This member function returns an element at the specified
  232. //     numeric index. Reference to the returned object is increased.
  233. // Parameters:
  234. //     nIndex - An integer index that is greater than or equal to 0
  235. //              and less than the value returned by GetCount.
  236. //     bWithAddRef - A BOOL. If this parameter is TRUE then InternalAddRef()
  237. //                   is called for the returned object,
  238. //                   otherwise it is not called.
  239. // Remarks:
  240. //     Returns the array element at the specified index.
  241. // Example: See the example for the Add() method.
  242. // Returns:
  243. //     A pointer to a _TObject object. The element currently at this index.
  244. //-----------------------------------------------------------------------
  245. virtual _TObject* GetAt(int nIndex) const;
  246. virtual _TObject* GetAt(int nIndex, BOOL bWithAddRef) const; // <combine CXTPCalendarPtrCollectionT::GetAt@int@const>
  247. //-----------------------------------------------------------------------
  248. // Summary:
  249. //     This member function removes an element at the specified index
  250. //     from the array.
  251. // Parameters:
  252. //     nIndex - An integer index that is greater than or equal to 0
  253. //              and less than the value returned by GetCount().
  254. // Remarks:
  255. //     Removes the pointer from this array and releases instance
  256. //     of the stored object.
  257. //-----------------------------------------------------------------------
  258. virtual void RemoveAt(int nIndex);
  259. //-----------------------------------------------------------------------
  260. // Summary:
  261. //     This member function finds an element using its pointer and removes
  262. //     it from the array.
  263. // Parameters:
  264. //      pElement - A pointer to a _TObject object. The element to find in
  265. //                 the array.
  266. // Remarks:
  267. //     Removes the pointer from this array and releases instance
  268. //     of the stored object.
  269. // See Also: RemoveAt(), RemoveAll()
  270. //-----------------------------------------------------------------------
  271. virtual void Remove(_TObject* pElement);
  272. //-----------------------------------------------------------------------
  273. // Summary:
  274. //     This member function is used to remove all of the elements from
  275. //     the array.
  276. // Remarks:
  277. //     Removes all of the pointers from this array and releases instances
  278. //     of all stored objects.
  279. // Example: See example for CXTPCalendarEvents::Add method.
  280. // See Also: CXTPCalendarEvents overview
  281. //-----------------------------------------------------------------------
  282. virtual void RemoveAll();
  283. protected:
  284. typedef CXTPSmartPtrInternalT<_TObject> TObjectPtr;  // Smart pointer type for stored objects.
  285. CArray<TObjectPtr, TObjectPtr&> m_arElements;        // Objects storage.
  286. protected:
  287. };
  288. ////////////////////////////////////////////////////////////////////////////
  289. //===========================================================================
  290. // Summary:
  291. //     This class represents a simple array collection of objects pointers.
  292. // Remarks:
  293. //          <b>Parameters</b>
  294. //              BASE_CLASS
  295. //                  Base class of the typed pointer array class;
  296. //                  must be an array class (CObArray or CPtrArray).
  297. //              PTR_TYPE
  298. //                  Type of the elements stored in the base-class array.
  299. //
  300. //          Array indexes always start at position 0.
  301. //
  302. //          As with a C array, the access time for indexed element of this
  303. //          array is constant and is independent of the array size.
  304. //
  305. //          Overridden method RemoveAll() call delete operator for all not NULL
  306. //          elements before call RemoveAll() method from the base class.
  307. //
  308. //          RemoveAll() method is called from array destructor too.
  309. //
  310. // See Also: CTypedPtrArray, CPtrArray, CObArray, CArray
  311. //===========================================================================
  312. template<class BASE_CLASS, class PTR_TYPE>
  313. class CXTPCalendarTypedPtrAutoDeleteArray : public CTypedPtrArray<BASE_CLASS, PTR_TYPE>
  314. {
  315. public:
  316. //------------------------------------------------------------------------
  317. // Summary:
  318. //     Base class type definition
  319. //------------------------------------------------------------------------
  320. typedef CTypedPtrArray<BASE_CLASS, PTR_TYPE> TBase;
  321. //-----------------------------------------------------------------------
  322. // Summary:
  323. //     Default object constructor.
  324. //-----------------------------------------------------------------------
  325. CXTPCalendarTypedPtrAutoDeleteArray() {};
  326. //-----------------------------------------------------------------------
  327. // Summary:
  328. //     Default class destructor.
  329. // Remarks:
  330. //     Handles member items deallocation.
  331. //-----------------------------------------------------------------------
  332. virtual ~CXTPCalendarTypedPtrAutoDeleteArray() {
  333. RemoveAll();
  334. }
  335. //-----------------------------------------------------------------------
  336. // Summary:
  337. //     Removes all the elements from the array.
  338. // Remarks:
  339. //     Removes all the pointers from the array and deletes
  340. //     all stored objects.
  341. //-----------------------------------------------------------------------
  342. virtual void RemoveAll()
  343. {
  344. for(int i = 0; i < GetSize(); i++)
  345. {
  346. PTR_TYPE pObj = GetAt(i);
  347. if (pObj) {
  348. delete pObj;
  349. SetAt(i, NULL);
  350. }
  351. }
  352. TBase::RemoveAll();
  353. }
  354. };
  355. //===========================================================================
  356. // Summary:
  357. //     This class represents a simple map of typed keys to typed objects pointers.
  358. //     Overridden methods SetAt, operator[], RemoveKey, RemoveAll, call delete
  359. //     for previously stored elements.
  360. // Remarks:
  361. //          <b>Parameters</b>
  362. //              KEY
  363. //                  class to key values.
  364. //              TPtr
  365. //                  A pointer type of stored objects.
  366. //
  367. //          Overridden method RemoveAll() call delete operator for all not NULL
  368. //          spored elements before call RemoveAll() method from the base class.
  369. //
  370. //          RemoveAll() method is called from map destructor too.
  371. //
  372. // See Also: CMap
  373. //===========================================================================
  374. template<class KEY, class TPtr>
  375. class CXTPCalendarTypedPtrAutoDeleteMap : public CMap<KEY, KEY, TPtr, TPtr>
  376. {
  377. //{{AFX_CODEJOCK_PRIVATE
  378. typedef CMap<KEY, KEY, TPtr, TPtr> TBase;
  379. //}}AFX_CODEJOCK_PRIVATE
  380. public:
  381. //-----------------------------------------------------------------------
  382. // Summary:
  383. //     Default class destructor.
  384. // Remarks:
  385. //     Handles member items deallocation.
  386. //-----------------------------------------------------------------------
  387. virtual ~CXTPCalendarTypedPtrAutoDeleteMap()
  388. {
  389. RemoveAll();
  390. }
  391. //-----------------------------------------------------------------------
  392. // Summary:
  393. //     Removes all the elements from the map.
  394. // Parameters:
  395. //     bDeletePtr - If TRUE, delete operator will be called for not null
  396. //                  elements. Default value is TRUE.
  397. // Remarks:
  398. //     Removes all data from the map and deletes all stored objects if
  399. //     this specified by bDeletePtr parameter.
  400. //-----------------------------------------------------------------------
  401. virtual void RemoveAll(BOOL bDeletePtr = TRUE)
  402. {
  403. if (bDeletePtr)
  404. {
  405. POSITION pos = GetStartPosition();
  406. while (pos)
  407. {
  408. KEY tmpKey;
  409. TPtr pValue = NULL;
  410. GetNextAssoc(pos, tmpKey, pValue);
  411. if (pValue)
  412. delete pValue;
  413. }
  414. }
  415. TBase::RemoveAll();
  416. }
  417. //-----------------------------------------------------------------------
  418. // Summary:
  419. //     Add a new (key, value) pair.
  420. // Parameters:
  421. //     key            - A key.
  422. //     pValue         - A value.
  423. //     bDeletePrevPtr - If TRUE, delete operator will be called for previous
  424. //                      not null element.
  425. //-----------------------------------------------------------------------
  426. virtual void SetAt(KEY key, TPtr pValue, BOOL bDeletePrevPtr = TRUE)
  427. {
  428. if (bDeletePrevPtr)
  429. _DeletePtr(key);
  430. TBase::SetAt(key, pValue);
  431. }
  432. //-----------------------------------------------------------------------
  433. // Summary:
  434. //     Lookup and add a new element if not there.
  435. // Parameters:
  436. //     key - A key.
  437. // Returns:
  438. //     A reference for value associated to a key (new value added if need).
  439. //-----------------------------------------------------------------------
  440. TPtr& operator[](KEY key)
  441. {
  442. _DeletePtr(key);
  443. return TBase::operator[](key);
  444. }
  445. //-----------------------------------------------------------------------
  446. // Summary:
  447. //     Removes element by it's key.
  448. // Parameters:
  449. //     key        - A key.
  450. //     bDeletePtr - If TRUE, delete operator will be called for not null
  451. //                  element. Default value is TRUE.
  452. //-----------------------------------------------------------------------
  453. virtual BOOL RemoveKey(KEY key, BOOL bDeletePtr = TRUE)
  454. {
  455. if (bDeletePtr)
  456. _DeletePtr(key);
  457. return TBase::RemoveKey(key);
  458. }
  459. private:
  460. void _DeletePtr(KEY key)
  461. {
  462. TPtr pValPrev = NULL;
  463. if (Lookup(key, pValPrev) && pValPrev)
  464. {
  465. delete pValPrev;
  466. }
  467. }
  468. };
  469. /////////////////////////////////////////////////////////////////////////////
  470. template<class _TObject>
  471. AFX_INLINE CXTPCalendarPtrCollectionT<_TObject>::CXTPCalendarPtrCollectionT()
  472. {
  473. }
  474. template<class _TObject>
  475. AFX_INLINE CXTPCalendarPtrCollectionT<_TObject>::~CXTPCalendarPtrCollectionT()
  476. {
  477. }
  478. template<class _TObject>
  479. AFX_INLINE int CXTPCalendarPtrCollectionT<_TObject>::GetCount() const
  480. {
  481. return (int)m_arElements.GetSize();
  482. }
  483. template<class _TObject>
  484. AFX_INLINE _TObject* CXTPCalendarPtrCollectionT<_TObject>::GetAt(int nIndex) const
  485. {
  486. _TObject* pElement = m_arElements[nIndex];
  487. return pElement;
  488. }
  489. template<class _TObject>
  490. AFX_INLINE _TObject* CXTPCalendarPtrCollectionT<_TObject>::GetAt(int nIndex, BOOL bWithAddRef) const
  491. {
  492. _TObject* pElement = m_arElements[nIndex];
  493. if (bWithAddRef && pElement) {
  494. pElement->InternalAddRef();
  495. }
  496. return pElement;
  497. }
  498. template<class _TObject>
  499. AFX_INLINE void CXTPCalendarPtrCollectionT<_TObject>::Add(_TObject* pNewElement, BOOL bWithAddRef)
  500. {
  501. if (bWithAddRef && pNewElement) {
  502. pNewElement->InternalAddRef();
  503. }
  504. TObjectPtr ptrNewElement(pNewElement);
  505. m_arElements.Add(ptrNewElement);
  506. }
  507. template<class _TObject>
  508. AFX_INLINE void CXTPCalendarPtrCollectionT<_TObject>::Add(_TObject* pNewElement)
  509. {
  510. TObjectPtr ptrNewElement(pNewElement);
  511. m_arElements.Add(ptrNewElement);
  512. }
  513. template<class _TObject>
  514. AFX_INLINE void CXTPCalendarPtrCollectionT<_TObject>::Append(CXTPCalendarPtrCollectionT<_TObject>* pElementsArray)
  515. {
  516. if (!pElementsArray) {
  517. ASSERT(FALSE);
  518. return;
  519. }
  520. int nCount = pElementsArray->GetCount();
  521. for (int i = 0; i < nCount; i++)
  522. {
  523. TObjectPtr ptrElement = pElementsArray->GetAt(i, TRUE);
  524. m_arElements.Add(ptrElement);
  525. }
  526. }
  527. template<class _TObject>
  528. AFX_INLINE int CXTPCalendarPtrCollectionT<_TObject>::Find(const _TObject* pElement)
  529. {
  530. int nCount = GetCount();
  531. for (int i = 0; i < nCount; i++)
  532. {
  533. if (GetAt(i) == pElement)
  534. return i;
  535. }
  536. return -1;
  537. }
  538. template<class _TObject>
  539. AFX_INLINE void CXTPCalendarPtrCollectionT<_TObject>::Remove(_TObject* pElement)
  540. {
  541. int nCount = GetCount();
  542. for (int i = 0; i < nCount; i++)
  543. {
  544. if (GetAt(i) == pElement)
  545. {
  546. RemoveAt(i);
  547. break;
  548. }
  549. }
  550. }
  551. template<class _TObject>
  552. AFX_INLINE void CXTPCalendarPtrCollectionT<_TObject>::SetSize(int nNewSize)
  553. {
  554. m_arElements.SetSize(nNewSize);
  555. }
  556. template<class _TObject>
  557. AFX_INLINE void CXTPCalendarPtrCollectionT<_TObject>::SetAt(int nIndex, _TObject* pElement)
  558. {
  559. TObjectPtr ptrNewElement(pElement);
  560. m_arElements.SetAt(nIndex, ptrNewElement);
  561. }
  562. template<class _TObject>
  563. AFX_INLINE void CXTPCalendarPtrCollectionT<_TObject>::InsertAt(int nIndex, _TObject* pElement)
  564. {
  565. TObjectPtr ptrNewElement(pElement);
  566. m_arElements.InsertAt(nIndex, ptrNewElement);
  567. }
  568. template<class _TObject>
  569. AFX_INLINE void CXTPCalendarPtrCollectionT<_TObject>::RemoveAt(int nIndex/*, BOOL bWithRelease*/)
  570. {
  571. m_arElements.RemoveAt(nIndex);
  572. }
  573. template<class _TObject>
  574. AFX_INLINE void CXTPCalendarPtrCollectionT<_TObject>::RemoveAll(/*BOOL bWithRelease*/)
  575. {
  576. m_arElements.RemoveAll();
  577. }
  578. //===========================================================================
  579. #endif // !defined(_XTPCALENDARCOLLECTIONT_H__)