generic_char_map.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2009 Lukas Mejdrech
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00037 #ifndef LIBC_GENERIC_CHAR_MAP_H_
00038 #define LIBC_GENERIC_CHAR_MAP_H_
00039 
00040 #include <unistd.h>
00041 #include <errno.h>
00042 
00043 #include <adt/char_map.h>
00044 #include <adt/generic_field.h>
00045 
00047 #define GENERIC_CHAR_MAP_MAGIC_VALUE    0x12345622
00048 
00050 #define DTOR_T(identifier) \
00051         void (*identifier)(const void *)
00052 
00057 #define GENERIC_CHAR_MAP_DECLARE(name, type) \
00058         GENERIC_FIELD_DECLARE(name##_items, type) \
00059         \
00060         typedef struct name name##_t; \
00061         \
00062         struct  name { \
00063                 char_map_t names; \
00064                 name##_items_t values; \
00065                 int magic; \
00066         }; \
00067         \
00068         int name##_add(name##_t *, const uint8_t *, const size_t, type *); \
00069         int name##_count(name##_t *); \
00070         void name##_destroy(name##_t *, DTOR_T()); \
00071         void name##_exclude(name##_t *, const uint8_t *, const size_t, DTOR_T()); \
00072         type *name##_find(name##_t *, const uint8_t *, const size_t); \
00073         int name##_initialize(name##_t *); \
00074         int name##_is_valid(name##_t *);
00075 
00084 #define GENERIC_CHAR_MAP_IMPLEMENT(name, type) \
00085         GENERIC_FIELD_IMPLEMENT(name##_items, type) \
00086         \
00087         int name##_add(name##_t *map, const uint8_t *name, const size_t length, \
00088              type *value) \
00089         { \
00090                 int index; \
00091                 if (!name##_is_valid(map)) \
00092                         return EINVAL; \
00093                 index = name##_items_add(&map->values, value); \
00094                 if (index < 0) \
00095                         return index; \
00096                 return char_map_add(&map->names, name, length, index); \
00097         } \
00098         \
00099         int name##_count(name##_t *map) \
00100         { \
00101                 return name##_is_valid(map) ? \
00102                     name##_items_count(&map->values) : -1; \
00103         } \
00104         \
00105         void name##_destroy(name##_t *map, DTOR_T(dtor)) \
00106         { \
00107                 if (name##_is_valid(map)) { \
00108                         char_map_destroy(&map->names); \
00109                         name##_items_destroy(&map->values, dtor); \
00110                 } \
00111         } \
00112         \
00113         void name##_exclude(name##_t *map, const uint8_t *name, \
00114             const size_t length, DTOR_T(dtor)) \
00115         { \
00116                 if (name##_is_valid(map)) { \
00117                         int index; \
00118                         index = char_map_exclude(&map->names, name, length); \
00119                         if (index != CHAR_MAP_NULL) \
00120                                 name##_items_exclude_index(&map->values, \
00121                                      index, dtor); \
00122                 } \
00123         } \
00124         \
00125         type *name##_find(name##_t *map, const uint8_t *name, \
00126             const size_t length) \
00127         { \
00128                 if (name##_is_valid(map)) { \
00129                         int index; \
00130                         index = char_map_find(&map->names, name, length); \
00131                         if( index != CHAR_MAP_NULL) \
00132                                 return name##_items_get_index(&map->values, \
00133                                     index); \
00134                 } \
00135                 return NULL; \
00136         } \
00137         \
00138         int name##_initialize(name##_t *map) \
00139         { \
00140                 int rc; \
00141                 if (!map) \
00142                         return EINVAL; \
00143                 rc = char_map_initialize(&map->names); \
00144                 if (rc != EOK) \
00145                         return rc; \
00146                 rc = name##_items_initialize(&map->values); \
00147                 if (rc != EOK) { \
00148                         char_map_destroy(&map->names); \
00149                         return rc; \
00150                 } \
00151                 map->magic = GENERIC_CHAR_MAP_MAGIC_VALUE; \
00152                 return EOK; \
00153         } \
00154         \
00155         int name##_is_valid(name##_t *map) \
00156         { \
00157                 return map && (map->magic == GENERIC_CHAR_MAP_MAGIC_VALUE); \
00158         }
00159 
00160 #endif
00161 

Generated on Thu Jun 2 07:45:46 2011 for HelenOS/USB by  doxygen 1.4.7