Changeset b5e68c8 in mainline for uspace/lib/c/include/adt/generic_field.h
- Timestamp:
- 2011-05-12T16:49:44Z (14 years ago)
- 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. - File:
-
- 1 edited
-
uspace/lib/c/include/adt/generic_field.h (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/include/adt/generic_field.h
re80329d6 rb5e68c8 46 46 #define GENERIC_FIELD_MAGIC_VALUE 0x55667788 47 47 48 /** Generic destructor function pointer. */ 49 #define DTOR_T(identifier) \ 50 void (*identifier)(const void *) 51 48 52 /** Generic type field declaration. 49 53 * … … 53 57 #define GENERIC_FIELD_DECLARE(name, type) \ 54 58 typedef struct name name##_t; \ 55 typedef name##_t *name##_ref; \56 59 \ 57 60 struct name { \ … … 62 65 }; \ 63 66 \ 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 *); 72 75 73 76 /** Generic type field implementation. … … 79 82 */ 80 83 #define GENERIC_FIELD_IMPLEMENT(name, type) \ 81 int name##_add(name##_ reffield, type *value) \84 int name##_add(name##_t *field, type *value) \ 82 85 { \ 83 86 if (name##_is_valid(field)) { \ … … 92 95 } \ 93 96 field->items[field->next] = value; \ 94 ++field->next; \97 field->next++; \ 95 98 field->items[field->next] = NULL; \ 96 99 return field->next - 1; \ … … 99 102 } \ 100 103 \ 101 int name##_count(name##_ reffield) \104 int name##_count(name##_t *field) \ 102 105 { \ 103 106 return name##_is_valid(field) ? field->next : -1; \ 104 107 } \ 105 108 \ 106 void name##_destroy(name##_ ref field) \109 void name##_destroy(name##_t *field, DTOR_T(dtor)) \ 107 110 { \ 108 111 if (name##_is_valid(field)) { \ 109 112 int index; \ 110 113 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 } \ 114 119 } \ 115 120 free(field->items); \ … … 117 122 } \ 118 123 \ 119 void name##_exclude_index(name##_ ref field, int index) \124 void name##_exclude_index(name##_t *field, int index, DTOR_T(dtor)) \ 120 125 { \ 121 126 if (name##_is_valid(field) && (index >= 0) && \ 122 127 (index < field->next) && (field->items[index])) { \ 123 free(field->items[index]); \ 128 if (dtor) \ 129 dtor(field->items[index]); \ 124 130 field->items[index] = NULL; \ 125 131 } \ 126 132 } \ 127 133 \ 128 type *name##_get_index(name##_ reffield, int index) \134 type *name##_get_index(name##_t *field, int index) \ 129 135 { \ 130 136 if (name##_is_valid(field) && (index >= 0) && \ … … 134 140 } \ 135 141 \ 136 type **name##_get_field(name##_ reffield) \142 type **name##_get_field(name##_t *field) \ 137 143 { \ 138 144 return name##_is_valid(field) ? field->items : NULL; \ 139 145 } \ 140 146 \ 141 int name##_initialize(name##_ reffield) \147 int name##_initialize(name##_t *field) \ 142 148 { \ 143 149 if (!field) \ … … 153 159 } \ 154 160 \ 155 int name##_is_valid(name##_ reffield) \161 int name##_is_valid(name##_t *field) \ 156 162 { \ 157 163 return field && (field->magic == GENERIC_FIELD_MAGIC_VALUE); \
Note:
See TracChangeset
for help on using the changeset viewer.
