libamxp  1.4.0
Patterns C Implementation
amxp_slot.c File Reference
#include <stdlib.h>
#include <string.h>
#include <amxc/amxc.h>
#include <amxp/amxp_expression.h>
#include <amxp_signal_priv.h>

Go to the source code of this file.

Macros

#define _GNU_SOURCE
 

Functions

static amxp_slot_tamxp_slot_find (const amxp_signal_t *const sig, amxp_slot_fn_t fn, amxp_slot_t *start)
 
static amxp_signal_tamxp_slot_find_signal (const amxp_signal_mngr_t *const sig_mngr, const char *const sig_name)
 
static amxp_slot_tamxp_slot_new (amxp_slot_fn_t fn, const char *regexp, const char *expression, void *const priv)
 
static int amxp_slot_connect_impl (amxp_signal_t *sig, amxp_slot_fn_t fn, const char *expression, void *const priv)
 
static int amxp_slot_connnect_all_of (amxp_signal_mngr_t *const sigmngr, amxp_slot_fn_t fn, const char *const regexp_str, const char *expression, void *const priv)
 
static int amxp_slot_connnect_all_sigmngrs (const amxc_llist_t *const sigmngrs, amxp_slot_fn_t fn, const char *const regexp, const char *expression, void *const priv)
 
static void amxp_slot_delete_all (amxp_signal_t *sig, amxp_slot_t *slot, amxp_slot_fn_t fn)
 
static void amxp_slot_disconnnect_all_of (const amxp_signal_mngr_t *const sigmngr, amxp_slot_fn_t fn)
 
static void amxp_slot_disconnect_all_sgmngrs (const amxc_llist_t *const sigmngrs, amxp_slot_fn_t fn)
 
static int amxp_slot_disconnect_name (amxp_signal_mngr_t *sig_mngr, const char *const sig_name, amxp_slot_fn_t fn)
 
int amxp_slot_connect (amxp_signal_mngr_t *const sig_mngr, const char *const sig_name, const char *const expression, amxp_slot_fn_t fn, void *const priv)
 Connects a slot (function) to a named signal of a signal manager. More...
 
int amxp_slot_connect_filtered (amxp_signal_mngr_t *const sig_mngr, const char *const sig_reg_exp, const char *const expression, amxp_slot_fn_t fn, void *const priv)
 Connects a slot (function) to signals using a regular expression. More...
 
int amxp_slot_connect_all (const char *sig_reg_exp, const char *const expression, amxp_slot_fn_t fn, void *const priv)
 Connects a slot to all existing and future signals. More...
 
int amxp_slot_disconnect (amxp_signal_mngr_t *const sig_mngr, const char *const sig_name, amxp_slot_fn_t fn)
 Disconnects a slot from (a) signal(s). More...
 
int amxp_slot_disconnect_with_priv (amxp_signal_mngr_t *sig_mngr, amxp_slot_fn_t fn, void *priv)
 Disconnects a slot from (a) signal(s). More...
 
int amxp_slot_disconnect_signal_with_priv (amxp_signal_mngr_t *sig_mngr, const char *sig_name, amxp_slot_fn_t fn, void *priv)
 Disconnects a slot from a signal. More...
 
void amxp_slot_disconnect_all (amxp_slot_fn_t fn)
 Disconnects a slot from all signals it was connected to. More...
 

Macro Definition Documentation

◆ _GNU_SOURCE

#define _GNU_SOURCE

Definition at line 56 of file amxp_slot.c.

Function Documentation

◆ amxp_slot_connect_impl()

static int amxp_slot_connect_impl ( amxp_signal_t sig,
amxp_slot_fn_t  fn,
const char *  expression,
void *const  priv 
)
static

Definition at line 128 of file amxp_slot.c.

131  {
132  int retval = -1;
133  amxp_slot_t* slot = NULL;
134 
135  slot = amxp_slot_find(sig, fn, NULL);
136  if(slot != NULL) {
137  if((expression == NULL) && (slot->expr == NULL) && (priv == NULL)) {
138  if(slot->deleted) {
139  slot->deleted = false;
140  }
141  retval = 0;
142  goto exit;
143  }
144  }
145 
146  slot = amxp_slot_new(fn, NULL, expression, priv);
147  when_null(slot, exit);
148  amxc_llist_append(&sig->slots, &slot->it);
149 
150  retval = 0;
151 
152 exit:
153  return retval;
154 }
static amxp_slot_t * amxp_slot_new(amxp_slot_fn_t fn, const char *regexp, const char *expression, void *const priv)
Definition: amxp_slot.c:103
static amxp_slot_t * amxp_slot_find(const amxp_signal_t *const sig, amxp_slot_fn_t fn, amxp_slot_t *start)
Definition: amxp_slot.c:67
amxc_llist_t slots
Definition: amxp_signal.h:121
amxc_llist_it_t it
amxp_expr_t * expr

◆ amxp_slot_connnect_all_of()

