vlist.h

Go to the documentation of this file.
00001 
00008 #ifndef VLIST_H
00009 #define VLIST_H
00010 
00011 
00012 #include <stdlib.h>
00013  
00014 #ifdef _WIN32_WCE
00015 #define VLIST_CDECL __cdecl
00016 #else
00017 #define VLIST_CDECL 
00018 #endif
00019  
00020 #ifdef __cplusplus
00021 extern "C" {
00022 #endif /* __cplusplus */
00023 
00024 #ifndef DWFTK_STATIC
00025 
00026 #ifdef  _DWFCORE_WIN32_SYSTEM
00027 #ifndef DWFTK_EXPORT_API
00028 #define _W3DTK_API __declspec(dllimport)
00029 #else
00030 #define _W3DTK_API __declspec(dllexport)
00031 #endif
00032 #else
00033 #define _W3DTK_API
00034 #endif
00035 
00036 #else
00037 #define _W3DTK_API
00038 #endif
00039 
00044 typedef struct vlist_node_s{
00045         void * item;
00046         struct vlist_node_s * next;
00047 } vlist_node_t;
00048 
00049 typedef struct vlist_s{
00050         struct vlist_node_s * head;
00051         struct vlist_node_s * tail;
00052         struct vlist_node_s * cursor;
00053         struct vlist_node_s * cursor_backlink;
00054         unsigned int cursor_index;
00055         unsigned int count;
00056         void *(VLIST_CDECL *malloc) (size_t);
00057         void (VLIST_CDECL *free) (void*);
00058 } vlist_t;
00059 
00060 
00067 _W3DTK_API vlist_t* new_vlist(
00068         void *  (VLIST_CDECL *  vlist_malloc) (size_t),
00069         void (VLIST_CDECL *vlist_free) (void *));
00070 
00075 _W3DTK_API void delete_vlist( 
00076         vlist_t* vlist); 
00077 
00083 _W3DTK_API void vlist_add_first( 
00084         vlist_t* vlist, 
00085         void* item);
00086 
00092 _W3DTK_API void vlist_add_last( 
00093         vlist_t* vlist, 
00094         void* item);
00095 
00096 
00102 _W3DTK_API void vlist_add_before_cursor( 
00103         vlist_t* vlist, 
00104         void* item);
00105 
00111 _W3DTK_API void vlist_add_after_cursor(
00112         vlist_t* vlist,
00113         void *item);
00114 
00121 _W3DTK_API int vlist_remove(
00122         vlist_t* vlist, 
00123         void* item);
00124 
00130 _W3DTK_API void * vlist_remove_first(
00131         vlist_t* vlist);
00132 
00138 _W3DTK_API void * vlist_remove_cursor_next(
00139         vlist_t* vlist);
00140 
00146 _W3DTK_API void * vlist_remove_at_cursor(
00147         vlist_t* vlist);
00148 
00153 _W3DTK_API void vlist_reset_cursor(
00154         vlist_t* vlist);
00155 
00161 _W3DTK_API void * vlist_peek_cursor(
00162         vlist_t* vlist);
00163 
00169 _W3DTK_API void * vlist_peek_cursor_next(
00170         vlist_t* vlist);
00171 
00177 _W3DTK_API void * vlist_peek_cursor_next_next(
00178         vlist_t* vlist);
00179 
00184 _W3DTK_API void vlist_advance_cursor(
00185         vlist_t* vlist);
00186 
00192 _W3DTK_API void * vlist_peek_first(
00193         vlist_t* vlist);
00194 
00200 _W3DTK_API void * vlist_peek_last(
00201         vlist_t* vlist);
00202 
00208 _W3DTK_API unsigned long vlist_count(
00209         vlist_t* vlist);
00210 
00217 _W3DTK_API int vlist_item_exists(
00218         vlist_t* vlist, 
00219         void* item);
00220 
00226 _W3DTK_API void vlist_items_to_array(
00227         vlist_t* vlist, 
00228         void** items);
00229 
00236 _W3DTK_API void vlist_map_function(
00237         vlist_t* vlist, 
00238         void(*function)(void*, void*),
00239         void * user_data);
00240 
00247 _W3DTK_API void* vlist_nth_item(
00248         vlist_t* vlist,
00249         unsigned long index);
00250 
00255 _W3DTK_API void vlist_reverse(
00256         vlist_t* vlist);
00257 
00262 _W3DTK_API void vlist_flush(
00263         vlist_t* vlist);
00264 
00265 #ifdef __cplusplus
00266 } /* extern "C" */
00267 #endif /* __cplusplus */
00268 
00269 #ifndef V_POINTER_SIZED_INT
00270 #ifdef _WIN64
00271 #define  V_POINTER_SIZED_INT    unsigned __int64
00272 #else
00273 #define  V_POINTER_SIZED_INT    unsigned long
00274 #endif
00275 #endif
00276 
00277 /* these are used for the internal functions */
00278 #define VLIST_RESET_CURSOR(XL) \
00279         (XL)->cursor = (XL)->head; \
00280         (XL)->cursor_index=0; \
00281         (XL)->cursor_backlink=0;
00282 
00283 #define VLIST_PEEK_CURSOR(XL) \
00284     ((!(XL)->cursor)?0:(XL)->cursor->item)
00285 
00286 #define VLIST_ADVANCE_CURSOR(XL) \
00287     if((XL)->cursor) {\
00288                 (XL)->cursor_backlink=(XL)->cursor; \
00289                 (XL)->cursor = (XL)->cursor->next; \
00290                 (XL)->cursor_index++; \
00291     }
00292 
00293 #define LIST_REMOVE_AND_CONTINUE_ITERATION(XL) \
00294         vlist_remove_at_cursor((XL)); \
00295         continue;
00296 
00297 #define START_LIST_ITERATION(TYPE, LIST) VLIST_RESET_CURSOR(LIST);\
00298                                 while (1) {\
00299                                 TYPE *temp;\
00300                                 if ((temp = (TYPE *)VLIST_PEEK_CURSOR(LIST)) == NULL)\
00301                                         break;
00302 
00303 #define END_LIST_ITERATION(LIST)  VLIST_ADVANCE_CURSOR(LIST); }
00304 
00305 
00306 /* template wrapper */
00307 #ifdef __cplusplus
00308 extern "C++" {
00309 
00310 /*the following macro generates a compile-time errors
00311   if anyone tries to instantiate a VList with incompatible types */
00312 #define VLIST_COMPILE_TIME_ASSERT(expr) { char unnamed[(expr)?1:0]; (void)unnamed; }
00313 
00314 template <class T> class VList {
00315 public:
00316 
00317         VList(struct vlist_s * vl) { m_vlist = vl; m_free_items=false; size_assert(); };
00318 
00319         VList() { m_vlist = new_vlist(malloc,free); m_free_items=false; size_assert(); };
00320         
00321         VList(void * (VLIST_CDECL *  vlist_malloc) (size_t), void (VLIST_CDECL *vlist_free) (void *)) 
00322                 {m_vlist = new_vlist( vlist_malloc,vlist_free); m_free_items=false; size_assert(); };
00323 
00324         ~VList() {
00325                 if(m_free_items) {
00326                         T tmp;
00327                         while((tmp=RemoveFirst()))
00328                                 m_vlist->free((void*)(V_POINTER_SIZED_INT)tmp);
00329                 }
00330                 delete_vlist(m_vlist);
00331         };
00332 
00333         void AddFirst(T item) { vlist_add_first(m_vlist, (void *)(V_POINTER_SIZED_INT)item); };
00334 
00335         void AddLast(T item) { vlist_add_last(m_vlist, (void *)(V_POINTER_SIZED_INT)item); };
00336 
00337         void AddAfterCursor(T item) { vlist_add_after_cursor(m_vlist, (void *)(V_POINTER_SIZED_INT)item); };
00338 
00339         int Remove(T item) { return (vlist_remove(m_vlist, (void *)(V_POINTER_SIZED_INT)item)); };
00340 
00341         T RemoveFirst() { return ((T)(V_POINTER_SIZED_INT)vlist_remove_first(m_vlist)); };
00342 
00343         void ResetCursor() { vlist_reset_cursor(m_vlist); };
00344 
00345         T PeekCursor() { return ((T)(V_POINTER_SIZED_INT)vlist_peek_cursor(m_vlist)); };
00346 
00347         T PeekCursorNext() { return ((T)(V_POINTER_SIZED_INT)vlist_peek_cursor_next(m_vlist)); };
00348 
00349         T PeekCursorNextNext() { return ((T)(V_POINTER_SIZED_INT)vlist_peek_cursor_next_next(m_vlist)); };
00350 
00351         void AdvanceCursor() { vlist_advance_cursor(m_vlist); };
00352 
00353         T PeekFirst() { return ((T)(V_POINTER_SIZED_INT)vlist_peek_first(m_vlist)); };
00354 
00355         T PeekLast() { return ((T)(V_POINTER_SIZED_INT)vlist_peek_last(m_vlist)); };
00356 
00357         unsigned long Count() { return (vlist_count(m_vlist)); };
00358 
00359         int ItemExists(T item) { return (vlist_item_exists(m_vlist, (void *)(V_POINTER_SIZED_INT)item)); };
00360 
00361         void ItemsToArray(T* items) {
00362                 if (sizeof(T) != sizeof(V_POINTER_SIZED_INT)) {
00363                         V_POINTER_SIZED_INT *tmp=new V_POINTER_SIZED_INT[m_vlist->count];
00364                         vlist_items_to_array(m_vlist, (void **)tmp);
00365                         for(unsigned int i=0; i<m_vlist->count; i++)
00366                                 items[i] = (T)tmp[i];
00367                         delete[] tmp;
00368                 }
00369                 else
00370                         vlist_items_to_array(m_vlist, (void **)items);
00371         };
00372 
00373         void MapFunction(void(*function)(T, void*), void const * const user_data) {
00374                 unsigned long i = Count();
00375                 ResetCursor();
00376                 while(i){
00377                         (*function)(PeekCursor(),(void*)user_data);
00378                         AdvanceCursor();
00379                         i--;
00380                 }
00381         };
00382 
00383         T NthItem(unsigned long index) { return ((T)(V_POINTER_SIZED_INT)vlist_nth_item(m_vlist, index)); };
00384         
00385         void Reverse() { vlist_reverse(m_vlist); };
00386         
00387         void Flush() { vlist_flush(m_vlist); };
00388 
00389         void SetFreeItemsOnDelete() {m_free_items = true;};
00390 
00391 private:
00392         struct vlist_s *m_vlist;
00393         bool m_free_items;
00394 
00395         /* VList cannot hold any type that cannot be converted to and from a (void *) */
00396         void size_assert() { VLIST_COMPILE_TIME_ASSERT(sizeof(T) <= sizeof(void *)); };
00397 
00398 };
00399 } /* extern "C++" */
00400 #endif
00401 
00402 #endif /*VLIST_H*/
00403 
00404 
00405 

Generated on Tue Jan 6 22:41:37 2009 for Autodesk DWF 3D Toolkit by  doxygen 1.4.5