/* Copyright ©2006 Ricardo Pescuma Domenecci Modified ©2008-2010 DeathAxe, Yasnovidyashii, Merlin, K. Romanov, Kreol This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifndef __CONTACTASYNCQUEUE_H__ #define __CONTACTASYNCQUEUE_H__ /** * * **/ struct CQueueItem { DWORD check_time; HCONTACT hContact; PVOID param; }; /** * * **/ class CContactQueue { public: enum EQueueStatus { RUNNING = 0, STOPPING = 1, STOPPED = 2 }; CContactQueue (int initialSize = 10); ~CContactQueue (); inline int Size () const { return _queue.getCount();} inline int Remove (int idx) { mir_free(_queue[idx]); return _queue.remove(idx);} inline CQueueItem* Get (int idx) const { return _queue[idx];} void RemoveAll(); /** * This function removes all queue items for the hContact. * * @param hContact - the contact whose queue items to delete * * @return nothing **/ void RemoveAll(HCONTACT hContact); /** * This function removes all queue items for the hContact considering the correct parameter. * * @param hContact - the contact whose queue items to delete * @param param - a caller defined parameter passed to the callback function * * @return nothing **/ void RemoveAllConsiderParam(HCONTACT hContact, PVOID param); /** * This method adds the desired new item. * * @param waitTime - the time to wait until the callback is desired to run * @param hContact - the contact to perform the action for * @param param - a caller defined parameter passed to the callback function * * @retval TRUE - The item is added to the queue successfully. * @retval FALSE - The item is not added to the queue. **/ BOOL Add(int waitTime, HCONTACT hContact, PVOID param = NULL); /** * This method adds the desired new item only, if the queue does not yet contain * an item for the contact. * * @param waitTime - the time to wait until the callback is desired to run * @param hContact - the contact to perform the action for * @param param - a caller defined parameter passed to the callback function * * @retval TRUE - The item is added to the queue successfully. * @retval FALSE - The item is not added to the queue. **/ BOOL AddIfDontHave(int waitTime, HCONTACT hContact, PVOID param = NULL); /** * This method removes all existing queue items for the contact and adds a new queue item * for the given contact. This method might be used to move an existing entry, * whose check_time has changed. * * @param waitTime - the time to wait until the callback is desired to run * @param hContact - the contact to perform the action for * @param param - a caller defined parameter passed to the callback function * * @return nothing **/ BOOL AddUnique(int waitTime, HCONTACT hContact, PVOID param = NULL); /** * This method removes all existing queue items for the contact with the same parameter as @e param * and adds a new queue item for the given contact. This method might be used to move an existing * entry, whose check_time has changed. * * @param waitTime - the time to wait until the callback is desired to run * @param hContact - the contact to perform the action for * @param param - a caller defined parameter passed to the callback function * * @return nothing **/ BOOL AddUniqueConsiderParam (int waitTime, HCONTACT hContact, PVOID param = NULL); /** * This method resumes the worker thread and immitiatly goes on with the next entry. * * @param none * * @return nothing **/ void ContinueWithNext(); protected: virtual void OnEmpty () {}; virtual void Callback (HCONTACT hContact, PVOID param) = 0; /** * This is the real thread callback function. As long as _status * is set to RUNNING it looks for items in the queue to perform * the _pfnCallback function on them. If the queue is empty or the * next upcoming item is located in the future, the thread is suspended * in the meanwhile. * * @param none * * @return nothing **/ void Thread(); /** * This is a static method to redirect the thread's callback function * to the desired class object. * * @param obj - pointer to the object (instance) of CContactQueue * * @return nothing **/ static void ThreadProc(CContactQueue* obj) { obj->Thread(); } /** * This method suspends the worker thread for the given ammount of time. * * @param time - milliseconds to suspend the thread for * * @return nothing **/ void Suspend(int time) const; private: LIST _queue; CRITICAL_SECTION _cs; HANDLE _hEvent; EQueueStatus _status; void Lock(); void Release(); /** * This member function really adds an item into the time sorted queue list. * * @param waitTime - the time to wait until the callback is desired to run * @param hContact - the contact to perform the action for * @param param - a caller defined parameter passed to the callback function * * @retval TRUE - The item is added to the queue successfully. * @retval FALSE - The item is not added to the queue. **/ BOOL InternalAdd(int waitTime, HCONTACT hContact, PVOID param); }; #endif // __CONTACTASYNCQUEUE_H__