design_pattern_for_c  V 1.00
chain_of_responsibility.c
Go to the documentation of this file.
1 
6 #include "flyweight.h"
7 #include "chain_element.h"
8 #include "dp_util.h"
9 
11 /* @{ */
17  int id;
19 };
20 
22 static void chain_of_resp_new(void *this, size_t size, void *input_parameter);
24 static int chain_of_resp_equall_operand(void *this, size_t size, void *input_parameter);
26 static int chain_of_resp_setter(void *this, size_t size, void *input_parameter);
28 static void chain_of_resp_free(void *this);
29 /* @} */
30 
38 } cor_mng_g = {
39  .method ={
41  .equall_operand=chain_of_resp_equall_operand,
42  .setter=chain_of_resp_setter,
43  .destructor=chain_of_resp_free,
44  },
45  .handle = NULL,
46  .is_threadsafe = 0,
47 };
48 
49 /*************
50  * ChainOfResponsibility method definition
51 *************/
53 /*input is name*/
54 static void chain_of_resp_new(void *this, size_t size, void *input_parameter) {
56  int * id = (int *)input_parameter;
57  instance->id = *id;
58  instance->element = chain_element_new(cor_mng_g.is_threadsafe);
59 }
60 
62 static int chain_of_resp_equall_operand(void *this, size_t size, void *input_parameter) {
64  int * id = (int *)input_parameter;
65 
66  //check name is same
67  return instance->id == *id;
68 }
69 
71 static int chain_of_resp_setter(void *this, size_t size, void *input_parameter) {
73  chain_element_req_t * data = input_parameter;
74  return chain_element_add_function(instance->element, data);
75 }
76 
78 static void chain_of_resp_free(void *this) {
80  chain_element_delete(instance->element);
81 }
82 
83 /*************
84  * public interface API implement
85 *************/
86 void cor_set_threadsafe(int is_threadsafe) {
87  cor_mng_g.is_threadsafe = is_threadsafe;
88 }
89 
90 ChainElementPart cor_add_function(const int id, chain_func func, void *ctx) {
91  if(!func) {
92  return NULL;
93  }
94 
95  if(!cor_mng_g.handle) {
96  /* get flyweight handle with thread safe */
98  if(!cor_mng_g.handle) {
99  return NULL;
100  }
101  }
102 
103  chain_element_req_t data={{func, ctx}, NULL};
104  int ret = flyweight_set(cor_mng_g.handle, (void *)&id, &data, NULL);
105  if( ret == COR_FAILED ) {
106  return NULL;
107  } else {
108  return data.result_element_part;
109  }
110 }
111 
112 void cor_call(const int id, void *arg) {
113  if(!cor_mng_g.handle) {
114  return;
115  }
116  ChainOfResponsibility cor_instance = flyweight_get(cor_mng_g.handle, (void *)&id);
117  if(cor_instance) {
118  chain_element_call(cor_instance->element, arg);
119  }
120 }
121 
122 void cor_remove_function(const int id, chain_func func) {
123  if(!cor_mng_g.handle || !func) {
124  return;
125  }
126 
127  ChainOfResponsibility cor_instance = flyweight_get(cor_mng_g.handle, (void *)&id);
128  if(cor_instance) {
129  chain_element_remove_function(cor_instance->element, func);
130  }
131 }
132 
133 void cor_remove_chain_element_part(const int id, ChainElementPart element) {
134  if(!cor_mng_g.handle || !element) {
135  return;
136  }
137 
138  ChainOfResponsibility cor_instance = flyweight_get(cor_mng_g.handle, (void *)&id);
139  if(cor_instance) {
140  chain_element_remove_element_part(cor_instance->element, element);
141  }
142 }
143 
144 void cor_clear(void) {
146  cor_mng_g.handle=NULL;
147 }
ChainElementPart class instance definition, which is a part of chain.
static void chain_of_resp_new(void *this, size_t size, void *input_parameter)
new API
#define COR_FAILED
Definition: chain_element.h:11
Utility headers
void flyweight_factory_free(FlyweightFactory this)
clear class handle
Definition: flyweight.c:293
This is API as Flyweight design petten.
struct chain_of_resp_t * ChainOfResponsibility
void * flyweight_get(FlyweightFactory this, void *constructor_parameter)
getter
Definition: flyweight.c:244
static int chain_of_resp_equall_operand(void *this, size_t size, void *input_parameter)
equall operand, check name
ChainElementPart result_element_part
Definition: chain_element.h:28
void chain_element_remove_function(ChainElement this, chain_func func)
Definition: chain_element.c:86
void cor_set_threadsafe(int is_threadsafe)
set thredsafe
void chain_element_delete(ChainElement this)
ChainElement chain_element_new(int is_threadsafe)
Definition: chain_element.c:57
int flyweight_set(FlyweightFactory this, void *constructor_parameter, void *data, int(*setter)(void *this, size_t size, void *input_parameter))
setter
Definition: flyweight.c:264
FlyweightFactory member definition, defined in flyweight.c.
Definition: flyweight.c:41
This is API for Chain of Responsibility design pettern class.
int chain_element_add_function(ChainElement this, chain_element_req_t *elemnt_data)
Definition: chain_element.c:73
ChainElementPart cor_add_function(const int id, chain_func func, void *ctx)
add to chain api
void cor_remove_chain_element_part(const int id, ChainElementPart element)
remove to chain api
void chain_element_call(ChainElement this, void *arg)
cor_result_e(* chain_func)(void *arg, void *ctx)
chain func
void(* constructor)(void *this, size_t size, void *input_parameter)
constructor of class
Definition: flyweight.h:22
ChainOfResponsibility class instance definition.
FlyweightFactory flyweight_factory_new(size_t class_size, int is_threadsafe, FlyweightMethodsIF methods)
define class for flyweight
Definition: flyweight.c:212
void cor_remove_function(const int id, chain_func func)
remove to chain api
static int chain_of_resp_setter(void *this, size_t size, void *input_parameter)
setter, add function to ChainElement
flyweight_methods_t method
This is API for element of chain.
static void chain_of_resp_free(void *this)
free member resource
chain function element part class instance definition
void cor_clear(void)
clear all list
management parameter of this class API, to use flyweight
void chain_element_remove_element_part(ChainElement this, ChainElementPart element)
void cor_call(const int id, void *arg)
call chain api
Flyweight methods interface definition, to set flyweight_factory_new.
Definition: flyweight.h:14
struct chain_of_resp_mng_t cor_mng_g