libamxp  1.4.0
Patterns C Implementation
Collaboration diagram for Slots:

Typedefs

typedef void(* amxp_deferred_fn_t) (const amxc_var_t *const data, void *const priv)
 Deferred callback function signature. More...
 
typedef void(* amxp_slot_fn_t) (const char *const sig_name, const amxc_var_t *const data, void *const priv)
 Slot callback function signature. More...
 

Functions

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 *const 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...
 

Detailed Description

Slots are callback functions that are connected to named signals.

When a signal is triggered, all slots (callback functions) connected to the named signal are called.

Typedef Documentation

◆ amxp_deferred_fn_t

typedef void(* amxp_deferred_fn_t) (const amxc_var_t *const data, void *const priv)

Deferred callback function signature.

Deferred functions are called from the eventloop

Parameters
datasome data
priva pointer to some data

Definition at line 137 of file amxp_signal.h.

◆ amxp_slot_fn_t

typedef void(* amxp_slot_fn_t) (const char *const sig_name, const amxc_var_t *const data, void *const priv)

Slot callback function signature.

A slot is a callback function with this signature.

Parameters
sig_namethe name of the signal
datathe data of the signal
priva pointer to some data, provided when connecting

Definition at line 93 of file amxp_slot.h.

Function Documentation

◆ amxp_slot_connect()

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.

When the function (slot) is connected to the signal, the function is called each time the signal is "triggered".

When "*" is used as signal name, the slots is connected to all know signals of the given signal manager or when sig_mngr is NULL to all know signals from all signal managers.

When sig_mgr is NULL and sig_name is "*", the slot will also be connected to new added signals (signals that were not existing at the time of call to this function).

When a valid expression is added the slot is only called when expression evaluates to true, using the signal data as input. If the signal has not data the slot is also called even when an expression is provided.

Use amxp_slot_disconnect or amxp_slot_disconnect_all to disconnect the slot from signals.

Parameters
sig_mngrthe signal manager, or null for the global signal manager
sig_namethe signal you want to connect to
expressionNull or a string containing a valid expression
fnthe slot function (callback function)
priva pointer to some data, will be passed to the slot function when the signal is triggered
Returns
0 when connection was successful, otherwise an error occurred

Definition at line 300 of file amxp_slot.c.

304  {
305  int retval = -1;
306  amxp_signal_t* sig = NULL;
307 
308  when_null(sig_name, exit);
309  when_true(*sig_name == 0, exit);
310  when_null(fn, exit);
311 
312  if(strcmp(sig_name, "*") == 0) {
313  if(sig_mngr == NULL) {
314  retval = amxp_slot_connect_all(NULL, expression, fn, priv);
315  } else {
316  retval = amxp_slot_connnect_all_of(sig_mngr, fn, NULL, expression, priv);
317  }
318  goto exit;
319  }
320 
321  sig = amxp_slot_find_signal(sig_mngr, sig_name);
322  when_null(sig, exit);
323 
324  retval = amxp_slot_connect_impl(sig, fn, expression, priv);
325 
326 exit:
327  return retval;
328 }
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
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
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
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.
Definition: amxp_slot.c:353
Structure containing the signal information.
Definition: amxp_signal.h:119

◆ amxp_slot_connect_all()

int amxp_slot_connect_all ( const char *const  sig_reg_exp,
const char *const  expression,
amxp_slot_fn_t  fn,
void *const  priv 
)

Connects a slot to all existing and future signals.

This function does exactly the same as amxp_slot_connect where sig_name is "*" and sig_mngr is NULL, if no signal regular expression is provided (NULL).

If a regular expression is provided the slot is connected to all known and future signals that matches the regular expression.

Parameters
sig_reg_expa regular expression, the slot is connected to all signals matching the regular expression
expressionNull or a string containing a valid expression
fnthe slot function (callback function)
priva pointer to some data, will be passed to the slot function when the signal is triggered
Returns
0 when connection was successful, otherwise an error occurred

Definition at line 353 of file amxp_slot.c.

