BRL-CAD
db5_attr_registry.cpp
Go to the documentation of this file.
1 /* D B 5 _ A T T R _ R E G I S T R Y . C P P
2  * BRL-CAD
3  *
4  * Copyright (c) 2013-2014 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 #include "common.h"
22 
23 #include <map>
24 #include <string>
25 
26 #include "raytrace.h"
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31  void db5_attr_registry_init(struct db5_registry *registry);
32  void db5_attr_registry_free(struct db5_registry *registry);
33  int db5_attr_create(struct db5_registry *registry,
34  int attr_type,
35  int is_binary,
36  int attr_subtype,
37  const char *name,
38  const char *description,
39  const char *examples,
40  const char *aliases,
41  const char *property,
42  const char *long_description);
43  int db5_attr_register(struct db5_registry *registry, struct db5_attr_ctype *attribute);
44  int db5_attr_deregister(struct db5_registry *registry, const char *name);
45  struct db5_attr_ctype *db5_attr_get(struct db5_registry *registry, const char *name);
46  struct db5_attr_ctype **db5_attr_dump(struct db5_registry *registry);
47 #ifdef __cplusplus
48 }
49 #endif
50 
51 #define attr_regmap_t std::map<std::string, struct db5_attr_ctype *>
52 #define attr_regmap_it std::map<std::string, struct db5_attr_ctype *>::iterator
53 
54 void db5_attr_registry_init(struct db5_registry *registry)
55 {
56  if (!registry) return;
57  registry->internal = (void *)new attr_regmap_t;
58 }
59 
60 HIDDEN
61 void db5_attr_ctype_free(struct db5_attr_ctype *attr)
62 {
63  bu_free((void *)attr->name, "free attr name");
64  if (attr->description) bu_free((void *)attr->description, "free attr description");
65  if (attr->examples) bu_free((void *)attr->examples, "free attr examples");
66  if (attr->aliases) bu_free((void *)attr->aliases, "free attr aliases");
67  if (attr->property) bu_free((void *)attr->property, "free attr property");
68  if (attr->long_description) bu_free((void *)attr->long_description, "free attr long_description");
69  bu_free(attr, "free registered attribute");
70 }
71 
72 void
73 db5_attr_registry_free(struct db5_registry *registry)
74 {
75  if (!registry) return;
76  attr_regmap_t *regmap = static_cast<attr_regmap_t *>(registry->internal);
77  attr_regmap_it it;
78  for (it = regmap->begin(); it != regmap->end(); it++) {
79  struct db5_attr_ctype *attr = (struct db5_attr_ctype *)(*it).second;
80  db5_attr_ctype_free(attr);
81  }
82  regmap->clear();
83 }
84 
85 int
86 db5_attr_create(struct db5_registry *registry,
87  int attr_type,
88  int is_binary,
89  int attr_subtype,
90  const char *name,
91  const char *description,
92  const char *examples,
93  const char *aliases,
94  const char *property,
95  const char *long_description)
96 {
97  if (!registry) return -1;
98  if (!name || strlen(name) == 0) return -1;
99  attr_regmap_t *regmap = static_cast<attr_regmap_t *>(registry->internal);
100  if (regmap->find(std::string(name)) != regmap->end()) return -1;
101  struct db5_attr_ctype *new_attr = (struct db5_attr_ctype *)bu_calloc(1, sizeof(struct db5_attr_ctype), "new attribute");
102  new_attr->attr_type = attr_type;
103  new_attr->is_binary = is_binary;
104  new_attr->attr_subtype = attr_subtype;
105  new_attr->name = bu_strdup(name);
106  if (description) new_attr->description = bu_strdup(description);
107  if (examples) new_attr->examples = bu_strdup(examples);
108  if (aliases) new_attr->aliases = bu_strdup(aliases);
109  if (property) new_attr->property = bu_strdup(property);
110  if (long_description) new_attr->long_description = bu_strdup(long_description);
111  (*regmap)[std::string(name)] = new_attr;
112  return 0;
113 }
114 
115 
116 int
117 db5_attr_register(struct db5_registry *registry, struct db5_attr_ctype *attribute)
118 {
119  if (!registry) return -1;
120  attr_regmap_t *regmap = static_cast<attr_regmap_t *>(registry->internal);
121  attr_regmap_it it = regmap->find(std::string(attribute->name));
122  if (it == regmap->end()) {
123  (*regmap)[std::string(attribute->name)] = attribute;
124  return 0;
125  }
126  return 1;
127 }
128 
129 int
130 db5_attr_deregister(struct db5_registry *registry, const char *name)
131 {
132  if (!registry) return -1;
133  attr_regmap_t *regmap = static_cast<attr_regmap_t *>(registry->internal);
134  attr_regmap_it it = regmap->find(std::string(name));
135  if (it != regmap->end()) {
136  struct db5_attr_ctype *attr = (struct db5_attr_ctype *)(*it).second;
137  db5_attr_ctype_free(attr);
138  regmap->erase(it);
139  return 0;
140  }
141  return 1;
142 }
143 
144 
145 struct db5_attr_ctype *
146 db5_attr_get(struct db5_registry *registry, const char *name)
147 {
148  if (!registry) return NULL;
149  attr_regmap_t *regmap = static_cast<attr_regmap_t *>(registry->internal);
150  attr_regmap_it it = regmap->find(std::string(name));
151  if (it != regmap->end()) {
152  return (struct db5_attr_ctype *)(*it).second;
153  }
154  return NULL;
155 }
156 
157 struct db5_attr_ctype **
158 db5_attr_dump(struct db5_registry *registry)
159 {
160  int i = 0;
161  if (!registry) return NULL;
162  attr_regmap_t *regmap = static_cast<attr_regmap_t *>(registry->internal);
163  if (!regmap->size()) return NULL;
164  attr_regmap_it it;
165  struct db5_attr_ctype **attr_array = (struct db5_attr_ctype **)bu_calloc(regmap->size() + 1, sizeof(struct db5_attr_ctype *), "attribute pointer list");
166  for (it = regmap->begin(); it != regmap->end(); it++) {
167  struct db5_attr_ctype *attr = (struct db5_attr_ctype *)(*it).second;
168  attr_array[i] = attr;
169  i++;
170  }
171  return attr_array;
172 }
173 
174 /*
175  * Local Variables:
176  * mode: C++
177  * tab-width: 8
178  * c-basic-offset: 4
179  * indent-tabs-mode: t
180  * c-file-style: "stroustrup"
181  * End:
182  * ex: shiftwidth=4 tabstop=8
183  */
184 
struct db5_attr_ctype ** db5_attr_dump(struct db5_registry *registry)
int db5_attr_deregister(struct db5_registry *registry, const char *name)
Definition: clone.c:90
Header file for the BRL-CAD common definitions.
void db5_attr_registry_init(struct db5_registry *registry)
#define HIDDEN
Definition: common.h:86
void db5_attr_registry_free(struct db5_registry *registry)
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)
void * bu_calloc(size_t nelem, size_t elsize, const char *str)
Definition: malloc.c:321
#define attr_regmap_t
int db5_attr_register(struct db5_registry *registry, struct db5_attr_ctype *attribute)
#define attr_regmap_it
struct db5_attr_ctype * db5_attr_get(struct db5_registry *registry, const char *name)
void bu_free(void *ptr, const char *str)
Definition: malloc.c:328
HIDDEN void db5_attr_ctype_free(struct db5_attr_ctype *attr)
#define bu_strdup(s)
Definition: str.h:71