Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/net/structures/generic_char_map.h

    raadf01e r21580dd  
    5454 *  @param[in] type Inner object type.
    5555 */
    56 #define GENERIC_CHAR_MAP_DECLARE(name, type)                                                                    \
     56#define GENERIC_CHAR_MAP_DECLARE( name, type )                                                                  \
    5757                                                                                                                                                                \
    58 GENERIC_FIELD_DECLARE(name##_items, type)                                                                               \
     58GENERIC_FIELD_DECLARE( name##_items, type )                                                                             \
    5959                                                                                                                                                                \
    6060typedef struct name             name##_t;                                                                                               \
     
    6262                                                                                                                                                                \
    6363struct  name{                                                                                                                                   \
    64         char_map_t names;                                                                                                               \
    65         name##_items_t values;                                                                                                          \
    66         int magic;                                                                                                              \
     64        char_map_t              names;                                                                                                          \
     65        name##_items_t  values;                                                                                                         \
     66        int                             magic;                                                                                                          \
    6767};                                                                                                                                                              \
    6868                                                                                                                                                                \
    69 int name##_add(name##_ref map, const char * name, const size_t length, type * value);   \
    70 int name##_count(name##_ref map);                                                                                               \
    71 void name##_destroy(name##_ref map);                                                                                    \
    72 void name##_exclude(name##_ref map, const char * name, const size_t length);    \
    73 type * name##_find(name##_ref map, const char * name, const size_t length);             \
    74 int name##_initialize(name##_ref map);                                                                                  \
    75 int name##_is_valid(name##_ref map);
     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 );
    7676
    7777/** Character string to generic type map implementation.
     
    8080 *  @param[in] type Inner object type.
    8181 */
    82 #define GENERIC_CHAR_MAP_IMPLEMENT(name, type)                                                                  \
     82#define GENERIC_CHAR_MAP_IMPLEMENT( name, type )                                                                \
    8383                                                                                                                                                                \
    84 GENERIC_FIELD_IMPLEMENT(name##_items, type)                                                                             \
     84GENERIC_FIELD_IMPLEMENT( name##_items, type )                                                                   \
    8585                                                                                                                                                                \
    86 int name##_add(name##_ref map, const char * name, const size_t length, type * value){   \
     86int name##_add( name##_ref map, const char * name, const size_t length, type * value ){ \
    8787        ERROR_DECLARE;                                                                                                                          \
    8888                                                                                                                                                                \
    89         int index;                                                                                                                                      \
     89        int     index;                                                                                                                                  \
    9090                                                                                                                                                                \
    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);                                                \
     91        if( ! name##_is_valid( map )) return EINVAL;                                                            \
     92        index = name##_items_add( & map->values, value );                                                       \
     93        if( index < 0 ) return index;                                                                                           \
     94        if( ERROR_OCCURRED( char_map_add( & map->names, name, length, index ))){                \
     95                name##_items_exclude_index( & map->values, index );                                             \
    10096                return ERROR_CODE;                                                                                                              \
    10197        }                                                                                                                                                       \
     
    10399}                                                                                                                                                               \
    104100                                                                                                                                                                \
    105 int name##_count(name##_ref map){                                                                                               \
    106         return name##_is_valid(map) ? name##_items_count(&map->values) : -1;            \
     101int name##_count( name##_ref map ){                                                                                             \
     102        return name##_is_valid( map ) ? name##_items_count( & map->values ) : -1;       \
    107103}                                                                                                                                                               \
    108104                                                                                                                                                                \
    109 void name##_destroy(name##_ref map){                                                                                    \
    110         if(name##_is_valid(map)){                                                                                                       \
    111                 char_map_destroy(&map->names);                                                                                  \
    112                 name##_items_destroy(&map->values);                                                                             \
     105void name##_destroy( name##_ref map ){                                                                                  \
     106        if( name##_is_valid( map )){                                                                                            \
     107                char_map_destroy( & map->names );                                                                               \
     108                name##_items_destroy( & map->values );                                                                  \
    113109        }                                                                                                                                                       \
    114110}                                                                                                                                                               \
    115111                                                                                                                                                                \
    116 void name##_exclude(name##_ref map, const char * name, const size_t length){    \
    117         if(name##_is_valid(map)){                                                                                                       \
    118                 int index;                                                                                                                              \
     112void name##_exclude( name##_ref map, const char * name, const size_t length ){  \
     113        if( name##_is_valid( map )){                                                                                            \
     114                int     index;                                                                                                                          \
    119115                                                                                                                                                                \
    120                 index = char_map_exclude(&map->names, name, length);                                    \
    121                 if(index != CHAR_MAP_NULL){                                                                                             \
    122                         name##_items_exclude_index(&map->values, index);                                        \
     116                index = char_map_exclude( & map->names, name, length );                                 \
     117                if( index != CHAR_MAP_NULL ){                                                                                   \
     118                        name##_items_exclude_index( & map->values, index );                                     \
    123119                }                                                                                                                                               \
    124120        }                                                                                                                                                       \
    125121}                                                                                                                                                               \
    126122                                                                                                                                                                \
    127 type * name##_find(name##_ref map, const char * name, const size_t length){             \
    128         if(name##_is_valid(map)){                                                                                                       \
    129                 int index;                                                                                                                              \
     123type * name##_find( name##_ref map, const char * name, const size_t length ){   \
     124        if( name##_is_valid( map )){                                                                                            \
     125                int     index;                                                                                                                          \
    130126                                                                                                                                                                \
    131                 index = char_map_find(&map->names, name, length);                                               \
    132                 if(index != CHAR_MAP_NULL){                                                                                             \
    133                         return name##_items_get_index(&map->values, index);                                     \
     127                index = char_map_find( & map->names, name, length );                                    \
     128                if( index != CHAR_MAP_NULL ){                                                                                   \
     129                        return name##_items_get_index( & map->values, index );                          \
    134130                }                                                                                                                                               \
    135131        }                                                                                                                                                       \
     
    137133}                                                                                                                                                               \
    138134                                                                                                                                                                \
    139 int name##_initialize(name##_ref map){                                                                                  \
     135int name##_initialize( name##_ref map ){                                                                                \
    140136        ERROR_DECLARE;                                                                                                                          \
    141137                                                                                                                                                                \
    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);                                                                                  \
     138        if( ! map ) return EINVAL;                                                                                                      \
     139        ERROR_PROPAGATE( char_map_initialize( & map->names ));                                          \
     140        if( ERROR_OCCURRED( name##_items_initialize( & map->values ))){                         \
     141                char_map_destroy( & map->names );                                                                               \
    148142                return ERROR_CODE;                                                                                                              \
    149143        }                                                                                                                                                       \
     
    152146}                                                                                                                                                               \
    153147                                                                                                                                                                \
    154 int name##_is_valid(name##_ref map){                                                                                    \
    155         return map && (map->magic == GENERIC_CHAR_MAP_MAGIC_VALUE);                                     \
     148int name##_is_valid( name##_ref map ){                                                                                  \
     149        return map && ( map->magic == GENERIC_CHAR_MAP_MAGIC_VALUE );                           \
    156150}
    157151
Note: See TracChangeset for help on using the changeset viewer.