source: mainline/uspace/lib/socket/include/adt/generic_char_map.h@ 849ed54

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 849ed54 was 849ed54, checked in by Martin Decky <martin@…>, 15 years ago

Networking work:
Split the networking stack into end-user library (libsocket) and two helper libraries (libnet and libnetif).
Don't use over-the-hand compiling and linking, but rather separation of conserns.
There might be still some issues and the non-modular networking architecture is currently broken, but this will be fixed soon.

  • Property mode set to 100644
File size: 5.8 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 net
30 * @{
31 */
32
33/** @file
34 * Character string to generic type map.
35 */
36
37#ifndef __GENERIC_CHAR_MAP_H__
38#define __GENERIC_CHAR_MAP_H__
39
40#include <errno.h>
41#include <unistd.h>
42
43#include <net_err.h>
44
45#include <adt/char_map.h>
46#include <adt/generic_field.h>
47
48/** Internal magic value for a&nbsp;map consistency check.
49 */
50#define GENERIC_CHAR_MAP_MAGIC_VALUE 0x12345622
51
52/** Character string to generic type map declaration.
53 * @param[in] name Name of the map.
54 * @param[in] type Inner object type.
55 */
56#define GENERIC_CHAR_MAP_DECLARE(name, type) \
57 \
58GENERIC_FIELD_DECLARE(name##_items, type) \
59 \
60typedef struct name name##_t; \
61typedef name##_t * name##_ref; \
62 \
63struct name{ \
64 char_map_t names; \
65 name##_items_t values; \
66 int magic; \
67}; \
68 \
69int name##_add(name##_ref map, const char * name, const size_t length, type * value); \
70int name##_count(name##_ref map); \
71void name##_destroy(name##_ref map); \
72void name##_exclude(name##_ref map, const char * name, const size_t length); \
73type * name##_find(name##_ref map, const char * name, const size_t length); \
74int name##_initialize(name##_ref map); \
75int name##_is_valid(name##_ref map);
76
77/** Character string to generic type map implementation.
78 * Should follow declaration with the same parameters.
79 * @param[in] name Name of the map.
80 * @param[in] type Inner object type.
81 */
82#define GENERIC_CHAR_MAP_IMPLEMENT(name, type) \
83 \
84GENERIC_FIELD_IMPLEMENT(name##_items, type) \
85 \
86int name##_add(name##_ref map, const char * name, const size_t length, type * value){ \
87 ERROR_DECLARE; \
88 \
89 int index; \
90 \
91 if(! name##_is_valid(map)){ \
92 return EINVAL; \
93 } \
94 index = name##_items_add(&map->values, value); \
95 if(index < 0){ \
96 return index; \
97 } \
98 if(ERROR_OCCURRED(char_map_add(&map->names, name, length, index))){ \
99 name##_items_exclude_index(&map->values, index); \
100 return ERROR_CODE; \
101 } \
102 return EOK; \
103} \
104 \
105int name##_count(name##_ref map){ \
106 return name##_is_valid(map) ? name##_items_count(&map->values) : -1; \
107} \
108 \
109void name##_destroy(name##_ref map){ \
110 if(name##_is_valid(map)){ \
111 char_map_destroy(&map->names); \
112 name##_items_destroy(&map->values); \
113 } \
114} \
115 \
116void name##_exclude(name##_ref map, const char * name, const size_t length){ \
117 if(name##_is_valid(map)){ \
118 int index; \
119 \
120 index = char_map_exclude(&map->names, name, length); \
121 if(index != CHAR_MAP_NULL){ \
122 name##_items_exclude_index(&map->values, index); \
123 } \
124 } \
125} \
126 \
127type * name##_find(name##_ref map, const char * name, const size_t length){ \
128 if(name##_is_valid(map)){ \
129 int index; \
130 \
131 index = char_map_find(&map->names, name, length); \
132 if(index != CHAR_MAP_NULL){ \
133 return name##_items_get_index(&map->values, index); \
134 } \
135 } \
136 return NULL; \
137} \
138 \
139int name##_initialize(name##_ref map){ \
140 ERROR_DECLARE; \
141 \
142 if(! map){ \
143 return EINVAL; \
144 } \
145 ERROR_PROPAGATE(char_map_initialize(&map->names)); \
146 if(ERROR_OCCURRED(name##_items_initialize(&map->values))){ \
147 char_map_destroy(&map->names); \
148 return ERROR_CODE; \
149 } \
150 map->magic = GENERIC_CHAR_MAP_MAGIC_VALUE; \
151 return EOK; \
152} \
153 \
154int name##_is_valid(name##_ref map){ \
155 return map && (map->magic == GENERIC_CHAR_MAP_MAGIC_VALUE); \
156}
157
158#endif
159
160/** @}
161 */
Note: See TracBrowser for help on using the repository browser.