source: mainline/uspace/lib/c/include/adt/generic_char_map.h@ 7c34b28f

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7c34b28f was aaa3f33a, checked in by Jiri Svoboda <jiri@…>, 15 years ago

Remove xxx_ref typedefs (part 5).

  • Property mode set to 100644
File size: 4.6 KB
Line 
1/*
2 * Copyright (c) 2009 Lukas Mejdrech
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup libc
30 * @{
31 */
32
33/** @file
34 * Character string to generic type map.
35 */
36
37#ifndef LIBC_GENERIC_CHAR_MAP_H_
38#define LIBC_GENERIC_CHAR_MAP_H_
39
40#include <unistd.h>
41#include <errno.h>
42
43#include <adt/char_map.h>
44#include <adt/generic_field.h>
45
46/** Internal magic value for a&nbsp;map consistency check. */
47#define GENERIC_CHAR_MAP_MAGIC_VALUE 0x12345622
48
49/** Character string to generic type map declaration.
50 * @param[in] name Name of the map.
51 * @param[in] type Inner object type.
52 */
53#define GENERIC_CHAR_MAP_DECLARE(name, type) \
54 GENERIC_FIELD_DECLARE(name##_items, type) \
55 \
56 typedef struct name name##_t; \
57 \
58 struct name { \
59 char_map_t names; \
60 name##_items_t values; \
61 int magic; \
62 }; \
63 \
64 int name##_add(name##_t *, const char *, const size_t, type *); \
65 int name##_count(name##_t *); \
66 void name##_destroy(name##_t *); \
67 void name##_exclude(name##_t *, const char *, const size_t); \
68 type *name##_find(name##_t *, const char *, const size_t); \
69 int name##_initialize(name##_t *); \
70 int name##_is_valid(name##_t *);
71
72/** Character string to generic type map implementation.
73 *
74 * Should follow declaration with the same parameters.
75 *
76 * @param[in] name Name of the map.
77 * @param[in] type Inner object type.
78 */
79#define GENERIC_CHAR_MAP_IMPLEMENT(name, type) \
80 GENERIC_FIELD_IMPLEMENT(name##_items, type) \
81 \
82 int name##_add(name##_t *map, const char *name, const size_t length, \
83 type *value) \
84 { \
85 int rc; \
86 int index; \
87 if (!name##_is_valid(map)) \
88 return EINVAL; \
89 index = name##_items_add(&map->values, value); \
90 if (index < 0) \
91 return index; \
92 rc = char_map_add(&map->names, name, length, index); \
93 if (rc != EOK) { \
94 name##_items_exclude_index(&map->values, index); \
95 return rc; \
96 } \
97 return EOK; \
98 } \
99 \
100 int name##_count(name##_t *map) \
101 { \
102 return name##_is_valid(map) ? \
103 name##_items_count(&map->values) : -1; \
104 } \
105 \
106 void name##_destroy(name##_t *map) \
107 { \
108 if (name##_is_valid(map)) { \
109 char_map_destroy(&map->names); \
110 name##_items_destroy(&map->values); \
111 } \
112 } \
113 \
114 void name##_exclude(name##_t *map, const char *name, \
115 const size_t length) \
116 { \
117 if (name##_is_valid(map)) { \
118 int index; \
119 index = char_map_exclude(&map->names, name, length); \
120 if (index != CHAR_MAP_NULL) \
121 name##_items_exclude_index(&map->values, \
122 index); \
123 } \
124 } \
125 \
126 type *name##_find(name##_t *map, const char *name, \
127 const size_t length) \
128 { \
129 if (name##_is_valid(map)) { \
130 int index; \
131 index = char_map_find(&map->names, name, length); \
132 if( index != CHAR_MAP_NULL) \
133 return name##_items_get_index(&map->values, \
134 index); \
135 } \
136 return NULL; \
137 } \
138 \
139 int name##_initialize(name##_t *map) \
140 { \
141 int rc; \
142 if (!map) \
143 return EINVAL; \
144 rc = char_map_initialize(&map->names); \
145 if (rc != EOK) \
146 return rc; \
147 rc = name##_items_initialize(&map->values); \
148 if (rc != EOK) { \
149 char_map_destroy(&map->names); \
150 return rc; \
151 } \
152 map->magic = GENERIC_CHAR_MAP_MAGIC_VALUE; \
153 return EOK; \
154 } \
155 \
156 int name##_is_valid(name##_t *map) \
157 { \
158 return map && (map->magic == GENERIC_CHAR_MAP_MAGIC_VALUE); \
159 }
160
161#endif
162
163/** @}
164 */
Note: See TracBrowser for help on using the repository browser.