Changeset 4edd39fc in mainline for uspace/lib/c/include/adt/generic_char_map.h
- Timestamp:
- 2010-09-26T15:51:09Z (14 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 820ab55c
- Parents:
- 7093a32
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/include/adt/generic_char_map.h
r7093a32 r4edd39fc 45 45 #include <adt/generic_field.h> 46 46 47 /** Internal magic value for a map consistency check. 48 */ 47 /** Internal magic value for a map consistency check. */ 49 48 #define GENERIC_CHAR_MAP_MAGIC_VALUE 0x12345622 50 49 51 50 /** Character string to generic type map declaration. 52 * @param[in] name 53 * @param[in] type 51 * @param[in] name Name of the map. 52 * @param[in] type Inner object type. 54 53 */ 55 #define GENERIC_CHAR_MAP_DECLARE(name, type) \ 56 \ 57 GENERIC_FIELD_DECLARE(name##_items, type) \ 58 \ 59 typedef struct name name##_t; \ 60 typedef name##_t * name##_ref; \ 61 \ 62 struct name{ \ 63 char_map_t names; \ 64 name##_items_t values; \ 65 int magic; \ 66 }; \ 67 \ 68 int name##_add(name##_ref map, const char * name, const size_t length, type * value); \ 69 int name##_count(name##_ref map); \ 70 void name##_destroy(name##_ref map); \ 71 void name##_exclude(name##_ref map, const char * name, const size_t length); \ 72 type * name##_find(name##_ref map, const char * name, const size_t length); \ 73 int name##_initialize(name##_ref map); \ 74 int name##_is_valid(name##_ref map); 54 #define GENERIC_CHAR_MAP_DECLARE(name, type) \ 55 GENERIC_FIELD_DECLARE(name##_items, type) \ 56 \ 57 typedef struct name name##_t; \ 58 typedef name##_t *name##_ref; \ 59 \ 60 struct name { \ 61 char_map_t names; \ 62 name##_items_t values; \ 63 int magic; \ 64 }; \ 65 \ 66 int name##_add(name##_ref, const char *, const size_t, type *); \ 67 int name##_count(name##_ref); \ 68 void name##_destroy(name##_ref); \ 69 void name##_exclude(name##_ref, const char *, const size_t); \ 70 type *name##_find(name##_ref, const char *, const size_t); \ 71 int name##_initialize(name##_ref); \ 72 int name##_is_valid(name##_ref); 75 73 76 74 /** Character string to generic type map implementation. 77 * Should follow declaration with the same parameters. 78 * @param[in] name Name of the map. 79 * @param[in] type Inner object type. 75 * 76 * Should follow declaration with the same parameters. 77 * 78 * @param[in] name Name of the map. 79 * @param[in] type Inner object type. 80 80 */ 81 #define GENERIC_CHAR_MAP_IMPLEMENT(name, type) \ 82 \ 83 GENERIC_FIELD_IMPLEMENT(name##_items, type) \ 84 \ 85 int name##_add(name##_ref map, const char * name, const size_t length, type * value){ \ 86 ERROR_DECLARE; \ 87 \ 88 int index; \ 89 \ 90 if(! name##_is_valid(map)){ \ 91 return EINVAL; \ 92 } \ 93 index = name##_items_add(&map->values, value); \ 94 if(index < 0){ \ 95 return index; \ 96 } \ 97 if(ERROR_OCCURRED(char_map_add(&map->names, name, length, index))){ \ 98 name##_items_exclude_index(&map->values, index); \ 99 return ERROR_CODE; \ 100 } \ 101 return EOK; \ 102 } \ 103 \ 104 int name##_count(name##_ref map){ \ 105 return name##_is_valid(map) ? name##_items_count(&map->values) : -1; \ 106 } \ 107 \ 108 void name##_destroy(name##_ref map){ \ 109 if(name##_is_valid(map)){ \ 110 char_map_destroy(&map->names); \ 111 name##_items_destroy(&map->values); \ 112 } \ 113 } \ 114 \ 115 void name##_exclude(name##_ref map, const char * name, const size_t length){ \ 116 if(name##_is_valid(map)){ \ 117 int index; \ 118 \ 119 index = char_map_exclude(&map->names, name, length); \ 120 if(index != CHAR_MAP_NULL){ \ 121 name##_items_exclude_index(&map->values, index); \ 122 } \ 123 } \ 124 } \ 125 \ 126 type * name##_find(name##_ref map, const char * name, const size_t length){ \ 127 if(name##_is_valid(map)){ \ 128 int index; \ 129 \ 130 index = char_map_find(&map->names, name, length); \ 131 if(index != CHAR_MAP_NULL){ \ 132 return name##_items_get_index(&map->values, index); \ 133 } \ 134 } \ 135 return NULL; \ 136 } \ 137 \ 138 int name##_initialize(name##_ref map){ \ 139 ERROR_DECLARE; \ 140 \ 141 if(! map){ \ 142 return EINVAL; \ 143 } \ 144 ERROR_PROPAGATE(char_map_initialize(&map->names)); \ 145 if(ERROR_OCCURRED(name##_items_initialize(&map->values))){ \ 146 char_map_destroy(&map->names); \ 147 return ERROR_CODE; \ 148 } \ 149 map->magic = GENERIC_CHAR_MAP_MAGIC_VALUE; \ 150 return EOK; \ 151 } \ 152 \ 153 int name##_is_valid(name##_ref map){ \ 154 return map && (map->magic == GENERIC_CHAR_MAP_MAGIC_VALUE); \ 155 } 81 #define GENERIC_CHAR_MAP_IMPLEMENT(name, type) \ 82 GENERIC_FIELD_IMPLEMENT(name##_items, type) \ 83 \ 84 int name##_add(name##_ref map, const char *name, const size_t length, \ 85 type *value) \ 86 { \ 87 ERROR_DECLARE; \ 88 int index; \ 89 if (!name##_is_valid(map)) \ 90 return EINVAL; \ 91 index = name##_items_add(&map->values, value); \ 92 if (index < 0) \ 93 return index; \ 94 if (ERROR_OCCURRED(char_map_add(&map->names, name, length, \ 95 index))) { \ 96 name##_items_exclude_index(&map->values, index); \ 97 return ERROR_CODE; \ 98 } \ 99 return EOK; \ 100 } \ 101 \ 102 int name##_count(name##_ref map) \ 103 { \ 104 return name##_is_valid(map) ? \ 105 name##_items_count(&map->values) : -1; \ 106 } \ 107 \ 108 void name##_destroy(name##_ref map) \ 109 { \ 110 if (name##_is_valid(map)) { \ 111 char_map_destroy(&map->names); \ 112 name##_items_destroy(&map->values); \ 113 } \ 114 } \ 115 \ 116 void name##_exclude(name##_ref map, const char *name, \ 117 const size_t length) \ 118 { \ 119 if (name##_is_valid(map)) { \ 120 int index; \ 121 index = char_map_exclude(&map->names, name, length); \ 122 if (index != CHAR_MAP_NULL) \ 123 name##_items_exclude_index(&map->values, \ 124 index); \ 125 } \ 126 } \ 127 \ 128 type *name##_find(name##_ref map, const char *name, \ 129 const size_t length) \ 130 { \ 131 if (name##_is_valid(map)) { \ 132 int index; \ 133 index = char_map_find(&map->names, name, length); \ 134 if( index != CHAR_MAP_NULL) \ 135 return name##_items_get_index(&map->values, \ 136 index); \ 137 } \ 138 return NULL; \ 139 } \ 140 \ 141 int name##_initialize(name##_ref map) \ 142 { \ 143 ERROR_DECLARE; \ 144 if (!map) \ 145 return EINVAL; \ 146 ERROR_PROPAGATE(char_map_initialize(&map->names)); \ 147 if (ERROR_OCCURRED(name##_items_initialize(&map->values))) { \ 148 char_map_destroy(&map->names); \ 149 return ERROR_CODE; \ 150 } \ 151 map->magic = GENERIC_CHAR_MAP_MAGIC_VALUE; \ 152 return EOK; \ 153 } \ 154 \ 155 int name##_is_valid(name##_ref map) \ 156 { \ 157 return map && (map->magic == GENERIC_CHAR_MAP_MAGIC_VALUE); \ 158 } 156 159 157 160 #endif
Note:
See TracChangeset
for help on using the changeset viewer.