Ignore:
Timestamp:
2011-05-12T16:49:44Z (14 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
f36787d7
Parents:
e80329d6 (diff), 750636a (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge mainline changes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/include/adt/generic_field.h

    re80329d6 rb5e68c8  
    4646#define GENERIC_FIELD_MAGIC_VALUE               0x55667788
    4747
     48/** Generic destructor function pointer. */
     49#define DTOR_T(identifier) \
     50        void (*identifier)(const void *)
     51
    4852/** Generic type field declaration.
    4953 *
     
    5357#define GENERIC_FIELD_DECLARE(name, type) \
    5458        typedef struct name name##_t; \
    55         typedef name##_t *name##_ref; \
    5659        \
    5760        struct  name { \
     
    6265        }; \
    6366        \
    64         int name##_add(name##_ref, type *); \
    65         int name##_count(name##_ref); \
    66         void name##_destroy(name##_ref); \
    67         void name##_exclude_index(name##_ref, int); \
    68         type **name##_get_field(name##_ref); \
    69         type *name##_get_index(name##_ref, int); \
    70         int name##_initialize(name##_ref); \
    71         int name##_is_valid(name##_ref);
     67        int name##_add(name##_t *, type *); \
     68        int name##_count(name##_t *); \
     69        void name##_destroy(name##_t *, DTOR_T()); \
     70        void name##_exclude_index(name##_t *, int, DTOR_T()); \
     71        type **name##_get_field(name##_t *); \
     72        type *name##_get_index(name##_t *, int); \
     73        int name##_initialize(name##_t *); \
     74        int name##_is_valid(name##_t *);
    7275
    7376/** Generic type field implementation.
     
    7982 */
    8083#define GENERIC_FIELD_IMPLEMENT(name, type) \
    81         int name##_add(name##_ref field, type *value) \
     84        int name##_add(name##_t *field, type *value) \
    8285        { \
    8386                if (name##_is_valid(field)) { \
     
    9295                        } \
    9396                        field->items[field->next] = value; \
    94                         ++field->next; \
     97                        field->next++; \
    9598                        field->items[field->next] = NULL; \
    9699                        return field->next - 1; \
     
    99102        } \
    100103        \
    101         int name##_count(name##_ref field) \
     104        int name##_count(name##_t *field) \
    102105        { \
    103106                return name##_is_valid(field) ? field->next : -1; \
    104107        } \
    105108        \
    106         void name##_destroy(name##_ref field) \
     109        void name##_destroy(name##_t *field, DTOR_T(dtor)) \
    107110        { \
    108111                if (name##_is_valid(field)) { \
    109112                        int index; \
    110113                        field->magic = 0; \
    111                         for (index = 0; index < field->next; ++ index) { \
    112                                 if (field->items[index]) \
    113                                         free(field->items[index]); \
     114                        if (dtor) { \
     115                                for (index = 0; index < field->next; index++) { \
     116                                        if (field->items[index]) \
     117                                                dtor(field->items[index]); \
     118                                } \
    114119                        } \
    115120                        free(field->items); \
     
    117122        } \
    118123         \
    119         void name##_exclude_index(name##_ref field, int index) \
     124        void name##_exclude_index(name##_t *field, int index, DTOR_T(dtor)) \
    120125        { \
    121126                if (name##_is_valid(field) && (index >= 0) && \
    122127                    (index < field->next) && (field->items[index])) { \
    123                         free(field->items[index]); \
     128                        if (dtor) \
     129                                dtor(field->items[index]); \
    124130                        field->items[index] = NULL; \
    125131                } \
    126132        } \
    127133         \
    128         type *name##_get_index(name##_ref field, int index) \
     134        type *name##_get_index(name##_t *field, int index) \
    129135        { \
    130136                if (name##_is_valid(field) && (index >= 0) && \
     
    134140        } \
    135141        \
    136         type **name##_get_field(name##_ref field) \
     142        type **name##_get_field(name##_t *field) \
    137143        { \
    138144                return name##_is_valid(field) ? field->items : NULL; \
    139145        } \
    140146        \
    141         int name##_initialize(name##_ref field) \
     147        int name##_initialize(name##_t *field) \
    142148        { \
    143149                if (!field) \
     
    153159        } \
    154160        \
    155         int name##_is_valid(name##_ref field) \
     161        int name##_is_valid(name##_t *field) \
    156162        { \
    157163                return field && (field->magic == GENERIC_FIELD_MAGIC_VALUE); \
Note: See TracChangeset for help on using the changeset viewer.