Răsfoiți Sursa

Merge branch 'std_function' of https://github.com/BlackEdder/TaskScheduler into testing

Anatoli Arkhipenko 8 ani în urmă
părinte
comite
d0084a9591
1 a modificat fișierele cu 31 adăugiri și 19 ștergeri
  1. 31 19
      src/TaskScheduler.h

+ 31 - 19
src/TaskScheduler.h

@@ -129,6 +129,7 @@
  *  #define _TASK_LTS_POINTER       // Compile with support for local task storage pointer
  *  #define _TASK_PRIORITY			// Support for layered scheduling priority
  *  #define _TASK_MICRO_RES			// Support for microsecond resolution
+ *  #define _TASK_STD_FUNCTION      // Support for std::function
  */
 
 
@@ -202,6 +203,17 @@ class StatusRequest {
 };
 #endif  // _TASK_STATUS_REQUEST
 
+#ifdef _TASK_STD_FUNCTION
+#define _TASK_STD_FUNCTION
+#include <functional>
+typedef std::function<void()> callback_t;
+typedef std::function<void()> onDisable_cb_t;
+typedef std::function<bool()> onEnable_cb_t;
+#else
+typedef void (*callback_t)();
+typedef void (*onDisable_cb_t)();
+typedef bool (*onEnable_cb_t)();
+#endif
 
 typedef struct  {
 	bool enabled : 1;							// indicates that task is enabled or not.
@@ -221,9 +233,9 @@ class Scheduler;
 class Task {
     friend class Scheduler;
     public:
-		inline Task(unsigned long aInterval=0, long aIterations=0, void (*aCallback)()=NULL, Scheduler* aScheduler=NULL, bool aEnable=false, bool (*aOnEnable)()=NULL, void (*aOnDisable)()=NULL);
+		inline Task(unsigned long aInterval=0, long aIterations=0, callback_t aCallback=NULL, Scheduler* aScheduler=NULL, bool aEnable=false, onEnable_cb_t aOnEnable=NULL, onDisable_cb_t aOnDisable=NULL);
 #ifdef _TASK_STATUS_REQUEST
-		inline Task(void (*aCallback)()=NULL, Scheduler* aScheduler=NULL, bool (*aOnEnable)()=NULL, void (*aOnDisable)()=NULL);
+		inline Task(callback_t aCallback=NULL, Scheduler* aScheduler=NULL, onEnable_cb_t aOnEnable=NULL, onDisable_cb_t aOnDisable=NULL);
 #endif  // _TASK_STATUS_REQUEST
         inline ~Task();
 
@@ -236,17 +248,17 @@ class Task {
 		inline void restartDelayed(unsigned long aDelay=0);
 		inline bool disable();
 		inline bool isEnabled() { return iStatus.enabled; }
-		inline void set(unsigned long aInterval, long aIterations, void (*aCallback)(),bool (*aOnEnable)()=NULL, void (*aOnDisable)()=NULL);
+		inline void set(unsigned long aInterval, long aIterations, callback_t aCallback,onEnable_cb_t aOnEnable=NULL, onDisable_cb_t aOnDisable=NULL);
 		inline void setInterval(unsigned long aInterval);
 		inline unsigned long getInterval() { return iInterval; }
 		inline void setIterations(long aIterations);
 		inline long getIterations() { return iIterations; }
 		inline unsigned long getRunCounter() { return iRunCounter; }
-		inline void setCallback(void (*aCallback)()) { iCallback = aCallback; }
-		inline void setOnEnable(bool (*aCallback)()) { iOnEnable = aCallback; }
-		inline void setOnDisable(void (*aCallback)()) { iOnDisable = aCallback; }
-		inline void yield(void (*aCallback)());
-		inline void yieldOnce(void (*aCallback)());
+		inline void setCallback(callback_t aCallback) { iCallback = aCallback; }
+		inline void setOnEnable(onEnable_cb_t aCallback) { iOnEnable = aCallback; }
+		inline void setOnDisable(callback_t aCallback) { iOnDisable = aCallback; }
+		inline void yield(callback_t aCallback);
+		inline void yieldOnce(callback_t aCallback);
 #ifdef _TASK_TIMECRITICAL
 		inline long getOverrun() { return iOverrun; }
 		inline long getStartDelay() { return iStartDelay; }
@@ -284,9 +296,9 @@ class Task {
 		volatile long			iIterations;		// number of iterations left. 0 - last iteration. -1 - infinite iterations
 		long					iSetIterations; 		// number of iterations originally requested (for restarts)
 		unsigned long			iRunCounter;		// current number of iteration (starting with 1). Resets on enable. 
-		void					(*iCallback)();		// pointer to the void callback method
-		bool					(*iOnEnable)();		// pointer to the bolol OnEnable callback method
-		void					(*iOnDisable)();	// pointer to the void OnDisable method
+		callback_t				iCallback;		// pointer to the void callback method
+		onEnable_cb_t		    iOnEnable;		// pointer to the bolol OnEnable callback method
+		onDisable_cb_t			iOnDisable;	// pointer to the void OnDisable method
 		Task					*iPrev, *iNext;		// pointers to the previous and next tasks in the chain
 		Scheduler				*iScheduler;		// pointer to the current scheduler
 #ifdef _TASK_STATUS_REQUEST
@@ -350,7 +362,7 @@ class Scheduler {
 /** Constructor, uses default values for the parameters
  * so could be called with no parameters.
  */
-Task::Task( unsigned long aInterval, long aIterations, void (*aCallback)(), Scheduler* aScheduler, bool aEnable, bool (*aOnEnable)(), void (*aOnDisable)() ) {
+Task::Task( unsigned long aInterval, long aIterations, callback_t aCallback, Scheduler* aScheduler, bool aEnable, onEnable_cb_t aOnEnable, onDisable_cb_t aOnDisable ) {
 	reset();
 	set(aInterval, aIterations, aCallback, aOnEnable, aOnDisable);
 	if (aScheduler) aScheduler->addTask(*this);
@@ -379,7 +391,7 @@ Task::~Task() {
 /** Constructor with reduced parameter list for tasks created for 
  *  StatusRequest only triggering (always immediate and only 1 iteration)
  */
-Task::Task( void (*aCallback)(), Scheduler* aScheduler, bool (*aOnEnable)(), void (*aOnDisable)() ) {
+Task::Task( callback_t aCallback, Scheduler* aScheduler, onEnable_cb_t aOnEnable, onDisable_cb_t aOnDisable ) {
 	reset();
 	set(TASK_IMMEDIATE, TASK_ONCE, aCallback, aOnEnable, aOnDisable);
 	if (aScheduler) aScheduler->addTask(*this);
@@ -468,7 +480,7 @@ void Task::reset() {
  * @param aOnEnable - pointer to the callback method which is called on enable()
  * @param aOnDisable - pointer to the callback method which is called on disable()
  */
-void Task::set(unsigned long aInterval, long aIterations, void (*aCallback)(),bool (*aOnEnable)(), void (*aOnDisable)()) {
+void Task::set(unsigned long aInterval, long aIterations, callback_t aCallback, onEnable_cb_t aOnEnable, onDisable_cb_t aOnDisable) {
 	setInterval(aInterval); 
 	iSetIterations = iIterations = aIterations;
 	iCallback = aCallback;
@@ -487,7 +499,7 @@ void Task::setIterations(long aIterations) {
 /** Prepare task for next step iteration following yielding of control to the scheduler
  * @param aCallback - pointer to the callback method for the next step
  */
-void Task::yield (void (*aCallback)()) {
+void Task::yield (callback_t aCallback) {
 	iCallback = aCallback;
 	forceNextIteration();
 	
@@ -501,7 +513,7 @@ void Task::yield (void (*aCallback)()) {
 /** Prepare task for next step iteration following yielding of control to the scheduler
  * @param aCallback - pointer to the callback method for the next step
  */
-void Task::yieldOnce (void (*aCallback)()) {
+void Task::yieldOnce (callback_t aCallback) {
 	yield(aCallback);
 	iIterations = 1;
 }
@@ -517,7 +529,7 @@ void Task::enable() {
 			Task *current = iScheduler->iCurrent;
 			iScheduler->iCurrent = this;
 			iStatus.inonenable = true;		// Protection against potential infinite loop
-			iStatus.enabled = (*iOnEnable)();
+			iStatus.enabled = iOnEnable();
 			iStatus.inonenable = false;	  	// Protection against potential infinite loop
 			iScheduler->iCurrent = current;
 		}
@@ -589,7 +601,7 @@ bool Task::disable() {
 	if (previousEnabled && iOnDisable) {
 		Task *current = iScheduler->iCurrent;
 		iScheduler->iCurrent = this;
-		(*iOnDisable)();
+		iOnDisable();
 		iScheduler->iCurrent = current;
 	}
 #ifdef _TASK_STATUS_REQUEST
@@ -858,7 +870,7 @@ bool Scheduler::execute() {
 
 				iCurrent->iDelay = i;
 				if ( iCurrent->iCallback ) {
-					( *(iCurrent->iCallback) )();
+					iCurrent->iCallback();
 					idleRun = false;
 				}
 			}