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