Clone of UAS2 @ https://github.com/drudgedance/uas2

Job.cpp 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * This file is part of UAS2.
  3. *
  4. * UAS2 is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * UAS2 is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. * You should have received a copy of the GNU General Public License
  14. * along with UASv1; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. */
  17. /**
  18. * @file Job.cpp
  19. * Implements functionality for a job pool.
  20. *
  21. * Used for objects that require scheduling.
  22. */
  23. #include "Job.h"
  24. clock_t cJobPool::longtime;
  25. clock_t cJobPool::NextTickTime;
  26. int cJob::Tick( )
  27. {
  28. if ( m_fEnabled )
  29. {
  30. if ( ++m_iCounter == m_iFrequency )
  31. {
  32. m_iCounter = 0;
  33. int ret = m_mcFunction( m_wParam, m_lParam );
  34. //UpdateConsole( " Yep.\r\n");
  35. if ( --m_iNumTimes == 0 )
  36. return JOB_REMOVE;
  37. else
  38. return ret;
  39. }
  40. }
  41. return -1;
  42. }
  43. void cJobPool::Tick( )
  44. {
  45. std::list< cJob * >::iterator itJob = m_lstJobList.begin( );
  46. for ( ; itJob != m_lstJobList.end( ); ++itJob)
  47. {
  48. //char szMessage[100];
  49. //sprintf (szMessage, "Job: %u.\r\n", (*itJob)->GetJobID( ) );
  50. //UpdateConsole( (char *)szMessage);
  51. switch ( (*itJob)->Tick( ) )
  52. {
  53. case JOB_DISABLE:
  54. (*itJob)->Enable( FALSE );
  55. break; // case JOB_DISABLE
  56. case JOB_NORMAL:
  57. break; // case JOB_NORMAL
  58. case JOB_REMOVE:
  59. //RemoveJob( (*itJob)->GetJobID( ) );
  60. SAFEDELETE( *itJob )
  61. itJob = m_lstJobList.erase( itJob );
  62. break; // case JOB_REMOVE
  63. }
  64. }
  65. }
  66. int cJobPool::CreateJob( MessageCall mc, LPVOID wParam, LPVOID lParam, char *szName, int iFreq, int iNumTimes )
  67. {
  68. cJob *pcNewJob = new cJob( mc, wParam, lParam, iFreq, iNumTimes, szName, m_iJobID++ );
  69. m_lstJobList.push_back( pcNewJob );
  70. return m_iJobID - 1;
  71. }
  72. int cJobPool::GetJobIDByName( char *szName )
  73. {
  74. std::list< cJob * >::iterator itJob = m_lstJobList.begin( );
  75. for ( ; itJob != m_lstJobList.end( ); ++itJob )
  76. {
  77. if ( lstrcmpi( (*itJob)->GetName( ), szName ) == 0 )
  78. return (*itJob)->GetJobID( );
  79. }
  80. return -1;
  81. }
  82. cJob *cJobPool::GetJobByID( int iID )
  83. {
  84. std::list< cJob * >::iterator itJob = m_lstJobList.begin( );
  85. for ( ; itJob != m_lstJobList.end( ); ++itJob )
  86. {
  87. if ( (*itJob)->GetJobID( ) == iID )
  88. return (*itJob);
  89. }
  90. return NULL;
  91. }
  92. void cJobPool::RemoveJob( int iID )
  93. {
  94. std::list< cJob * >::iterator itJob = m_lstJobList.begin( );
  95. for ( ; itJob != m_lstJobList.end( ); ++itJob )
  96. {
  97. if ( (*itJob)->GetJobID( ) == iID )
  98. {
  99. SAFEDELETE( *itJob )
  100. m_lstJobList.erase( itJob );
  101. break;
  102. }
  103. }
  104. }