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