GRASS 8 Programmer's Manual 8.6.0dev(2026)-c83afef6d3
Loading...
Searching...
No Matches
gis/handler.c
Go to the documentation of this file.
1/*!
2 \file lib/gis/handler.c
3
4 \brief GIS Library - Error handlers
5
6 SPDX-FileCopyrightText: 2010-2011 GRASS Development Team
7 SPDX-License-Identifier: GPL-2.0-or-later
8
9 \author Glynn Clements
10 */
11
12#include <stddef.h>
13#include <grass/gis.h>
14
15/*!
16 \brief Error handler (see G_add_error_handler() for usage)
17 */
18struct handler {
19 /*!
20 \brief Pointer to the handler routine
21 */
22 void (*func)(void *);
23 /*!
24 \brief Pointer to closure data
25 */
26 void *closure;
27};
28
29static struct handler *handlers;
30
31static int num_handlers;
32static int max_handlers;
33
34static struct handler *alloc_handler(void)
35{
36 int i;
37
38 for (i = 0; i < num_handlers; i++) {
39 struct handler *h = &handlers[i];
40
41 if (!h->func)
42 return h;
43 }
44
45 if (num_handlers >= max_handlers) {
46 max_handlers += 10;
47 handlers = G_realloc(handlers, max_handlers * sizeof(struct handler));
48 }
49
50 return &handlers[num_handlers++];
51}
52
53/*!
54 \brief Add new error handler
55
56 Example
57 \code
58 static void error_handler(void *p) {
59 const char *map = (const char *) p;
60 Vect_delete(map);
61 }
62 G_add_error_handler(error_handler, new->answer);
63 \endcode
64
65 \param func handler to add
66 \param closure pointer to closure data
67 */
68void G_add_error_handler(void (*func)(void *), void *closure)
69{
70 struct handler *h = alloc_handler();
71
72 h->func = func;
73 h->closure = closure;
74}
75
76/*!
77 \brief Remove existing error handler
78
79 \param func handler to be remove
80 \param closure pointer to closure data
81 */
82void G_remove_error_handler(void (*func)(void *), void *closure)
83{
84 int i;
85
86 for (i = 0; i < num_handlers; i++) {
87 struct handler *h = &handlers[i];
88
89 if (h->func == func && h->closure == closure) {
90 h->func = NULL;
91 h->closure = NULL;
92 }
93 }
94}
95
96/*!
97 \brief Call available error handlers (internal use only)
98 */
100{
101 int i;
102
103 for (i = 0; i < num_handlers; i++) {
104 struct handler *h = &handlers[i];
105
106 if (h->func)
107 (*h->func)(h->closure);
108 }
109}
#define NULL
Definition ccmath.h:32
#define G_realloc(p, n)
Definition defs/gis.h:138
void G_add_error_handler(void(*func)(void *), void *closure)
Add new error handler.
Definition gis/handler.c:68
void G__call_error_handlers(void)
Call available error handlers (internal use only)
Definition gis/handler.c:99
void G_remove_error_handler(void(*func)(void *), void *closure)
Remove existing error handler.
Definition gis/handler.c:82