static int amxp_slot_connnect_all_of ( amxp_signal_mngr_t *const  sigmngr,
amxp_slot_fn_t  fn,
const char *const  regexp_str,
const char *  expression,
void *const  priv 
)
static

Definition at line 156 of file amxp_slot.c.

160  {
161  int retval = -1;
162  bool use_reg_exp = false;
163  const amxc_htable_t* signals = amxp_get_signals(sigmngr);
164 
165  if((regexp_str != NULL) && (regexp_str[0] != 0)) {
166  regex_t regexp;
167  retval = regcomp(&regexp, regexp_str, REG_NOSUB | REG_EXTENDED);
168  if(retval != 0) {
169  goto exit;
170  }
171  regfree(&regexp);
172  use_reg_exp = true;
173  }
174 
175  if(use_reg_exp) {
176  amxp_slot_t* slot = amxp_slot_new(fn, regexp_str, expression, priv);
177  if(slot == NULL) {
178  retval = -1;
179  goto exit;
180  }
181  amxc_llist_append(&sigmngr->regexp_slots, &slot->it);
182  retval = 0;
183  } else {
184  amxc_htable_for_each(it, signals) {
185  amxp_signal_t* sig = amxc_htable_it_get_data(it, amxp_signal_t, hit);
186  retval = amxp_slot_connect_impl(sig, fn, expression, priv);
187  when_failed(retval, exit);
188  }
189  }
190 
191 exit:
192  return retval;
193 }
const amxc_htable_t PRIVATE * amxp_get_signals(const amxp_signal_mngr_t *sig_mngr)
Definition: amxp_signal.c:325
static int amxp_slot_connect_impl(amxp_signal_t *sig, amxp_slot_fn_t fn, const char *expression, void *const priv)
Definition: amxp_slot.c:128
amxc_llist_t regexp_slots
Definition: amxp_signal.h:106
Structure containing the signal information.
Definition: amxp_signal.h:119
static amxp_signal_mngr_t * sigmngr

◆ amxp_slot_connnect_all_sigmngrs()

static int amxp_slot_connnect_all_sigmngrs ( const amxc_llist_t *const  sigmngrs,
amxp_slot_fn_t  fn,
const char *const  regexp,
const char *  expression,
void *const  priv 
)
static

Definition at line 195 of file amxp_slot.c.

199  {
200  int retval = -1;
201 
202  amxc_llist_for_each(it, sigmngrs) {
203  amxp_signal_mngr_t* sigmngr = amxc_llist_it_get_data(it,
205  it);
207  fn,
208  regexp,
209  expression,
210  priv), exit);
211  }
212 
213  retval = 0;
214 
215 exit:
216  return retval;
217 }
static int amxp_slot_connnect_all_of(amxp_signal_mngr_t *const sigmngr, amxp_slot_fn_t fn, const char *const regexp_str, const char *expression, void *const priv)
Definition: amxp_slot.c:156
Structure containing the signal manager information.
Definition: amxp_signal.h:103

◆ amxp_slot_delete_all()

static void amxp_slot_delete_all ( amxp_signal_t sig,
amxp_slot_t slot,
amxp_slot_fn_t  fn 
)
static

Definition at line 219 of file amxp_slot.c.

221  {
222  while(slot != NULL) {
223  slot->deleted = true;
224  slot = amxp_slot_find(sig, fn, slot);
225  }
226 }

◆ amxp_slot_disconnect_all_sgmngrs()

static void amxp_slot_disconnect_all_sgmngrs ( const amxc_llist_t *const  sigmngrs,
amxp_slot_fn_t  fn 
)
static

Definition at line 253 of file amxp_slot.c.

254  {
255 
256  amxc_llist_for_each(it, sigmngrs) {
257  amxp_signal_mngr_t* sigmngr = amxc_llist_it_get_data(it,
259  it);
261  }
262 }
static void amxp_slot_disconnnect_all_of(const amxp_signal_mngr_t *const sigmngr, amxp_slot_fn_t fn)
Definition: amxp_slot.c:228

◆ amxp_slot_disconnect_name()

static int amxp_slot_disconnect_name ( amxp_signal_mngr_t sig_mngr,
const char *const  sig_name,
amxp_slot_fn_t  fn 
)
static

Definition at line 264 of file amxp_slot.c.

