I have this method that performs certain functions based on the state that the algorithm is currently at:
static infoStruct_t info;
void first_part_of_algorithm(void){
info.state = FIRST_STATE;
//set other information here as well
while(info.state < FINAL_STATE){
switch(info.state){
case FIRST_STATE:
perform_first_state_function();
break;
case SECOND_STATE:
perform_second_state_function();
break;
case THIRD_STATE:
perform_check_needed_for_algorithm();
break;
default:
//should never be reached
break;
}
}
}
Now in each of these "perform" functions, the state can change. The first function always causes the state to go to the second state, the second function always makes the state go to the third state, and the third function will either reset the state to the first state, or go to the final state and leave the first part of the algorithm. Also, different parts of the info
struct
are set at various points in any of the functions.
I have not yet written the second part of the algorithm, but it will need to have access to the info
struct
Here are the following state / check functions:
void perform_first_state_function(void){
while(wait_for_condition()!=SUCCESS){
sleep();
}
info.state = SECOND_STATE;
sleep();
}
void perform_second_state_function(void){
int attempts = 0;
while(attempts < ALLOWED_ATTEMPTS){
if(event_is_ready()){
if(info.detection_number==0){
//set various info properties
...
info.first_detection_time = get_time();
info.detection_number = 1;
break;
}
else{
//set other info properties
...
info.second_detection_time = get_time();
info.state = THIRD_STATE;
break;
}
}
attempts++;
sleep();
}
}
My questions are:
- Is this a code smell that the functions are implicitly accessing the
static
info
variable? - Should I explicitly pass in the
info
struct
as a pointer? (for readability) - Does the
info
struct
need to be a singleton? - Any general suggestions?
static
and singleton is needed? – Jamal♦ Jul 10 '14 at 14:43