356  {
357  int retval = -1;
358  amxc_llist_t* sigmngrs = NULL;
359  amxc_llist_t* pending_sigmngrs = NULL;
360 
361  when_null(fn, exit);
362 
363  amxp_get_sigmngrs(&sigmngrs, &pending_sigmngrs);
364  when_failed(amxp_slot_connnect_all_sigmngrs(sigmngrs,
365  fn,
366  sig_reg_exp,
367  expression,
368  priv), exit);
369  when_failed(amxp_slot_connnect_all_sigmngrs(pending_sigmngrs,
370  fn,
371  sig_reg_exp,
372  expression,
373  priv), exit);
374  retval = 0;
375 
376 exit:
377  return retval;
378 }
void PRIVATE amxp_get_sigmngrs(amxc_llist_t **sigmngrs, amxc_llist_t **pending_sigmngrs)
Definition: amxp_signal.c:319
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)
Definition: amxp_slot.c:195

◆ amxp_slot_connect_filtered()

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.

The slot is connected to all signals of which the name is matching the given regular expression. If later signals are added and the name of the new signal is also matching the regular expression the slot will be automatically connected to the new signal as well.

Optionally a expression can be added. The expression works only on signals the provide a data. The slot is only called when the expression evalutes to true or when the signal has no data.

Use amxp_slot_disconnect or amxp_slot_disconnect_all to disconnect the slot from signals.

Parameters
sig_mngrthe signal manager, or null for the global signal manager
sig_reg_expa regular expression, the slot is connected to all signals matching the regular expression
expressionNull or a string containing a valid expression
fnthe slot function (callback function)
priva pointer to some data, will be passed to the slot function when the signal is triggered
Returns
0 when connection was successful, otherwise an error occurred

Definition at line 330 of file amxp_slot.c.

334  {
335  int retval = -1;
336  amxp_signal_mngr_t* mngr = NULL;
337  when_null(sig_reg_exp, exit);
338  when_true(*sig_reg_exp == 0, exit);
339  when_null(fn, exit);
340 
341  mngr = amxp_get_sigmngr(sig_mngr);
342  when_null(mngr, exit);
343  retval = amxp_slot_connnect_all_of(mngr,
344  fn,
345  sig_reg_exp,
346  expression,
347  priv);
348 
349 exit:
350  return retval;
351 }
amxp_signal_mngr_t * amxp_get_sigmngr(amxp_signal_mngr_t *sig_mngr)
Definition: amxp_signal.c:315
Structure containing the signal manager information.
Definition: amxp_signal.h:103

◆ amxp_slot_disconnect()

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).

Disconnecting a slot using this function will:

  • Remove all connections of the slot from the signal with the matching name
  • Remove all connections with a regular expression matching the name.

A removed slot will not be called anymore when the signal is triggered.

When "*" is given as sign_name, the slot will be disconnected from all signals of the specified signal manager (sig_mngr), when sig_mngr is NULL, the slot is disconnected from all signals it was connected to.

Parameters
sig_mngrthe signal manager, or null for the global signal manager
sig_namethe signal you want to disconnect from use "*" for all
fnthe slot function (callback function).
Returns
0 when disconnection was successful, otherwise an error occurred.

Definition at line 380 of file amxp_slot.c.

382  {
383  int retval = -1;
384 
385  when_null(sig_name, exit);
386  when_true(*sig_name == 0, exit);
387  when_null(fn, exit);
388 
389  if(strcmp(sig_name, "*") == 0) {
390  if(sig_mngr == NULL) {
392  } else {
393  amxp_slot_disconnnect_all_of(sig_mngr, fn);
394  }
395  retval = 0;
396  goto exit;
397  }
398 
399  retval = amxp_slot_disconnect_name(sig_mngr, sig_name, fn);
400 
401 exit:
402  return retval;
403 }
static void amxp_slot_disconnnect_all_of(const amxp_signal_mngr_t *const sigmngr, amxp_slot_fn_t fn)
Definition: amxp_slot.c:228
static int amxp_slot_disconnect_name(amxp_signal_mngr_t *sig_mngr, const char *const sig_name, amxp_slot_fn_t fn)
Definition: amxp_slot.c:264
void amxp_slot_disconnect_all(amxp_slot_fn_t fn)
Disconnects a slot from all signals it was connected to.
Definition: amxp_slot.c:459

◆ amxp_slot_disconnect_all()

void amxp_slot_disconnect_all ( amxp_slot_fn_t  fn)

Disconnects a slot from all signals it was connected to.

The slot is removed from all signals it was connected to, including regular expression slot (see amxp_slot_connect_filtered)

Parameters
fnthe slot function (callback function)

