X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/4271ab433cd55bbd2612292bcf39e4dc3d7274f1..0907aa4037b12b6b88ee24495d4577a064d4f8db:/projects/stargazer/actions.h diff --git a/projects/stargazer/actions.h b/projects/stargazer/actions.h new file mode 100644 index 00000000..eea72b77 --- /dev/null +++ b/projects/stargazer/actions.h @@ -0,0 +1,90 @@ +#ifndef __ACTIONS_H__ +#define __ACTIONS_H__ + +// Usage: +// +// ACTIONS_LIST actionsList; +// CLASS myClass; +// DATA1 myData1; +// DATA2 myData2; +// +// actionsList.Enqueue(myClass, &CLASS::myMethod1, myData1); +// actionsList.Enqueue(myClass, &CLASS::myMethod2, myData2); +// +// actionsList.InvokeAll(); + +#include +#include +#include + +// Generalized actor type - a method of some class with one argument +template +struct ACTOR +{ +typedef void (ACTIVE_CLASS::*TYPE)(DATA_TYPE); +}; + +// Abstract base action class for polymorphic action invocation +class BASE_ACTION +{ +public: + virtual ~BASE_ACTION() {} + virtual void Invoke() = 0; +}; + +// Concrete generalized action type - an actor with it's data and owner +template +class ACTION : public BASE_ACTION, + public std::unary_function +{ +public: + ACTION(ACTIVE_CLASS & ac, + typename ACTOR::TYPE a, + DATA_TYPE d) + : activeClass(ac), actor(a), data(d) {} + void Invoke(); +private: + ACTION(const ACTION & rvalue); + ACTION & operator=(const ACTION & rvalue); + + ACTIVE_CLASS & activeClass; + typename ACTOR::TYPE actor; + DATA_TYPE data; +}; + +// A list of an actions +// All methods are thread-safe +class ACTIONS_LIST : private std::vector +{ +public: + // Just a typedef for parent class + typedef std::vector parent; + + // Initialize mutex + ACTIONS_LIST(); + // Delete actions and destroy mutex + virtual ~ACTIONS_LIST(); + + parent::iterator begin(); + parent::iterator end(); + parent::const_iterator begin() const; + parent::const_iterator end() const; + + bool empty() const; + size_t size() const; + void swap(ACTIONS_LIST & list); + + // Add an action to list + template + void Enqueue(ACTIVE_CLASS & ac, + typename ACTOR::TYPE a, + DATA_TYPE d); + // Invoke all actions in the list + void InvokeAll(); +private: + mutable pthread_mutex_t mutex; +}; + +#include "actions.inl.h" + +#endif