Rev 3886 | Rev 4192 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3666 | mejdrech | 1 | /* |
2 | * Copyright (c) 2008 Lukas Mejdrech |
||
3 | * All rights reserved. |
||
4 | * |
||
5 | * Redistribution and use in source and binary forms, with or without |
||
6 | * modification, are permitted provided that the following conditions |
||
7 | * are met: |
||
8 | * |
||
9 | * - Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * - Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
13 | * documentation and/or other materials provided with the distribution. |
||
14 | * - The name of the author may not be used to endorse or promote products |
||
15 | * derived from this software without specific prior written permission. |
||
16 | * |
||
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
||
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
||
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
27 | */ |
||
28 | |||
29 | /** @addtogroup net |
||
30 | * @{ |
||
31 | */ |
||
32 | |||
33 | /** |
||
34 | * @file |
||
35 | */ |
||
36 | |||
37 | #ifndef __GENERIC_FIELD_H__ |
||
38 | #define __GENERIC_FIELD_H__ |
||
39 | |||
40 | #include <errno.h> |
||
41 | #include <malloc.h> |
||
42 | #include <string.h> |
||
43 | #include <unistd.h> |
||
44 | |||
45 | #define GENERIC_FIELD_MAGIC_VALUE 0x55667788 |
||
46 | |||
47 | #define GENERIC_FIELD_DECLARE( name, type ) \ |
||
48 | \ |
||
49 | typedef struct name name##_t; \ |
||
50 | typedef name##_t * name##_ref; \ |
||
51 | \ |
||
52 | struct name{ \ |
||
53 | int size; \ |
||
54 | int next; \ |
||
55 | type ** items; \ |
||
56 | int magic; \ |
||
57 | }; \ |
||
58 | \ |
||
59 | int name##_add( name##_ref field, type * value ); \ |
||
60 | int name##_count( name##_ref field ); \ |
||
61 | void name##_destroy( name##_ref field ); \ |
||
62 | void name##_exclude_index( name##_ref field, int index ); \ |
||
63 | type ** name##_get_field( name##_ref field ); \ |
||
64 | type * name##_get_index( name##_ref field, int index ); \ |
||
65 | int name##_initialize( name##_ref field ); \ |
||
66 | int name##_is_valid( name##_ref field ); |
||
67 | |||
68 | #define GENERIC_FIELD_IMPLEMENT( name, type ) \ |
||
69 | \ |
||
70 | int name##_add( name##_ref field, type * value ){ \ |
||
71 | if( name##_is_valid( field )){ \ |
||
72 | if( field->next == ( field->size - 1 )){ \ |
||
73 | type ** tmp; \ |
||
74 | \ |
||
75 | tmp = ( type ** ) malloc( sizeof( type * ) * 2 * field->size ); \ |
||
76 | if( ! tmp ) return ENOMEM; \ |
||
77 | field->size *= 2; \ |
||
78 | memcpy( tmp, field->items, sizeof( type * ) * field->next ); \ |
||
79 | free( field->items ); \ |
||
80 | field->items = tmp; \ |
||
81 | } \ |
||
82 | field->items[ field->next ] = value; \ |
||
83 | ++ field->next; \ |
||
84 | field->items[ field->next ] = NULL; \ |
||
85 | return field->next - 1; \ |
||
86 | } \ |
||
87 | return EINVAL; \ |
||
88 | } \ |
||
89 | \ |
||
90 | int name##_count( name##_ref field ){ \ |
||
91 | return name##_is_valid( field ) ? field->next : -1; \ |
||
92 | } \ |
||
93 | \ |
||
94 | void name##_destroy( name##_ref field ){ \ |
||
95 | if( name##_is_valid( field )){ \ |
||
96 | int index; \ |
||
97 | \ |
||
98 | field->magic = 0; \ |
||
99 | for( index = 0; index < field->next; ++ index ){ \ |
||
100 | free( field->items[ index ] ); \ |
||
101 | } \ |
||
102 | free( field->items ); \ |
||
103 | } \ |
||
104 | } \ |
||
105 | \ |
||
106 | void name##_exclude_index( name##_ref field, int index ){ \ |
||
107 | if( name##_is_valid( field ) && ( index >= 0 ) && ( index < field->next ) && ( field->items[ index ] )){ \ |
||
108 | free( field->items[ index ] ); \ |
||
109 | field->items[ index ] = NULL; \ |
||
110 | } \ |
||
111 | } \ |
||
112 | \ |
||
113 | type * name##_get_index( name##_ref field, int index ){ \ |
||
114 | if( name##_is_valid( field ) && ( index >= 0 ) && ( index < field->next ) && ( field->items[ index ] )){ \ |
||
115 | return field->items[ index ]; \ |
||
116 | } \ |
||
117 | return NULL; \ |
||
118 | } \ |
||
119 | \ |
||
120 | type ** name##_get_field( name##_ref field ){ \ |
||
121 | return name##_is_valid( field ) ? field->items : NULL; \ |
||
122 | } \ |
||
123 | \ |
||
124 | int name##_initialize( name##_ref field ){ \ |
||
125 | if( ! field ) return EINVAL; \ |
||
126 | field->size = 2; \ |
||
127 | field->next = 0; \ |
||
128 | field->items = ( type ** ) malloc( sizeof( type * ) * field->size ); \ |
||
129 | if( ! field->items ) return ENOMEM; \ |
||
130 | field->items[ field->next ] = NULL; \ |
||
3901 | mejdrech | 131 | field->magic = GENERIC_FIELD_MAGIC_VALUE; \ |
3666 | mejdrech | 132 | return EOK; \ |
133 | } \ |
||
134 | \ |
||
135 | int name##_is_valid( name##_ref field ){ \ |
||
3901 | mejdrech | 136 | return field && ( field->magic == GENERIC_FIELD_MAGIC_VALUE ); \ |
3666 | mejdrech | 137 | } |
138 | |||
139 | #endif |
||
140 | |||
141 | /** @} |
||
142 | */ |
||
143 |