BRL-CAD
hook.h
Go to the documentation of this file.
1/* H O O K . H
2 * BRL-CAD
3 *
4 * Copyright (c) 2004-2023 United States Government as represented by
5 * the U.S. Army Research Laboratory.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public License
9 * version 2.1 as published by the Free Software Foundation.
10 *
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this file; see the file named COPYING for more
18 * information.
19 */
20
21#ifndef BU_HOOK_H
22#define BU_HOOK_H
23
24#include "common.h"
25
26#include "bu/defines.h"
27
28
29__BEGIN_DECLS
30
31/** @addtogroup bu_log
32 *
33 * These are hook routines for keeping track of callback functions.
34 *
35 */
36/** @{ */
37/** @file bu/log.h */
38
39
40/** log indentation hook */
41typedef int (*bu_hook_t)(void *, void *);
42
43struct bu_hook {
44 bu_hook_t hookfunc; /**< function to call */
45 void *clientdata; /**< data for caller */
46};
47
48
50 size_t size, capacity;
51 struct bu_hook *hooks; /**< linked list */
52};
53typedef struct bu_hook bu_hook_list_t;
54
55
56/**
57 * macro suitable for declaration statement initialization of a
58 * bu_hook_list struct. does not allocate memory. not suitable for
59 * initialization of a list head node.
60 */
61#define BU_HOOK_LIST_INIT_ZERO { 0, 0, NULL}
62
63
64/**
65 * returns truthfully whether a non-head node bu_hook_list has been
66 * initialized via BU_HOOK_LIST_INIT() or BU_HOOK_LIST_INIT_ZERO.
67 */
68#define BU_HOOK_LIST_IS_INITIALIZED(_p) ((_p)->capacity != 0)
69
70
71/**
72 * initialize a hook list to empty
73 *
74 * the caller is responsible for ensuring memory is not leaked.
75 */
76BU_EXPORT extern void bu_hook_list_init(struct bu_hook_list *hlp);
77
78
79/**
80 * add a hook to the list.
81 *
82 * in addition to the callback, the call may optionally provide a data
83 * pointer that will get passed as the first argument to the 'func'
84 * hook function. the hook function may be NULL, which will result in
85 * a no-op (skipped) when bu_hook_call() is called.
86 */
87BU_EXPORT extern void bu_hook_add(struct bu_hook_list *hlp,
88 bu_hook_t func,
89 void *clientdata);
90
91
92/**
93 * delete a hook from the list.
94 *
95 * this removes a specified callback function registered with a
96 * particular data pointer via bu_hook_add() from the hook list.
97 */
98BU_EXPORT extern void bu_hook_delete(struct bu_hook_list *hlp,
99 bu_hook_t func,
100 void *clientdata);
101
102
103/**
104 * call all registered hooks.
105 *
106 * this invokes all callbacks added via bu_hook_add() passing any data
107 * pointer as the first argument and the provided 'buf' argument as
108 * the second argument, either of which may be NULL if desired.
109 */
110BU_EXPORT extern void bu_hook_call(struct bu_hook_list *hlp,
111 void *buf);
112
113
114/**
115 * copy all hooks from one list to another
116 */
117BU_EXPORT extern void bu_hook_save_all(struct bu_hook_list *source,
118 struct bu_hook_list *destination);
119
120
121/**
122 * delete all hooks in a list
123 */
124BU_EXPORT extern void bu_hook_delete_all(struct bu_hook_list *hlp);
125
126
127/**
128 * replace all hooks in a list with the hooks from another list
129 *
130 * all hooks from ther 'destination' hook list will be deleted and all
131 * hooks in the 'source' list will be copied into the 'destination'
132 * list.
133 */
134BU_EXPORT extern void bu_hook_restore_all(struct bu_hook_list *destination,
135 struct bu_hook_list *source);
136
137/** @} */
138
139__END_DECLS
140
141#endif /* BU_HOOK_H */
142
143/*
144 * Local Variables:
145 * mode: C
146 * tab-width: 8
147 * indent-tabs-mode: t
148 * c-file-style: "stroustrup"
149 * End:
150 * ex: shiftwidth=4 tabstop=8
151 */
Header file for the BRL-CAD common definitions.
void bu_hook_delete(struct bu_hook_list *hlp, bu_hook_t func, void *clientdata)
void bu_hook_add(struct bu_hook_list *hlp, bu_hook_t func, void *clientdata)
void bu_hook_restore_all(struct bu_hook_list *destination, struct bu_hook_list *source)
void bu_hook_call(struct bu_hook_list *hlp, void *buf)
int(* bu_hook_t)(void *, void *)
Definition: hook.h:41
void bu_hook_delete_all(struct bu_hook_list *hlp)
void bu_hook_list_init(struct bu_hook_list *hlp)
void bu_hook_save_all(struct bu_hook_list *source, struct bu_hook_list *destination)
size_t size
Definition: hook.h:50
size_t capacity
Definition: hook.h:50
struct bu_hook * hooks
Definition: hook.h:51
Definition: hook.h:43
bu_hook_t hookfunc
Definition: hook.h:44
void * clientdata
Definition: hook.h:45