00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00070 #ifndef GWENHYWFAR_MISC_H
00071 #define GWENHYWFAR_MISC_H
00072
00073 #include <gwenhywfar/gwenhywfarapi.h>
00074 #include <gwenhywfar/types.h>
00075 #include <stdio.h>
00076 #include <stdlib.h>
00077 #include <string.h>
00078 #include <assert.h>
00079
00080 #ifndef GWEN_DUMMY_EMPTY_ARG
00081
00083 # define GWEN_DUMMY_EMPTY_ARG
00084 #endif
00085
00086 #ifdef __cplusplus
00087 extern "C" {
00088 #endif
00089
00187 #define GWEN_LIST_ADD(typ, sr, head) {\
00188 typ *curr; \
00189 \
00190 assert(sr); \
00191 assert(head); \
00192 \
00193 curr=*head; \
00194 if (!curr) { \
00195 *head=sr; \
00196 } \
00197 else { \
00198 while(curr->next) { \
00199 curr=curr->next; \
00200 } \
00201 curr->next=sr; \
00202 }\
00203 }
00204
00205
00206 #define GWEN_LIST_INSERT(typ, sr, head) {\
00207 typ *curr; \
00208 \
00209 assert(sr); \
00210 assert(head); \
00211 \
00212 curr=*head; \
00213 if (!curr) { \
00214 *head=sr; \
00215 } \
00216 else { \
00217 sr->next=curr;\
00218 *head=sr;\
00219 }\
00220 }
00221
00222
00223 #define GWEN_LIST_DEL(typ, sr, head) {\
00224 typ *curr; \
00225 \
00226 assert(sr); \
00227 assert(head); \
00228 curr=*head; \
00229 if (curr) { \
00230 if (curr==sr) { \
00231 *head=curr->next; \
00232 } \
00233 else { \
00234 while(curr->next!=sr) { \
00235 curr=curr->next; \
00236 } \
00237 if (curr) \
00238 curr->next=sr->next; \
00239 } \
00240 } \
00241 sr->next=0;\
00242 }
00243
00244
00249 #define GWEN_LIST_ELEMENT(t) \
00250 t *next; \
00251 t##_LIST *listPtr;
00252
00295 #define GWEN_LIST_FUNCTION_LIB_DEFS(t, pr, decl) \
00296 typedef struct t##_LIST_ELEMENT {\
00297 GWEN_TYPE_UINT32 id;\
00298 t *nextObject;\
00299 } t##_LIST__ELEMENT;\
00300 \
00301 typedef struct t##_LIST {\
00302 t *first;\
00303 GWEN_TYPE_UINT32 count;\
00304 GWEN_TYPE_UINT32 id;\
00305 } t##_LIST; \
00306 \
00307 decl void pr##_List_AddList(t##_LIST *dst, t##_LIST *l); \
00308 decl void pr##_List_Add(t *element, t##_LIST *list); \
00309 decl void pr##_List_Insert(t *element, t##_LIST *list); \
00310 decl void pr##_List_Del(t *element); \
00311 decl t* pr##_List_First(const t##_LIST *l); \
00312 decl t* pr##_List_Last(const t##_LIST *l); \
00313 decl void pr##_List_Clear(t##_LIST *l); \
00314 decl t##_LIST* pr##_List_new(); \
00315 decl void pr##_List_free(t##_LIST *l); \
00316 decl t* pr##_List_Next(const t *element); \
00317 decl t* pr##_List_Previous(const t *element); \
00318 decl GWEN_TYPE_UINT32 pr##_List_GetCount(const t##_LIST *l);
00319
00362 #define GWEN_LIST_FUNCTION_DEFS(t, pr) \
00363 GWEN_LIST_FUNCTION_LIB_DEFS(t, pr, GWEN_DUMMY_EMPTY_ARG)
00364
00370 #define GWEN_LIST_FUNCTIONS(t, pr) \
00371 static GWEN_TYPE_UINT32 pr##_List_NextId=0;\
00372 \
00373 void pr##_List_Add(t *element, t##_LIST *l) { \
00374 assert(l); \
00375 assert(element->listPtr==0); \
00376 GWEN_LIST_ADD(t, element, &(l->first)) \
00377 element->listPtr=l;\
00378 l->count++;\
00379 } \
00380 \
00381 void pr##_List_AddList(t##_LIST *dst, t##_LIST *l) { \
00382 t *n; \
00383 \
00384 assert(dst);\
00385 assert(l); \
00386 if (l->first) {\
00387 n=l->first; \
00388 while(n) {\
00389 n->listPtr=dst; \
00390 dst->count++;\
00391 n=n->next; \
00392 } \
00393 GWEN_LIST_ADD(t, l->first, &(dst->first)) \
00394 l->count=0;\
00395 l->first=0;\
00396 } \
00397 } \
00398 \
00399 void pr##_List_Insert(t *element, t##_LIST *l) { \
00400 assert(l); \
00401 assert(element->listPtr==0); \
00402 GWEN_LIST_INSERT(t, element, &(l->first)) \
00403 element->listPtr=l;\
00404 l->count++;\
00405 } \
00406 \
00407 void pr##_List_Del(t *element){ \
00408 assert(element->listPtr);\
00409 assert(element->listPtr->first); \
00410 assert(element->listPtr->count);\
00411 GWEN_LIST_DEL(t, element, &(element->listPtr->first)) \
00412 element->listPtr->count--;\
00413 element->listPtr=0;\
00414 }\
00415 \
00416 t* pr##_List_First(const t##_LIST *l) { \
00417 if (l) return l->first;\
00418 else return 0; \
00419 } \
00420 \
00421 t* pr##_List_Last(const t##_LIST *l) { \
00422 t* el; \
00423 \
00424 assert(l); \
00425 el=l->first; \
00426 if (!el) \
00427 return 0; \
00428 while(el->next)\
00429 el=el->next; \
00430 return el; \
00431 } \
00432 \
00433 void pr##_List_Clear(t##_LIST *l) { \
00434 t* el; \
00435 while(l->first) {\
00436 el=l->first;\
00437 pr##_List_Del(el);\
00438 pr##_free(el);\
00439 } \
00440 } \
00441 \
00442 t##_LIST* pr##_List_new(){\
00443 t##_LIST *l; \
00444 GWEN_NEW_OBJECT(t##_LIST, l);\
00445 l->id=++pr##_List_NextId;\
00446 return l;\
00447 }\
00448 \
00449 void pr##_List_free(t##_LIST *l) {\
00450 if (l) pr##_List_Clear(l);\
00451 free(l);\
00452 } \
00453 \
00454 t* pr##_List_Next(const t *element) { \
00455 assert(element);\
00456 return element->next;\
00457 } \
00458 \
00459 t* pr##_List_Previous(const t *element) { \
00460 t *tmpel; \
00461 \
00462 assert(element);\
00463 assert(element->listPtr);\
00464 assert(element->listPtr->first); \
00465 tmpel=element->listPtr->first; \
00466 while(tmpel->next && tmpel->next!=element) \
00467 tmpel=tmpel->next; \
00468 if (tmpel->next!=element) \
00469 return 0; \
00470 return tmpel; \
00471 } \
00472 \
00473 GWEN_TYPE_UINT32 pr##_List_GetCount(const t##_LIST *l){\
00474 assert(l);\
00475 return l->count;\
00476 }
00477
00478
00479
00485 #define GWEN_LIST_INIT(t, element) element->listPtr=0;
00486
00487
00493 #define GWEN_LIST_FINI(t, element) \
00494 if (element) { \
00495 if (element->listPtr) {\
00496 GWEN_LIST_DEL(t, element, &(element->listPtr->first)) \
00497 element->listPtr->count--;\
00498 element->listPtr=0;\
00499 }\
00500 }
00501
00502
00503 #ifdef __cplusplus
00504 }
00505 #endif
00506
00507
00508 #include <gwenhywfar/memory.h>
00509
00510
00511
00512
00513 #endif
00514
00515
00516