266  {
267  int retval = -1;
268  amxp_signal_t* sig = NULL;
269  amxp_slot_t* slot = NULL;
270  sig_mngr = amxp_get_sigmngr(sig_mngr);
271 
272  sig = amxp_slot_find_signal(sig_mngr, sig_name);
273  if(sig != NULL) {
274  slot = amxp_slot_find(sig, fn, NULL);
275  }
276  if((sig != NULL) && (slot != NULL)) {
277  amxp_slot_delete_all(sig, slot, fn);
278  retval = 0;
279  } else {
280  amxc_llist_it_t* it = amxc_llist_get_first(&sig_mngr->regexp_slots);
281  amxc_llist_it_t* next = NULL;
282  while(it) {
283  regex_t regexp;
284  slot = amxc_llist_it_get_data(it, amxp_slot_t, it);
285  next = amxc_llist_it_get_next(it);
286  regcomp(&regexp, slot->regexp, REG_NOSUB | REG_EXTENDED);
287  if((slot->fn == fn) &&
288  ( regexec(&regexp, sig_name, 0, NULL, 0) == 0)) {
289  slot->deleted = true;
290  retval = 0;
291  }
292  regfree(&regexp);
293  it = next;
294  }
295  }
296 
297  return retval;
298 }
amxp_signal_mngr_t * amxp_get_sigmngr(amxp_signal_mngr_t *sig_mngr)
Definition: amxp_signal.c:315
static void amxp_slot_delete_all(amxp_signal_t *sig, amxp_slot_t *slot, amxp_slot_fn_t fn)
Definition: amxp_slot.c:219
static amxp_signal_t * amxp_slot_find_signal(const amxp_signal_mngr_t *const sig_mngr, const char *const sig_name)
Definition: amxp_slot.c:88
amxp_slot_fn_t fn

◆ amxp_slot_disconnnect_all_of()

static void amxp_slot_disconnnect_all_of ( const amxp_signal_mngr_t *const  sigmngr,
amxp_slot_fn_t  fn 
)
static

Definition at line 228 of file amxp_slot.c.

229  {
230  const amxc_htable_t* signals = amxp_get_signals(sigmngr);
231  amxc_llist_it_t* next = NULL;
232  amxc_llist_it_t* regexp_it = NULL;
233  amxc_htable_for_each(it, signals) {
234  amxp_slot_t* slot = NULL;
235  amxp_signal_t* sig = NULL;
236  sig = amxc_htable_it_get_data(it, amxp_signal_t, hit);
237  slot = amxp_slot_find(sig, fn, NULL);
238  amxp_slot_delete_all(sig, slot, fn);
239  }
240 
241  regexp_it = amxc_llist_get_first(&sigmngr->regexp_slots);
242  while(regexp_it) {
243  amxp_slot_t* slot = NULL;
244  next = amxc_llist_it_get_next(regexp_it);
245  slot = amxc_llist_it_get_data(regexp_it, amxp_slot_t, it);
246  if(slot->fn == fn) {
247  slot->deleted = true;
248  }
249  regexp_it = next;
250  }
251 }

◆ amxp_slot_find()

static amxp_slot_t* amxp_slot_find ( const amxp_signal_t *const  sig,
amxp_slot_fn_t  fn,
amxp_slot_t start 
)
static

Definition at line 67 of file amxp_slot.c.

69  {
70  amxp_slot_t* slot = start;
71  amxc_llist_it_t* it = slot == NULL ? amxc_llist_get_first(&sig->slots) :
72  amxc_llist_it_get_next(&slot->it);
73 
74  while(it != NULL) {
75  slot = amxc_llist_it_get_data(it, amxp_slot_t, it);
76  if(slot->fn == fn) {
77  goto exit;
78  }
79  it = amxc_llist_it_get_next(&slot->it);
80  }
81 
82  slot = NULL;
83 
84 exit:
85  return slot;
86 }

◆ amxp_slot_find_signal()

static amxp_signal_t* amxp_slot_find_signal ( const amxp_signal_mngr_t *const  sig_mngr,
const char *const  sig_name 
)
static

Definition at line 88 of file amxp_slot.c.

89  {
90  amxc_htable_it_t* hit = NULL;
91  const amxc_htable_t* signals = amxp_get_signals(sig_mngr);
92  amxp_signal_t* sig = NULL;
93 
94  hit = amxc_htable_get(signals, sig_name);
95  when_null(hit, exit);
96 
97  sig = amxc_htable_it_get_data(hit, amxp_signal_t, hit);
98 
99 exit:
100  return sig;
101 }

◆ amxp_slot_new()

static amxp_slot_t* amxp_slot_new ( amxp_slot_fn_t  fn,
const char *  regexp,
const char *  expression,
void *const  priv 
)
static

Definition at line 103 of file amxp_slot.c.

106  {
107  int retval = -1;
108  amxp_slot_t* slot = (amxp_slot_t*) calloc(1, sizeof(amxp_slot_t));
109  when_null(slot, exit);
110  slot->fn = fn;
111  slot->priv = priv;
112  slot->regexp = regexp == NULL ? NULL : strdup(regexp);
113  if(expression != NULL) {
114  when_failed(amxp_expr_new(&slot->expr, expression), exit);
115  }
116  retval = 0;
117 
118 exit:
119  if((retval != 0) && (slot != NULL)) {
120  amxp_expr_delete(&slot->expr);
121  free(slot->regexp);
122  free(slot);
123  slot = NULL;
124  }
125  return slot;
126 }
amxp_expr_status_t amxp_expr_new(amxp_expr_t **expr, const char *expression)
Allocates and initializes an expression.
void amxp_expr_delete(amxp_expr_t **expr)
Deletes a previously allocated expression structure.