BRL-CAD
observer.h
Go to the documentation of this file.
1/* B U _ O B S E R V E R . 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_OBSERVER_H
22#define BU_OBSERVER_H
23
24#include "common.h"
25
26#include "bu/defines.h"
27#include "bu/magic.h"
28#include "bu/vls.h"
29
30__BEGIN_DECLS
31
32/** @addtogroup bu_observer
33 * @brief
34 * libbu observer.
35 */
36/** @{ */
37/** @file bu/observer.h */
38
39/* Generic observer cmd execution function */
40typedef void (bu_observer_eval_t)(void *, const char *);
41
42/**
43 * TBD
44 */
46 uint32_t magic;
48 struct bu_vls cmd;
49};
51#define BU_OBSERVER_NULL ((struct bu_observer *)0)
52
54 size_t size, capacity;
56};
57
58/**
59 * asserts the integrity of a non-head node bu_observer struct.
60 */
61#define BU_CK_OBSERVER(_op) BU_CKMAG(_op, BU_OBSERVER_MAGIC, "bu_observer magic")
62
63/**
64 * initializes a bu_observer struct without allocating any memory.
65 */
66#define BU_OBSERVER_INIT(_op) { \
67 (_op)->magic = BU_OBSERVER_MAGIC; \
68 BU_VLS_INIT(&(_op)->observer); \
69 BU_VLS_INIT(&(_op)->cmd); \
70 }
71
72/**
73 * macro suitable for declaration statement initialization of a bu_observer
74 * struct. does not allocate memory. not suitable for a head node.
75 */
76#define BU_OBSERVER_INIT_ZERO { BU_OBSERVER_MAGIC, BU_VLS_INIT_ZERO, BU_VLS_INIT_ZERO }
77
78#define BU_OBSERVER_LIST_INIT_ZERO { 0, 0, NULL }
79
80/**
81 * returns truthfully whether a bu_observer has been initialized.
82 */
83#define BU_OBSERVER_IS_INITIALIZED(_op) (((struct bu_observer *)(_op) != BU_OBSERVER_NULL) && LIKELY((_op)->magic == BU_OBSERVER_MAGIC))
84
85/** @brief Routines for implementing the observer pattern. */
86
87/**
88 * runs a given command, calling the corresponding observer callback
89 * if it matches.
90 */
91BU_EXPORT extern int bu_observer_cmd(void *clientData, int argc, const char *argv[]);
92
93/**
94 * Notify observers.
95 */
96BU_EXPORT extern void bu_observer_notify(void *context, struct bu_observer_list *observers, char *self, bu_observer_eval_t *ofunc);
97
98/**
99 * Free observers.
100 */
101BU_EXPORT extern void bu_observer_free(struct bu_observer_list *);
102
103/** @} */
104
105__END_DECLS
106
107#endif /* BU_OBSERVER_H */
108
109/*
110 * Local Variables:
111 * mode: C
112 * tab-width: 8
113 * indent-tabs-mode: t
114 * c-file-style: "stroustrup"
115 * End:
116 * ex: shiftwidth=4 tabstop=8
117 */
Header file for the BRL-CAD common definitions.
void() bu_observer_eval_t(void *, const char *)
Definition: observer.h:40
void bu_observer_free(struct bu_observer_list *)
void bu_observer_notify(void *context, struct bu_observer_list *observers, char *self, bu_observer_eval_t *ofunc)
int bu_observer_cmd(void *clientData, int argc, const char *argv[])
Routines for implementing the observer pattern.
Global registry of recognized magic numbers.
size_t capacity
Definition: observer.h:54
struct bu_observer * observers
Definition: observer.h:55
struct bu_vls cmd
Definition: observer.h:48
uint32_t magic
Definition: observer.h:46
struct bu_vls observer
Definition: observer.h:47
Definition: vls.h:53