Definition at line 459 of file amxp_slot.c.

459  {
460  amxc_llist_t* sigmngrs = NULL;
461  amxc_llist_t* pending_sigmngrs = NULL;
462 
463  when_null(fn, exit);
464 
465  amxp_get_sigmngrs(&sigmngrs, &pending_sigmngrs);
466  amxp_slot_disconnect_all_sgmngrs(sigmngrs, fn);
467  amxp_slot_disconnect_all_sgmngrs(pending_sigmngrs, fn);
468 
469 exit:
470  return;
471 }
static void amxp_slot_disconnect_all_sgmngrs(const amxc_llist_t *const sigmngrs, amxp_slot_fn_t fn)
Definition: amxp_slot.c:253

◆ amxp_slot_disconnect_signal_with_priv()

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.

Disconnecting a slot using this function will:

  • Remove all connections of the slot with the matching private data from the signal with the given name or from all signals when no signal name is given.
  • Remove all connections with a regular expression and matching private data when no signal name is given.

A removed slot will not be called anymore when the signal is triggered.

Parameters
sig_mngrthe signal manager, or null for the global signal manager
sig_namethe signal name, or NULL for all.
fnthe slot function (callback function).
privThe private data pointer that was provided when connecting
Returns
0 when disconnection was successful, otherwise an error occurred.

Definition at line 411 of file amxp_slot.c.

414  {
415  int retval = -1;
416  amxc_llist_it_t* slot_it = NULL;
417  amxc_llist_it_t* next = NULL;
418  const amxc_htable_t* signals = NULL;
419  sig_mngr = amxp_get_sigmngr(sig_mngr);
420  signals = amxp_get_signals(sig_mngr);
421 
422  amxc_htable_for_each(it, signals) {
423  amxp_signal_t* sig = amxc_htable_it_get_data(it, amxp_signal_t, hit);
424  if((sig_name != NULL) && (strcmp(sig_name, sig->name) != 0)) {
425  continue;
426  }
427  slot_it = amxc_llist_get_first(&sig->slots);
428  while(slot_it != NULL) {
429  amxp_slot_t* slot = NULL;
430  next = amxc_llist_it_get_next(slot_it);
431  slot = amxc_llist_it_get_data(slot_it, amxp_slot_t, it);
432  if(((slot->fn == fn) || (fn == NULL)) &&
433  ( slot->priv == priv)) {
434  slot->deleted = true;
435  }
436  slot_it = next;
437  }
438  }
439 
440  if(sig_name == NULL) {
441  slot_it = amxc_llist_get_first(&sig_mngr->regexp_slots);
442  while(slot_it != NULL) {
443  amxp_slot_t* slot = NULL;
444  next = amxc_llist_it_get_next(slot_it);
445  slot = amxc_llist_it_get_data(slot_it, amxp_slot_t, it);
446  if(((slot->fn == fn) || (fn == NULL)) &&
447  ( slot->priv == priv)) {
448  amxc_llist_it_clean(slot_it, amxp_free_slots);
449  }
450  slot_it = next;
451  }
452  }
453 
454  retval = 0;
455 
456  return retval;
457 }
const amxc_htable_t PRIVATE * amxp_get_signals(const amxp_signal_mngr_t *sig_mngr)
Definition: amxp_signal.c:325
void PRIVATE amxp_free_slots(amxc_llist_it_t *it)
Definition: amxp_signal.c:99
amxc_llist_t regexp_slots
Definition: amxp_signal.h:106
amxc_llist_t slots
Definition: amxp_signal.h:121
const char * name
Definition: amxp_signal.h:122
amxp_slot_fn_t fn

◆ amxp_slot_disconnect_with_priv()

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).

Disconnecting a slot using this function will:

  • Remove all connections of the slot with the matching private data from the signals
  • Remove all connections with a regular expression and matching private data

A removed slot will not be called anymore when the signal is triggered.

Parameters
sig_mngrthe signal manager, or null for the global signal manager
fnthe slot function (callback function).
privThe private data pointer that was provided when connecting
Returns
0 when disconnection was successful, otherwise an error occurred.

Definition at line 405 of file amxp_slot.c.

407  {
408  return amxp_slot_disconnect_signal_with_priv(sig_mngr, NULL, fn, priv);
409 }
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.
Definition: amxp_slot.c:411