/** * @file XTPCalendarPtrCollectionT.h * * @copyright * (c) 1998-2025 Codejock Software, All Rights Reserved. * * This source file is the property of Codejock Software and must not be * redistributed by any means without the explicit written permission of * Codejock Software. * * The use of this source code is governed by the terms and conditions specified * in the Toolkit Pro license agreement. Codejock Software grants you, as a * single software developer, the limited right to use this software on one * computer only. * * Contact Information: * support@codejock.com * http://www.codejock.com * */ #if !defined(_XTPCALENDARCOLLECTIONT_H__) # define _XTPCALENDARCOLLECTIONT_H__ # if _MSC_VER > 1000 # pragma once # endif // _MSC_VER > 1000 # include "Common/Base/Diagnostic/XTPDisableNoisyWarnings.h" # pragma warning(disable : 4097) /** * @brief * This class represents a simple array collection of objects. * * @details * Array indexes always start at position 0. * * As with a C array, the access time for indexed element of this * array is constant and is independent of the array size. * * @see overview * @see CArray overview */ template class CXTPCalendarPtrCollectionT : public CXTPCmdTarget { public: /** * @brief * Default collection constructor. * * @see ~CXTPCalendarPtrCollectionT() */ CXTPCalendarPtrCollectionT(); /** * @brief * Default collection destructor. * * @details * Handles member items deallocation. Decreases reference of all * stored objects. * * @see RemoveAll() */ virtual ~CXTPCalendarPtrCollectionT(); /** * @brief * This member function is used to obtain the number of elements * in the collection. * * @details * Call this method to retrieve the number of elements in the array. * Because indexes are zero-based, the size is 1 greater than * the largest index. * * Example: * See example for CXTPCalendarEvents::Add method. * * @return The number of items in the collection. */ virtual int GetCount() const; /** * @brief * This member function adds a new element to the end of the array. * * @param pNewElement A pointer to a _TObjectThe object. Element to add to the array. * * @details * Use this method to add the specified pointer to the end * of the events collection. Reference to the added object is * increased. * * Example: *
	 * CXTPCalendarPtrCollectionT arList;
	 * CXTPCalendarEventPtr ptrEvent1 = new CXTPCalendarEvent()
	 *
	 * // Add() will call InernalAddRef()
	 * arList.Add(ptrEvent1);
	 *
	 * // Add() will not call InernalAddRef()
	 * arList.Add(new CXTPCalendarEvent(), FALSE);
	 *
	 * // GetAt() will call InernalAddRef()
	 * CXTPCalendarEventPtr ptrEvent0 = arList.GetAt(0);
	 *
	 * // GetAt() will not call InernalAddRef()
	 * CXTPCalendarEvent* pEvent1 = arList.GetAt(1, FALSE);
	 *
	 * _ASSERTE(2 == arList.GetCount());
	 *
	 * //RemoveAll() will call InernalRelease() for all objects
	 * arList.RemoveAll();
	 *
	 * _ASSERTE(0 == arList.GetCount());
	 *
	 * Version 2
	 *
	 * CXTPCalendarPtrCollectionT arList;
	 * CXTPCalendarEventPtr ptrEvent1 = new CXTPCalendarEvent()
	 *
	 * // Add() will call InernalAddRef()
	 * arList.Add(ptrEvent1);
	 *
	 * // Add() will not call InernalAddRef()
	 * arList.Add(new CXTPCalendarEvent(), FALSE);
	 *
	 * // GetAt() will call InernalAddRef()
	 * CXTPCalendarEventPtr ptrEvent0 = arList.GetAt(0);
	 *
	 * // GetAt() will not call InernalAddRef()
	 * CXTPCalendarEvent* pEvent1 = arList.GetAt(1, FALSE);
	 *
	 * _ASSERTE(2 == arList.GetCount());
	 *
	 * //RemoveAll() will call InernalRelease() for all objects
	 * arList.RemoveAll();
	 *
	 * _ASSERTE(0 == arList.GetCount());
	 * 
