Skip to content

Commit

Permalink
Active Object: add fields to struct and constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
polesskiy-dev committed Jul 30, 2023
1 parent 813ca7b commit 1267cdd
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
11 changes: 6 additions & 5 deletions examples/request-fsm/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ const char *const STATES_STRINGS[] = {
"REQUEST_NO_ST", "PENDING_ST", "SUCCESS_ST", "ERROR_ST"
};

DECLARE_ACTIVE_OBJECT(REQUEST_AO, REQUEST_EVENT, REQUEST_STATE, REQUEST_AO_ID, REQUEST_QUEUE_MAX_CAPACITY);
typedef struct {
uint8_t maxRetries;
} REQUEST_AO_FIELDS;

DECLARE_ACTIVE_OBJECT(REQUEST_AO, REQUEST_EVENT, REQUEST_STATE, REQUEST_AO_FIELDS, REQUEST_AO_ID, REQUEST_QUEUE_MAX_CAPACITY);
DECLARE_FSM(REQUEST_AO, REQUEST_EVENT, REQUEST_STATE, REQUEST_SIG_MAX, REQUEST_ST_MAX);

/**
Expand All @@ -61,11 +65,8 @@ REQUEST_STATE_HANDLE_F requestTransitionTable[REQUEST_ST_MAX][REQUEST_SIG_MAX] =
[PENDING_ST]= {[REQUEST_SUCCESS_SIG]=requestSuccess, [REQUEST_ERROR_SIG]=GUARD(canRetry, performRequest, requestError), [TIMEOUT_SIG]=GUARD(canRetry, performRequest, requestError)}
};

// TODO should we pass cb to state transition?
int maxRetries = 4;

int main(void) {
REQUEST_AO_Ctor(&requestActiveObject, REQUEST_NO_ST);
REQUEST_AO_Ctor(&requestActiveObject, REQUEST_NO_ST, (REQUEST_AO_FIELDS){.maxRetries = 4});

printf("Starting Request FSM\n");
printf("Dispatching MAKE REQUEST Event\n");
Expand Down
Binary file modified main
Binary file not shown.
10 changes: 6 additions & 4 deletions src/active-object/active-object.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ void ACTIVE_OBJECT_BASE_Ctor(ACTIVE_OBJECT_BASE *const self, uint8_t id) {
self->id = id;
};

#define DECLARE_ACTIVE_OBJECT(ACTIVE_OBJECT_T, EVENT_T, STATE_T, id, maxQueueCapacity) \
#define DECLARE_ACTIVE_OBJECT(ACTIVE_OBJECT_T, EVENT_T, STATE_T, FIELDS_T, id, maxQueueCapacity) \
\
DECLARE_QUEUE(EVENT_T, maxQueueCapacity); \
\
typedef struct { \
ACTIVE_OBJECT_BASE super; \
QUEUE_##EVENT_T queue; \
STATE_T state; \
STATE_T state; \
FIELDS_T fields; \
} ACTIVE_OBJECT_T; \
\
/**
Expand All @@ -45,10 +46,11 @@ void ACTIVE_OBJECT_BASE_Ctor(ACTIVE_OBJECT_BASE *const self, uint8_t id) {
\
typedef void (*ACTIVE_OBJECT_T##HAS_EMPTY_QUEUE_F)(ACTIVE_OBJECT_T *const self); \
\
void ACTIVE_OBJECT_T##_Ctor(ACTIVE_OBJECT_T *const self, STATE_T initialState) { \
void ACTIVE_OBJECT_T##_Ctor(ACTIVE_OBJECT_T *const self, STATE_T initialState, FIELDS_T fields) { \
ACTIVE_OBJECT_BASE_Ctor(&self->super, id); \
QUEUE_##EVENT_T##_Ctor(&self->queue); \
self->state = initialState; \
self->state = initialState; \
self->fields = fields; \
}; \
\
void ACTIVE_OBJECT_T##_Dispatch(ACTIVE_OBJECT_T *const self, EVENT_T event) { \
Expand Down

0 comments on commit 1267cdd

Please sign in to comment.