Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

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 
00029 typedef struct vlist_s{
00030         struct vlist_node_s * head;
00031         struct vlist_node_s * tail;
00032         struct vlist_node_s * cursor;
00033         unsigned int cursor_index;
00034         unsigned int count;
00035         void *(VLIST_CDECL *malloc) (size_t);
00036         void (VLIST_CDECL *free) (void*);
00037 } vlist_t;
00038 
00039 
00046 vlist_t* new_vlist(
00047         void *  (VLIST_CDECL *  vlist_malloc) (size_t),
00048         void (VLIST_CDECL *vlist_free) (void *));
00049 
00054 void delete_vlist( 
00055         vlist_t* vlist); 
00056 
00062 void vlist_add_first( 
00063         vlist_t* vlist, 
00064         void* item);
00065 
00071 void vlist_add_last( 
00072         vlist_t* vlist, 
00073         void* item);
00074 
00080 void vlist_add_after_cursor(
00081         vlist_t* vlist,
00082         void *item);
00083 
00090 int vlist_remove(
00091         vlist_t* vlist, 
00092         void* item);
00093 
00099 void * vlist_remove_first(
00100         vlist_t* vlist);
00101 
00107 void * vlist_remove_cursor_next(
00108         vlist_t* vlist);
00109 
00114 void vlist_reset_cursor(
00115         vlist_t* vlist);
00116 
00122 void * vlist_peek_cursor(
00123         vlist_t* vlist);
00124 
00130 void * vlist_peek_cursor_next(
00131         vlist_t* vlist);
00132 
00138 void * vlist_peek_cursor_next_next(
00139         vlist_t* vlist);
00140 
00145 void vlist_advance_cursor(
00146         vlist_t* vlist);
00147 
00153 void * vlist_peek_first(
00154         vlist_t* vlist);
00155 
00161 void * vlist_peek_last(
00162         vlist_t* vlist);
00163 
00169 unsigned long vlist_count(
00170         vlist_t* vlist);
00171 
00178 int vlist_item_exists(
00179         vlist_t* vlist, 
00180         void* item);
00181 
00187 void vlist_items_to_array(
00188         vlist_t* vlist, 
00189         void** items);
00190 
00197 void vlist_map_function(
00198         vlist_t* vlist, 
00199         void(*function)(void*, void*),
00200         void * user_data);
00201 
00208 void* vlist_nth_item(
00209         vlist_t* vlist,
00210         unsigned long index);
00211 
00216 void vlist_reverse(
00217         vlist_t* vlist);
00218 
00223 void vlist_flush(
00224         vlist_t* vlist);
00225 
00226 #ifdef __cplusplus
00227 } /* extern "C" */
00228 #endif /* __cplusplus */
00229 
00230 #ifndef V_POINTER_SIZED_INT
00231 #ifdef _M_IA64
00232 #define  V_POINTER_SIZED_INT    unsigned __int64
00233 #else
00234 #define  V_POINTER_SIZED_INT    unsigned long
00235 #endif
00236 #endif
00237 
00238 /* template wrapper */
00239 #ifdef __cplusplus
00240 extern "C++" {
00241 
00242 /*the following macro generates a compile-time errors
00243   if anyone tries to instantiate a VList with incompatible types */
00244 #define VLIST_COMPILE_TIME_ASSERT(expr) { char unnamed[(expr)?1:0]; (void)unnamed; }
00245 
00246 template <class T> class VList {
00247 public:
00248 
00249         VList() { m_vlist = new_vlist(malloc,free); size_assert(); };
00250         
00251         VList(void * (VLIST_CDECL *  vlist_malloc) (size_t), void (VLIST_CDECL *vlist_free) (void *)) {m_vlist = new_vlist( vlist_malloc,vlist_free); size_assert(); };
00252 
00253         ~VList() {delete_vlist(m_vlist);};
00254 
00255         void AddFirst(T item) { vlist_add_first(m_vlist, (void *)item); };
00256 
00257         void AddLast(T item) { vlist_add_last(m_vlist, (void *)item); };
00258 
00259         void AddAfterCursor(T item) { vlist_add_after_cursor(m_vlist, (void *)item); };
00260 
00261         int Remove(T item) { return (vlist_remove(m_vlist, (void *)item)); };
00262 
00263         T RemoveFirst() { return ((T)(V_POINTER_SIZED_INT)vlist_remove_first(m_vlist)); };
00264 
00265         void ResetCursor() { vlist_reset_cursor(m_vlist); };
00266 
00267         T PeekCursor() { return ((T)(V_POINTER_SIZED_INT)vlist_peek_cursor(m_vlist)); };
00268 
00269         T PeekCursorNext() { return ((T)(V_POINTER_SIZED_INT)vlist_peek_cursor_next(m_vlist)); };
00270 
00271         T PeekCursorNextNext() { return ((T)(V_POINTER_SIZED_INT)vlist_peek_cursor_next_next(m_vlist)); };
00272 
00273         void AdvanceCursor() { vlist_advance_cursor(m_vlist); };
00274 
00275         T PeekFirst() { return ((T)(V_POINTER_SIZED_INT)vlist_peek_first(m_vlist)); };
00276 
00277         T PeekLast() { return ((T)(V_POINTER_SIZED_INT)vlist_peek_last(m_vlist)); };
00278 
00279         unsigned long Count() { return (vlist_count(m_vlist)); };
00280 
00281         int ItemExists(T item) { return (vlist_item_exists(m_vlist, (void *)item)); };
00282 
00283         void ItemsToArray(T* items) { vlist_items_to_array(m_vlist, (void **)items); };
00284 
00285         void MapFunction(void(*function)(T, void*), void * user_data) {
00286                 unsigned long i = Count();
00287                 ResetCursor();
00288                 while(i){
00289                         (*function)(PeekCursor(),user_data);
00290                         AdvanceCursor();
00291                         i--;
00292                 }
00293         };
00294 
00295         T NthItem(unsigned long index) { return ((T)(V_POINTER_SIZED_INT)vlist_nth_item(m_vlist, index)); };
00296         
00297         void Reverse() { vlist_reverse(m_vlist); };
00298         
00299         void Flush() { vlist_flush(m_vlist); };
00300 
00301 private:
00302         struct vlist_s *m_vlist;
00303 
00304         /* VList cannot hold any type that cannot be converted to and from a (void *) */
00305         void size_assert() { VLIST_COMPILE_TIME_ASSERT(sizeof(T) <= sizeof(void *)); };
00306 
00307 };
00308 } /* extern "C++" */
00309 #endif
00310 
00311 #endif /*VLIST_H*/
00312 
00313 
00314 

Generated on Tue May 17 12:06:00 2005 for Autodesk DWF 3D Toolkit by  doxygen 1.4.1