BRL-CAD
db_attr.h
Go to the documentation of this file.
1/* D B _ A T T R . H
2 * BRL-CAD
3 *
4 * Copyright (c) 2014-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/** @file db_attr.h
21 *
22 */
23
24#ifndef RT_DB_ATTR_H
25#define RT_DB_ATTR_H
26
27#include "common.h"
28#include "bu/avs.h"
29#include "rt/defines.h"
30
31__BEGIN_DECLS
32
33
34struct rt_comb_internal; /* forward declaration */
35
36
37/* db5_attr.c */
38/**
39 * Define standard attribute types in BRL-CAD geometry. (See the
40 * attributes manual page.) These should be a collective enumeration
41 * starting from 0 and increasing without any gaps in the numbers so
42 * db5_standard_attribute() can be used as an index-based iterator.
43 */
44enum {
54 ATTR_TIMESTAMP, /* a binary attribute */
56};
57
58/* Enum to characterize status of attributes */
59enum {
63};
64
66 int attr_type; /* ID from the main attr enum list */
67 int is_binary; /* false for ASCII attributes; true for binary attributes */
68 int attr_subtype; /* ID from attribute status enum list */
69
70 /* names should be specified with alphanumeric characters
71 * (lower-case letters, no white space) and will act as unique
72 * keys to an object's attribute list */
73 const char *name; /* the "standard" name */
74 const char *description;
75 const char *examples;
76 /* list of alternative names for this attribute: */
77 const char *aliases;
78 /* property name */
79 const char *property;
80 /* a longer description for lists outside a table */
81 const char *long_description;
82};
83
84RT_EXPORT extern const struct db5_attr_ctype db5_attr_std[];
85
86/* Container for holding user-registered attributes */
88 void *internal; /**< @brief implementation-specific container for holding information*/
89};
90
91/**
92 * Initialize a user attribute registry
93 *
94 * PRIVATE: this is new API and should be considered private for the
95 * time being.
96 */
97RT_EXPORT extern void db5_attr_registry_init(struct db5_registry *registry);
98
99/**
100 * Free a user attribute registry
101 *
102 * PRIVATE: this is new API and should be considered private for the
103 * time being.
104 */
105RT_EXPORT extern void db5_attr_registry_free(struct db5_registry *registry);
106
107/**
108 * Register a user attribute
109 *
110 * PRIVATE: this is new API and should be considered private for the
111 * time being.
112 */
113RT_EXPORT extern int db5_attr_create(struct db5_registry *registry,
114 int attr_type,
115 int is_binary,
116 int attr_subtype,
117 const char *name,
118 const char *description,
119 const char *examples,
120 const char *aliases,
121 const char *property,
122 const char *long_description);
123
124/**
125 * Register a user attribute
126 *
127 * PRIVATE: this is new API and should be considered private for the
128 * time being.
129 */
130RT_EXPORT extern int db5_attr_register(struct db5_registry *registry,
131 struct db5_attr_ctype *attribute);
132
133/**
134 * De-register a user attribute
135 *
136 * PRIVATE: this is new API and should be considered private for the
137 * time being.
138 */
139RT_EXPORT extern int db5_attr_deregister(struct db5_registry *registry,
140 const char *name);
141
142/**
143 * Look to see if a specific attribute is registered
144 *
145 * PRIVATE: this is new API and should be considered private for the
146 * time being.
147 */
148RT_EXPORT extern struct db5_attr_ctype *db5_attr_get(struct db5_registry *registry,
149 const char *name);
150
151/**
152 * Get an array of pointers to all registered attributes
153 *
154 * PRIVATE: this is new API and should be considered private for the
155 * time being.
156 */
157RT_EXPORT extern struct db5_attr_ctype **db5_attr_dump(struct db5_registry *registry);
158
159
160/**
161 * Function returns the string name for a given standard attribute
162 * index. Index values returned from db5_standardize_attribute()
163 * correspond to the names returned from this function, returning the
164 * "standard" name. Callers may also iterate over all names starting
165 * with an index of zero until a NULL is returned.
166 *
167 * PRIVATE: this is new API and should be considered private for the
168 * time being.
169 */
170RT_EXPORT extern const char *db5_standard_attribute(int idx);
171
172
173/**
174 * Function returns the string definition for a given standard
175 * attribute index. Index values returned from
176 * db5_standardize_attribute_def() correspond to the definition
177 * returned from this function, returning the "standard" definition.
178 * Callers may also iterate over all names starting with an index of
179 * zero until a NULL is returned.
180 *
181 * PRIVATE: this is new API and should be considered private for the
182 * time being.
183 */
184RT_EXPORT extern const char *db5_standard_attribute_def(int idx);
185
186/**
187 * Function for recognizing various versions of the DB5 standard
188 * attribute names that have been used - returns the attribute type of
189 * the supplied attribute name, or -1 if it is not a recognized
190 * variation of the standard attributes.
191 *
192 * PRIVATE: this is new API and should be considered private for the
193 * time being.
194 */
195RT_EXPORT extern int db5_is_standard_attribute(const char *attrname);
196
197/**
198 * Ensures that an attribute set containing standard attributes with
199 * non-standard/old/deprecated names gets the standard name added. It
200 * will update the first non-standard name encountered, but will leave
201 * any subsequent matching attributes found unmodified if they have
202 * different values. Such "conflict" attributes must be resolved
203 * manually.
204 *
205 * Returns the number of conflicting attributes.
206 *
207 * PRIVATE: this is new API and should be considered private for the
208 * time being.
209 */
210RT_EXPORT extern size_t db5_standardize_avs(struct bu_attribute_value_set *avs);
211
212/**
213 * PRIVATE: this is new API and should be considered private for the
214 * time being.
215 */
216RT_EXPORT extern int db5_standardize_attribute(const char *attr);
217/**
218 * PRIVATE: this is new API and should be considered private for the
219 * time being.
220 */
221RT_EXPORT extern void db5_sync_attr_to_comb(struct rt_comb_internal *comb, const struct bu_attribute_value_set *avs, struct directory *dp);
222/**
223 * PRIVATE: this is new API and should be considered private for the
224 * time being.
225 */
226RT_EXPORT extern void db5_sync_comb_to_attr(struct bu_attribute_value_set *avs, const struct rt_comb_internal *comb);
227
228/* Convenience macros */
229#define ATTR_STD(attr) db5_standard_attribute(db5_standardize_attribute(attr))
230
231
232/**
233 * Convert the on-disk encoding into a handy easy-to-use
234 * bu_attribute_value_set structure.
235 *
236 * Take advantage of the readonly_min/readonly_max capability so that
237 * we don't have to bu_strdup() each string, but can simply point to
238 * it in the provided buffer *ap. Important implication: don't free
239 * *ap until you're done with this avs.
240 *
241 * The upshot of this is that bu_avs_add() and bu_avs_remove() can be
242 * safely used with this *avs.
243 *
244 * Returns -
245 * >0 count of attributes successfully imported
246 * -1 Error, mal-formed input
247 */
248RT_EXPORT extern int db5_import_attributes(struct bu_attribute_value_set *avs,
249 const struct bu_external *ap);
250
251/**
252 * Encode the attribute-value pair information into the external
253 * on-disk format.
254 *
255 * The on-disk encoding is:
256 *
257 * name1 NULL value1 NULL ... nameN NULL valueN NULL NULL
258 *
259 * For binary attributes the on-disk encoding is:
260 *
261 * bname1 NULL uchar valuelen1 comment1 NULL bvalue1 NULL ...
262 * bnameN NULL uchar valuelenN commentN NULL bvalueN NULL NULL
263 *
264 * 'ext' is initialized on behalf of the caller.
265 */
266RT_EXPORT extern void db5_export_attributes(struct bu_external *ap,
267 const struct bu_attribute_value_set *avs);
268
269
270
271/* lookup directory entries based on attributes */
272
273/**
274 * lookup directory entries based on directory flags (dp->d_flags) and
275 * attributes the "dir_flags" arg is a mask for the directory flags
276 * the *"avs" is an attribute value set used to select from the
277 * objects that *pass the flags mask. if "op" is 1, then the object
278 * must have all the *attributes and values that appear in "avs" in
279 * order to be *selected. If "op" is 2, then the object must have at
280 * least one of *the attribute/value pairs from "avs".
281 *
282 * dir_flags are in the form used in struct directory (d_flags)
283 *
284 * for op:
285 * 1 -> all attribute name/value pairs must be present and match
286 * 2 -> at least one of the name/value pairs must be present and match
287 *
288 * returns a ptbl list of selected directory pointers. An empty list
289 * means nothing met the requirements and a NULL return means something
290 * went wrong.
291 */
292RT_EXPORT extern struct bu_ptbl *db_lookup_by_attr(struct db_i *dbip,
293 int dir_flags,
294 struct bu_attribute_value_set *avs,
295 int op);
296/**
297 * Put the old region-id-color-table into the global object. A null
298 * attribute is set if the material table is empty.
299 *
300 * Returns -
301 * <0 error
302 * 0 OK
303 */
304RT_EXPORT extern int db5_put_color_table(struct db_i *dbip);
305RT_EXPORT extern int db5_update_ident(struct db_i *dbip,
306 const char *title,
307 double local2mm);
308
309
310/**
311 * Update an arbitrary number of attributes on a given database
312 * object. For efficiency, this is done without looking at the object
313 * body at all.
314 *
315 * Contents of the bu_attribute_value_set are freed, but not the
316 * struct itself.
317 *
318 * Returns -
319 * 0 on success
320 * <0 on error
321 */
322RT_EXPORT extern int db5_update_attributes(struct directory *dp,
323 struct bu_attribute_value_set *avsp,
324 struct db_i *dbip);
325
326/**
327 * A convenience routine to update the value of a single attribute.
328 *
329 * Returns -
330 * 0 on success
331 * <0 on error
332 */
333RT_EXPORT extern int db5_update_attribute(const char *obj_name,
334 const char *aname,
335 const char *value,
336 struct db_i *dbip);
337
338/**
339 * Replace the attributes of a given database object.
340 *
341 * For efficiency, this is done without looking at the object body at
342 * all. Contents of the bu_attribute_value_set are freed, but not the
343 * struct itself.
344 *
345 * Returns -
346 * 0 on success
347 * <0 on error
348 */
349RT_EXPORT extern int db5_replace_attributes(struct directory *dp,
350 struct bu_attribute_value_set *avsp,
351 struct db_i *dbip);
352
353/**
354 *
355 * Get attributes for an object pointed to by *dp
356 *
357 * returns:
358 * 0 - all is well
359 * <0 - error
360 */
361RT_EXPORT extern int db5_get_attributes(const struct db_i *dbip,
362 struct bu_attribute_value_set *avs,
363 const struct directory *dp);
364
365
366
367__END_DECLS
368
369#endif /*RT_DB_ATTR_H*/
370
371/*
372 * Local Variables:
373 * tab-width: 8
374 * mode: C
375 * indent-tabs-mode: t
376 * c-file-style: "stroustrup"
377 * End:
378 * ex: shiftwidth=4 tabstop=8
379 */
Header file for the BRL-CAD common definitions.
void db5_attr_registry_init(struct db5_registry *registry)
const char * db5_standard_attribute_def(int idx)
int db5_update_attributes(struct directory *dp, struct bu_attribute_value_set *avsp, struct db_i *dbip)
int db5_replace_attributes(struct directory *dp, struct bu_attribute_value_set *avsp, struct db_i *dbip)
int db5_attr_register(struct db5_registry *registry, struct db5_attr_ctype *attribute)
size_t db5_standardize_avs(struct bu_attribute_value_set *avs)
@ ATTR_USER_DEFINED
Definition: db_attr.h:61
@ ATTR_STANDARD
Definition: db_attr.h:60
@ ATTR_UNKNOWN_ORIGIN
Definition: db_attr.h:62
int db5_put_color_table(struct db_i *dbip)
int db5_update_attribute(const char *obj_name, const char *aname, const char *value, struct db_i *dbip)
void db5_sync_comb_to_attr(struct bu_attribute_value_set *avs, const struct rt_comb_internal *comb)
int db5_attr_deregister(struct db5_registry *registry, const char *name)
int db5_standardize_attribute(const char *attr)
struct db5_attr_ctype * db5_attr_get(struct db5_registry *registry, const char *name)
int db5_get_attributes(const struct db_i *dbip, struct bu_attribute_value_set *avs, const struct directory *dp)
void db5_export_attributes(struct bu_external *ap, const struct bu_attribute_value_set *avs)
int db5_import_attributes(struct bu_attribute_value_set *avs, const struct bu_external *ap)
const char * db5_standard_attribute(int idx)
@ ATTR_MATERIAL_ID
Definition: db_attr.h:47
@ ATTR_COLOR
Definition: db_attr.h:51
@ ATTR_LOS
Definition: db_attr.h:50
@ ATTR_SHADER
Definition: db_attr.h:52
@ ATTR_NULL
Definition: db_attr.h:55
@ ATTR_INHERIT
Definition: db_attr.h:53
@ ATTR_TIMESTAMP
Definition: db_attr.h:54
@ ATTR_REGION
Definition: db_attr.h:45
@ ATTR_AIR
Definition: db_attr.h:49
@ ATTR_REGION_ID
Definition: db_attr.h:46
@ ATTR_MATERIAL_NAME
Definition: db_attr.h:48
int db5_attr_create(struct db5_registry *registry, int attr_type, int is_binary, int attr_subtype, const char *name, const char *description, const char *examples, const char *aliases, const char *property, const char *long_description)
int db5_is_standard_attribute(const char *attrname)
const struct db5_attr_ctype db5_attr_std[]
void db5_attr_registry_free(struct db5_registry *registry)
int db5_update_ident(struct db_i *dbip, const char *title, double local2mm)
struct db5_attr_ctype ** db5_attr_dump(struct db5_registry *registry)
void db5_sync_attr_to_comb(struct rt_comb_internal *comb, const struct bu_attribute_value_set *avs, struct directory *dp)
struct bu_ptbl * db_lookup_by_attr(struct db_i *dbip, int dir_flags, struct bu_attribute_value_set *avs, int op)
Definition: ptbl.h:53
const char * examples
Definition: db_attr.h:75
int attr_subtype
Definition: db_attr.h:68
const char * description
Definition: db_attr.h:74
const char * aliases
Definition: db_attr.h:77
const char * name
Definition: db_attr.h:73
const char * property
Definition: db_attr.h:79
int attr_type
Definition: db_attr.h:66
int is_binary
Definition: db_attr.h:67
const char * long_description
Definition: db_attr.h:81
void * internal
implementation-specific container for holding information
Definition: db_attr.h:88