* * @see CXTPCalendarPtrCollectionT overview * @see GetAt * @see RemoveAll * @see GetCount */ virtual void Add(_TObject* pNewElement); /** * @brief * This member function adds a new element to the end of the array. * * @param pNewElement A pointer to a _TObjectThe object. Element to add to the array. * @param bWithAddRef A BOOL. If this parameter is TRUE then InternalAddRef() * is called for pNewElement, otherwise it * is not called. * * @details * Use this method to add the specified pointer to the end * of the events collection. Reference to the added object is * increased. * * Example: *
	 * CXTPCalendarPtrCollectionT arList;
	 * CXTPCalendarEventPtr ptrEvent1 = new CXTPCalendarEvent()
	 *
	 * // Add() will call InernalAddRef()
	 * arList.Add(ptrEvent1);
	 *
	 * // Add() will not call InernalAddRef()
	 * arList.Add(new CXTPCalendarEvent(), FALSE);
	 *
	 * // GetAt() will call InernalAddRef()
	 * CXTPCalendarEventPtr ptrEvent0 = arList.GetAt(0);
	 *
	 * // GetAt() will not call InernalAddRef()
	 * CXTPCalendarEvent* pEvent1 = arList.GetAt(1, FALSE);
	 *
	 * _ASSERTE(2 == arList.GetCount());
	 *
	 * //RemoveAll() will call InernalRelease() for all objects
	 * arList.RemoveAll();
	 *
	 * _ASSERTE(0 == arList.GetCount());
	 *
	 * Version 2
	 *
	 * CXTPCalendarPtrCollectionT arList;
	 * CXTPCalendarEventPtr ptrEvent1 = new CXTPCalendarEvent()
	 *
	 * // Add() will call InernalAddRef()
	 * arList.Add(ptrEvent1);
	 *
	 * // Add() will not call InernalAddRef()
	 * arList.Add(new CXTPCalendarEvent(), FALSE);
	 *
	 * // GetAt() will call InernalAddRef()
	 * CXTPCalendarEventPtr ptrEvent0 = arList.GetAt(0);
	 *
	 * // GetAt() will not call InernalAddRef()
	 * CXTPCalendarEvent* pEvent1 = arList.GetAt(1, FALSE);
	 *
	 * _ASSERTE(2 == arList.GetCount());
	 *
	 * //RemoveAll() will call InernalRelease() for all objects
	 * arList.RemoveAll();
	 *
	 * _ASSERTE(0 == arList.GetCount());
	 * 
* * @see CXTPCalendarPtrCollectionT overview * @see GetAt * @see RemoveAll * @see GetCount */ virtual void Add(_TObject* pNewElement, BOOL bWithAddRef); /** * @brief * This member function is used to set a new element to the position * specified in the nIndex parameter. * * @param nIndex An integer index that is greater than or equal * to 0 and less than the value returned by * GetCount. * @param pElement A pointer to a _TObject object. The element to add to the array. * * @details * Use this method to set the specified element to the * position specified in nIndex parameter. * Reference to the previous object is decreased. * Reference to the new object is increased. * * Example: *
	 * CXTPCalendarPtrCollectionT arList;
	 * CXTPCalendarEventPtr ptrEvent1 = new CXTPCalendarEvent()
	 *
	 * arList.Add(ptrEvent1);
	 * arList.SetAt(0, new CXTPCalendarEvent());
	 * 
* * @see GetAt * @see Add * @see InsertAt */ virtual void SetAt(int nIndex, _TObject* pElement); /** * @brief * This member function finds an element index in the collection using * its pointer. * * @param pElement A pointer to a _TObject object. The element to find in * the array. * * @return -1 if element is not found, otherwise an integer index that is * greater than or equal to 0 and less than the value returned * by GetCount. The specified element currently at this index. */ virtual int Find(const _TObject* pElement) const; /** * @brief * This member function is used to insert a new element at the * position specified in the nIndex parameter. * * @param nIndex An integer index that is greater than or equal * to 0 and less than the value returned by * GetCount(). * @param pElement A pointer to a _TObject object. The element to add to the array. * * @details * Use this method to insert the specified element t the * position specified in the nIndex parameter. * Reference to the new object is increased. * * Example: *
	 * CXTPCalendarPtrCollectionT arList;
	 * CXTPCalendarEventPtr ptrEvent1 = new CXTPCalendarEvent()
	 *
	 * arList.Add(ptrEvent1);
	 * arList.InsertAt(0, new CXTPCalendarEvent());
	 * 
* * @see GetAt * @see Add * @see InsertAt */ virtual void InsertAt(int nIndex, _TObject* pElement); /** * @brief * This member function is used to add a new elements to the end * of the array. * * @param pElementsArray A pointer to an array of _TObject objects. * A pointer to the collection to add to the array. * * @details * Use this method to add the elements from the specified * array to the end of the collection. Reference to the * new object is increased. * * Example: *
	 * CXTPCalendarPtrCollectionT arList1;
	 * CXTPCalendarPtrCollectionT arList2;
	 * ...
	 * arList1.Append(&arList2);
	 * 
* * @see GetAt * @see Add * @see InsertAt * @see SetAt */ virtual void Append(const CXTPCalendarPtrCollectionT<_TObject>* pElementsArray); /** * @brief * This member function is used to change the array size. * * @param nNewSize An int that contains the new array size. * * @details * Argument nNewSize contains the new array size (number of elements). * Must be greater than or equal to 0. */ virtual void SetSize(int nNewSize); /** * @brief * This member function returns an element at the specified * numeric index. Reference to the returned object is increased. * * @param nIndex An integer index that is greater than or equal to 0 * and less than the value returned by GetCount. * * @details * Returns the array element at the specified index. * * Example: * See the example for the Add() method. * * @return A pointer to a _TObject object. The element currently at this index. */ virtual _TObject* GetAt(int nIndex) const; /** * @brief * This member function returns an element at the specified * numeric index. Reference to the returned object is increased. * * @param nIndex An integer index that is greater than or equal to 0 * and less than the value returned by GetCount. * @param bWithAddRef A BOOL. If this parameter is TRUE then InternalAddRef() * is called for the returned object, * otherwise it is not called. * * @details * Returns the array element at the specified index. * * Example: * See the example for the Add() method. * * @return A pointer to a _TObject object. The element currently at this index. */ virtual _TObject* GetAt(int nIndex, BOOL bWithAddRef) const; /** * @brief * This member function removes an element at the specified index * from the array. * * @param nIndex An integer index that is greater than or equal to 0 * and less than the value returned by GetCount(). * * @details * Removes the pointer from this array and releases the instance * of the stored object. */ virtual void RemoveAt(int nIndex); /** * @brief * This member function finds an element using its pointer and removes * it from the array. * * @param pElement A pointer to a _TObject object. The element to find in * the array. * * @details * Removes the pointer from this array and releases the instance * of the stored object. * * @see RemoveAt() * @see RemoveAll() */ virtual void Remove(_TObject* pElement); /** * @brief * This member function is used to remove all of the elements from * the array. * * @details * Removes all of the pointers from this array and releases instances * of all stored objects. * * Example: * See example for CXTPCalendarEvents::Add method. * * @see CXTPCalendarEvents overview */ virtual void RemoveAll(); protected: typedef CXTPSmartPtrInternalT<_TObject> TObjectPtr; /**< Smart pointer type for stored objects.*/ CArray m_arElements; /**< Objects storage.*/ protected: # ifdef _XTP_ACTIVEX virtual long OleGetItemCount(); virtual LPDISPATCH OleGetItem(long nIndex); virtual void OleRemove(long nIndex); // virtual void OleRemoveAll(); # endif }; //////////////////////////////////////////////////////////////////////////// /** * @brief * This class represents a simple array collection of objects pointers. * * @details * Parameters * BASE_CLASS * Base class of the typed pointer array class; * must be an array class (CObArray or CPtrArray). * PTR_TYPE * Type of the elements stored in the base-class array. * * Array indexes always start at position 0. * * As with a C array, the access time for indexed element of this * array is constant and is independent of the array size. * * Overridden method RemoveAll() calls the delete operator for all non-NULL * elements before calling the RemoveAll() method from the base class. * * The RemoveAll() method is called from the array destructor too. * * @see CTypedPtrArray * @see CPtrArray * @see CObArray * @see CArray */ template class CXTPCalendarTypedPtrAutoDeleteArray : public CTypedPtrArray { public: /** * @brief * Base class type definition. */ typedef CTypedPtrArray TBase; /** * @brief * Default object constructor. */ CXTPCalendarTypedPtrAutoDeleteArray(){}; /** * @brief * Default class destructor. * * @details * Handles member items deallocation. */ virtual ~CXTPCalendarTypedPtrAutoDeleteArray() { RemoveAll(); } /** * @brief * Removes all the elements from the array. * * @details * Removes all the pointers from the array and deletes * all stored objects. */ virtual void RemoveAll() { for (int i = 0; i < TBase::GetSize(); i++) { PTR_TYPE pObj = TBase::GetAt(i); if (pObj) { delete pObj; TBase::SetAt(i, NULL); } } TBase::RemoveAll(); } }; /** * @brief * This class represents a simple map of typed keys to typed objects pointers. * Overridden methods SetAt, operator[], RemoveKey, RemoveAll, call delete * for previously stored elements. * * @details * Parameters * KEY * class to key values. * TPtr * A pointer type of stored objects. * * Overridden method RemoveAll() calls the delete operator for all non-NULL * spored elements before calling the RemoveAll() method from the base class. * * The RemoveAll() method is called from the map destructor too. * * @see CMap */ template class CXTPCalendarTypedPtrAutoDeleteMap : public CMap { typedef CMap TBase; public: /** * @brief * Default class destructor. * * @details * Handles member items deallocation. */ virtual ~CXTPCalendarTypedPtrAutoDeleteMap() { RemoveAll(); } /** * @brief * Removes all the elements from the map. * * @param bDeletePtr If TRUE, delete operator will be called for non-null * elements. Default value is TRUE. * * @details * Removes all data from the map and deletes all stored objects if * this is specified by the bDeletePtr parameter. */ virtual void RemoveAll(BOOL bDeletePtr = TRUE) { if (bDeletePtr) { POSITION pos = TBase::GetStartPosition(); while (pos) { KEY tmpKey; TPtr pValue = NULL; TBase::GetNextAssoc(pos, tmpKey, pValue); if (pValue) delete pValue; } } TBase::RemoveAll(); } /** * @brief * Add a new (key, value) pair. * * @param key A key. * @param pValue A value. * @param bDeletePrevPtr If TRUE, delete operator will be called for previous * not null element. */ virtual void SetAt(KEY key, TPtr pValue, BOOL bDeletePrevPtr = TRUE) { if (bDeletePrevPtr) _DeletePtr(key); TBase::SetAt(key, pValue); } /** * @brief * Lookup and add a new element if not there. * * @param key A key. * * @return A reference for value associated to a key (new value added if need). */ TPtr& operator[](KEY key) { _DeletePtr(key); return TBase::operator[](key); } /** * @brief * Removes element by its key. * * @param key A key. * @param bDeletePtr If TRUE, delete operator will be called for not null * element. Default value is TRUE. * @return TRUE if operation is successful, FALSE otherwise. */ virtual BOOL RemoveKey(KEY key, BOOL bDeletePtr = TRUE) { if (bDeletePtr) _DeletePtr(key); return TBase::RemoveKey(key); } private: void _DeletePtr(KEY key) { TPtr pValPrev = NULL; if (TBase::Lookup(key, pValPrev) && pValPrev) { delete pValPrev; } } }; ///////////////////////////////////////////////////////////////////////////// template AFX_INLINE CXTPCalendarPtrCollectionT<_TObject>::CXTPCalendarPtrCollectionT() { } template AFX_INLINE CXTPCalendarPtrCollectionT<_TObject>::~CXTPCalendarPtrCollectionT() { } template AFX_INLINE int CXTPCalendarPtrCollectionT<_TObject>::GetCount() const { return (int)m_arElements.GetSize(); } template AFX_INLINE _TObject* CXTPCalendarPtrCollectionT<_TObject>::GetAt(int nIndex) const { _TObject* pElement = m_arElements[nIndex]; return pElement; } template AFX_INLINE _TObject* CXTPCalendarPtrCollectionT<_TObject>::GetAt(int nIndex, BOOL bWithAddRef) const { _TObject* pElement = m_arElements[nIndex]; if (bWithAddRef && pElement) { ((CCmdTarget*)pElement)->InternalAddRef(); } return pElement; } template AFX_INLINE void CXTPCalendarPtrCollectionT<_TObject>::Add(_TObject* pNewElement, BOOL bWithAddRef) { if (bWithAddRef && pNewElement) { ((CCmdTarget*)pNewElement)->InternalAddRef(); } TObjectPtr ptrNewElement(pNewElement); m_arElements.Add(ptrNewElement); } template AFX_INLINE void CXTPCalendarPtrCollectionT<_TObject>::Add(_TObject* pNewElement) { TObjectPtr ptrNewElement(pNewElement); m_arElements.Add(ptrNewElement); } template AFX_INLINE void CXTPCalendarPtrCollectionT<_TObject>::Append( const CXTPCalendarPtrCollectionT<_TObject>* pElementsArray) { if (!pElementsArray) { _ASSERTE(FALSE); return; } int nCount = pElementsArray->GetCount(); for (int i = 0; i < nCount; i++) { TObjectPtr ptrElement = pElementsArray->GetAt(i, TRUE); m_arElements.Add(ptrElement); } } template AFX_INLINE int CXTPCalendarPtrCollectionT<_TObject>::Find(const _TObject* pElement) const { int nCount = GetCount(); for (int i = 0; i < nCount; i++) { if (GetAt(i) == pElement) return i; } return -1; } template AFX_INLINE void CXTPCalendarPtrCollectionT<_TObject>::Remove(_TObject* pElement) { int nCount = GetCount(); for (int i = 0; i < nCount; i++) { if (GetAt(i) == pElement) { RemoveAt(i); break; } } } template AFX_INLINE void CXTPCalendarPtrCollectionT<_TObject>::SetSize(int nNewSize) { m_arElements.SetSize(nNewSize); } template AFX_INLINE void CXTPCalendarPtrCollectionT<_TObject>::SetAt(int nIndex, _TObject* pElement) { TObjectPtr ptrNewElement(pElement); m_arElements.SetAt(nIndex, ptrNewElement); } template AFX_INLINE void CXTPCalendarPtrCollectionT<_TObject>::InsertAt(int nIndex, _TObject* pElement) { TObjectPtr ptrNewElement(pElement); m_arElements.InsertAt(nIndex, ptrNewElement); } template AFX_INLINE void CXTPCalendarPtrCollectionT<_TObject>::RemoveAt(int nIndex /*, BOOL bWithRelease*/) { m_arElements.RemoveAt(nIndex); } template AFX_INLINE void CXTPCalendarPtrCollectionT<_TObject>::RemoveAll(/*BOOL bWithRelease*/) { m_arElements.RemoveAll(); } //=========================================================================== # ifdef _XTP_ACTIVEX template AFX_INLINE long CXTPCalendarPtrCollectionT<_TObject>::OleGetItemCount() { return (long)GetCount(); } template AFX_INLINE LPDISPATCH CXTPCalendarPtrCollectionT<_TObject>::OleGetItem(long nIndex) { if (nIndex < 0 || nIndex >= (long)OleGetItemCount()) AfxThrowOleException(DISP_E_BADINDEX); CCmdTarget* pObj = (CXTPCmdTarget*)GetAt(nIndex); return pObj ? pObj->GetIDispatch(TRUE) : NULL; } template AFX_INLINE void CXTPCalendarPtrCollectionT<_TObject>::OleRemove(long nIndex) { if (nIndex < 0 || nIndex >= (long)OleGetItemCount()) AfxThrowOleException(DISP_E_BADINDEX); RemoveAt(nIndex); } # endif // _XTP_ACTIVEX # include "Common/Base/Diagnostic/XTPEnableNoisyWarnings.h" #endif // !defined(_XTPCALENDARCOLLECTIONT_H__)