design_pattern_for_c  V 1.00
state_manager.c
Go to the documentation of this file.
1 
5 #include <stdarg.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include "state_manager.h"
10 #include "dp_util.h"
11 
12 /*************
13  * public define
14 *************/
15 
25 };
26 
34 };
35 
37 /* @{ */
39 static int state_manager_add_new_state(StateManager this, const state_info_t * state);
42 /* @} */
43 /*************
44  * private API
45 *************/
46 static int state_manager_add_new_state(StateManager this, const state_info_t * state) {
47  /*allocate and copy state info*/
48  StateManagerStateInfo state_info=calloc(1, sizeof(*state_info));
49  if(!state_info) {
50  DEBUG_ERRPRINT("allocate state_info error\n");
51  return STATE_MNG_FAILED;
52  }
53 
54  memcpy(&state_info->state, state, sizeof(state_info_t));
55  /*push to list*/
56  dputil_list_push((DPUtilList)this, (DPUtilListData)state_info);
57  return STATE_MNG_SUCCESS;
58 }
59 
61  StateManagerStateInfo state_info=this->head;
62  while(state_info) {
63  if(state_info->state.state == state) {
64  break;
65  }
66  state_info=state_info->next;
67  }
68 
69  return state_info;
70 }
71 /*************
72  * public interface API implement
73 *************/
74 StateManager state_manager_new(size_t state_info_num, const state_info_t * state) {
75  StateManager instance = calloc(1, sizeof(*instance));
76  if( !instance ) {
77  DEBUG_ERRPRINT("allocate error\n");
78  goto err;
79  }
80 
81  /*set state info into manager*/
82  size_t i=0;
83  for( i = 0; i < state_info_num; i ++) {
84  /*allocate and copy state info*/
85  if(state_manager_add_new_state(instance, &state[i]) != STATE_MNG_SUCCESS) {
86  goto err;
87  }
88  }
89 
90  return instance;
91 err:
92  state_manager_free(instance);
93  return NULL;
94 }
95 
97  if(!this || !state) {
98  return STATE_MNG_FAILED;
99  }
100 
101  int ret = STATE_MNG_SUCCESS;
102  StateManagerStateInfo state_info = state_manager_find_state(this, state->state);
103  if(state_info) {
104  memcpy(&state_info->state, state, sizeof(state_info_t));
105  } else {
106  ret = state_manager_add_new_state(this, state);
107  }
108  return ret;
109 }
110 
111 void state_manager_set_state(StateManager this, int state) {
112  if(!this) {
113  return;
114  }
115  this->current_state = state_manager_find_state(this, state);
116 }
117 
119  if(!this) {
120  return STATE_MNG_FAILED;
121  }
122 
123  int current_state = STATE_MNG_FAILED;
124  if(this->current_state) {
125  current_state = this->current_state->state.state;
126  }
127  return current_state;
128 }
129 int state_manager_call(StateManager this, void *arg) {
130  /*fail safe*/
131  if(!this) {
132  return STATE_MNG_FAILED;
133  }
134 
135  if(!this->current_state) {
136  DEBUG_ERRPRINT("You don't set state\n");
137  return STATE_MNG_FAILED;
138  }
139 
140  int (*state_method)(void *arg) = this->current_state->state.state_method;
141  return state_method(arg);
142 }
143 
145  if(!this) {
146  return;
147  }
148 
149  fprintf(stderr, "-------- Show state table --------\n");
150  if(this->current_state) {
151  fprintf(stderr, "[Current state: %d] [method: %s] \n", this->current_state->state.state, this->current_state->state.name);
152  } else {
153  fprintf(stderr, "[Currently no set state]\n");
154  }
155 
156  StateManagerStateInfo state_info=this->head;
157  while(state_info) {
158  fprintf(stderr,"\t[state: %d] [method: %s]\n", state_info->state.state, state_info->state.name);
159  state_info=state_info->next;
160  }
161  fprintf(stderr,"----------------------------------\n");
162 
163 }
164 
166  if(!this) {
167  return;
168  }
169 
171  while(state_info) {
172  free(state_info);
174  }
175  free(this);
176 }
int state_manager_update_method(StateManager this, const state_info_t *state)
update method related to state
Definition: state_manager.c:96
#define STATE_MNG_FAILED
Definition: state_manager.h:10
This is API for Sate design pattern.
Utility headers
StateManager class member definition, detail is defined in C file.
Definition: state_manager.c:30
state info definition.
Definition: state_manager.c:21
void(* free)(EventInstance this)
Definition: event_thread.c:39
void state_manager_free(StateManager this)
free StateManager class
int state_manager_get_current_state(StateManager this)
get current state
void state_manager_show(StateManager this)
show current state table
void state_manager_set_state(StateManager this, int state)
set state
int state_manager_call(StateManager this, void *arg)
call state method
StateManagerStateInfo tail
Definition: state_manager.c:32
StateManagerStateInfo current_state
Definition: state_manager.c:33
StateManagerStateInfo next
Definition: state_manager.c:22
#define STATE_MNG_SUCCESS
Definition: state_manager.h:9
DPUtilListData dputil_list_pop(DPUtilList this)
list pop
Definition: dp_util.c:80
void dputil_list_push(DPUtilList this, DPUtilListData data)
list push
Definition: dp_util.c:21
StateManagerStateInfo head
Definition: state_manager.c:31
StateManager state_manager_new(size_t state_info_num, const state_info_t *state)
Create StateManager class.
Definition: state_manager.c:74
static int state_manager_add_new_state(StateManager this, const state_info_t *state)
Add new state.
Definition: state_manager.c:46
#define DEBUG_ERRPRINT(...)
Definition: dp_debug.h:69
struct state_manager_state_info_t * StateManagerStateInfo
Definition: state_manager.c:20
char * name
state value related function
Definition: state_manager.h:17
StateManagerStateInfo state_manager_find_state(StateManager this, int state)
Find state.
Definition: state_manager.c:60
StateManagerStateInfo prev
Definition: state_manager.c:23
state method definition, to know detail, add parameter "name".
Definition: state_manager.h:15