task.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2016 RWS Inc, All Rights Reserved
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of version 2 of the GNU General Public License as published by
  7. // the Free Software Foundation
  8. //
  9. // This program 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. //
  14. // You should have received a copy of the GNU General Public License along
  15. // with this program; if not, write to the Free Software Foundation, Inc.,
  16. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. //
  18. #ifndef TASK_H
  19. #define TASK_H
  20. #include "Blue.h"
  21. // If PATHS_IN_INCLUDES macro is defined, we can utilized relative
  22. // paths to a header file. In this case we generally go off of our
  23. // RSPiX root directory. System.h MUST be included before this macro
  24. // is evaluated. System.h is the header that, based on the current
  25. // platform (or more so in this case on the compiler), defines
  26. // PATHS_IN_INCLUDES. Blue.h includes system.h so you can include that
  27. // instead.
  28. #ifdef PATHS_IN_INCLUDES
  29. #include "ORANGE/CDT/List.h"
  30. #else
  31. #include "list.h"
  32. #endif // PATHS_IN_INCLUDES
  33. // Forward declare RTask for typedef.
  34. class RTask;
  35. // Handy-dandy typedef.
  36. typedef RTask* PTASK;
  37. class RTask
  38. {
  39. /////////////////////// Typedefs & Enums ////////////////////////////////////
  40. public:
  41. // Typedef for user task callback. This is the type of function called.
  42. typedef void (*TaskFunc)( // Returns nothing.
  43. ULONG ulUser); // User defined m_ulUser.
  44. // Typedef to override this RTask's timer. If overridden, this RTask
  45. // will use this callback to get the time.
  46. typedef long (*TimeFunc)( // Returns time as a long in milliseconds.
  47. long lTimeUser); // User defined m_lTimeUser.
  48. /////////////////////// Con/Destruction ////////////////////////////////////
  49. public:
  50. // Default constructor.
  51. RTask();
  52. // Special constructor that passes parms on to Init().
  53. RTask(TaskFunc tf, ULONG ulUser);
  54. // Destructor.
  55. ~RTask();
  56. ////////////////////////// Querries ///////////////////////////////////////
  57. public:
  58. // Returns TRUE if this task is currently in the list of tasks to be
  59. // run; FALSE, otherwise.
  60. short IsActive(void) { return m_sActive; }
  61. // Returns the current time based on either the user base or Blue.
  62. long GetTime(void)
  63. { return (m_fnTime == NULL ? rspGetMilliseconds() : (*m_fnTime)(m_lTimeUser)); }
  64. ////////////////////////// Methods ////////////////////////////////////////
  65. public:
  66. // Initialize task info.
  67. void Init(TaskFunc tf, ULONG ulUser);
  68. // Kill task info.
  69. // Returns 0 on success.
  70. short Kill(void);
  71. // Start this task.
  72. // Returns 0 on success.
  73. short Start(void);
  74. // Suspend this task (can be restarted after this is call).
  75. // Returns 0 on success.
  76. short Suspend(void);
  77. // Use a custom timer.
  78. void SetTimeFunc(TimeFunc fnTime, long lTimeUser)
  79. { m_fnTime = fnTime; m_lTimeUser = lTimeUser; }
  80. /////////////////////// Static functions ///////////////////////////////
  81. // Critical call for all instances (static).
  82. static void Do(void);
  83. ////////////////////////// Internal Methods ///////////////////////////////
  84. protected:
  85. // Initialize instantiable members.
  86. void Reset(void);
  87. ////////////////////////// Member vars ////////////////////////////////////
  88. public:
  89. // The following member variables are safe to tamper with from outside
  90. // this function, and that is why they're public.
  91. TaskFunc m_fnTask; // User specified task function.
  92. ULONG m_ulUser; // User specified parm to task function.
  93. long m_lInterval; // User specified timer interval.
  94. long m_lNextExpiration; // Next time to call task.
  95. protected:
  96. // The following member variables are NOT safe to tamper with from
  97. // outside this function, and that is why they're protected.
  98. short m_sActive; // TRUE if active (in list), FALSE otherwise.
  99. TimeFunc m_fnTime; // Custom time function.
  100. long m_lTimeUser;// Custom time function user value.
  101. /////////////////////// Static members /////////////////////////////////
  102. static RList<RTask> ms_listActive; // List of tasks to be called.
  103. };
  104. #endif // TASK_H
  105. //////////////////////////////////////////////////////////////////////////////
  106. // EOF
  107. //////////////////////////////////////////////////////////////////////////////