GRASS GIS 7 Programmer's Manual  7.0.2(2015)-r00000
handler.c
Go to the documentation of this file.
1 
15 #include <stddef.h>
16 #include <grass/gis.h>
17 
21 struct handler {
25  void (*func)(void *);
29  void *closure;
30 };
31 
32 static struct handler *handlers;
33 
34 static int num_handlers;
35 static int max_handlers;
36 
37 static struct handler *alloc_handler(void)
38 {
39  int i;
40 
41  for (i = 0; i < num_handlers; i++) {
42  struct handler *h = &handlers[i];
43  if (!h->func)
44  return h;
45  }
46 
47  if (num_handlers >= max_handlers) {
48  max_handlers += 10;
49  handlers = G_realloc(handlers, max_handlers * sizeof(struct handler));
50  }
51 
52  return &handlers[num_handlers++];
53 }
54 
70 void G_add_error_handler(void (*func)(void *), void *closure)
71 {
72  struct handler *h = alloc_handler();
73 
74  h->func = func;
75  h->closure = closure;
76 }
77 
84 void G_remove_error_handler(void (*func)(void *), void *closure)
85 {
86  int i;
87 
88  for (i = 0; i < num_handlers; i++) {
89  struct handler *h = &handlers[i];
90 
91  if (h->func == func && h->closure == closure) {
92  h->func = NULL;
93  h->closure = NULL;
94  }
95  }
96 }
97 
102 {
103  int i;
104 
105  for (i = 0; i < num_handlers; i++) {
106  struct handler *h = &handlers[i];
107  if (h->func)
108  (*h->func)(h->closure);
109  }
110 }
111 
#define NULL
Definition: ccmath.h:32
void G_add_error_handler(void(*func)(void *), void *closure)
Add new error handler.
Definition: handler.c:70
void G__call_error_handlers(void)
Call available error handlers (internal use only)
Definition: handler.c:101
void G_remove_error_handler(void(*func)(void *), void *closure)
Remove existing error hander.
Definition: handler.c:84