加入状态机
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
#ifndef INCLUDED_GAI_ACTION_H
|
||||
#define INCLUDED_GAI_ACTION_H
|
||||
|
||||
#ifndef INCLUDED_OS_TYPES_H
|
||||
#include <os_types.h>
|
||||
#endif /*INCLUDED_OS_TYPES_H*/
|
||||
|
||||
#ifndef INCLUDED_OS_COMPILER_H
|
||||
#include <os_compiler.h>
|
||||
#endif /*INCLUDED_OS_COMPILER_H*/
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------------------------ */
|
||||
/* */
|
||||
|
||||
typedef struct gai_action_t gai_action_t;
|
||||
typedef void (*gai_action_function_t)(gai_action_t* self);
|
||||
typedef void (*gai_action_update_function_t)(gai_action_t* self, void* params);
|
||||
|
||||
typedef enum {
|
||||
kGaiActionStatus_UnInitialized=0,
|
||||
kGaiActionStatus_Running,
|
||||
kGaiActionStatus_Terminated,
|
||||
}gai_action_status_t;
|
||||
|
||||
struct gai_action_t{
|
||||
gai_action_status_t status;
|
||||
gai_action_function_t onInitialize;
|
||||
gai_action_update_function_t onUpdate;
|
||||
gai_action_function_t onCleanup;
|
||||
void* userdata;
|
||||
};
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------------------------ */
|
||||
/* */
|
||||
|
||||
OS_STATIC_FORCE_INLINE
|
||||
void gai_action_init(gai_action_t* self,
|
||||
gai_action_function_t onInitialize,
|
||||
gai_action_update_function_t onUpdate,
|
||||
gai_action_function_t onCleanup, void* userdata){
|
||||
self->status = kGaiActionStatus_UnInitialized;
|
||||
self->onInitialize = onInitialize;
|
||||
self->onUpdate = onUpdate;
|
||||
self->onCleanup = onCleanup;
|
||||
self->userdata = userdata;
|
||||
}
|
||||
|
||||
OS_STATIC_FORCE_INLINE
|
||||
void gai_action_Initialize(gai_action_t* self){
|
||||
if(self->onInitialize){
|
||||
self->onInitialize(self);
|
||||
}
|
||||
self->status = kGaiActionStatus_Running;
|
||||
}
|
||||
|
||||
OS_STATIC_FORCE_INLINE
|
||||
gai_action_status_t gai_action_Update(gai_action_t* self, void* userdata){
|
||||
if(self->status==kGaiActionStatus_Terminated){
|
||||
return kGaiActionStatus_Terminated;
|
||||
}
|
||||
if(self->onUpdate){
|
||||
self->onUpdate(self, userdata);
|
||||
}else{
|
||||
self->status = kGaiActionStatus_Terminated;
|
||||
}
|
||||
return self->status;
|
||||
}
|
||||
|
||||
OS_STATIC_FORCE_INLINE
|
||||
void gai_action_Cleanup(gai_action_t* self){
|
||||
if(self->status == kGaiActionStatus_Terminated){
|
||||
if(self->onCleanup){
|
||||
self->onCleanup(self);
|
||||
}
|
||||
}
|
||||
self->status = kGaiActionStatus_UnInitialized;
|
||||
}
|
||||
|
||||
|
||||
#endif /*INCLUDED_GAI_ACTION_H*/
|
||||
Reference in New Issue
Block a user