common.h

Go to the documentation of this file.
00001 #ifndef __OE_SERVICES_COMMON_H__
00002 #define __OE_SERVICES_COMMON_H__
00003 
00008 #include "services/keybcodes.h"
00009 
00010 #include <stdlib.h>
00011 #include <string.h>
00012 #include <stdio.h>      /* sprintf() and friends */
00013 
00015 template<typename _UserType>
00016 class OE_LIST
00017 {
00019     struct OE_LIST_ENTRY {
00020         _UserType        data;  
00021         OE_LIST_ENTRY   *prev;  
00022         OE_LIST_ENTRY   *next;  
00024         OE_LIST_ENTRY(const _UserType &_data, OE_LIST_ENTRY *_prev, OE_LIST_ENTRY *_next)
00025         :data(_data), prev(_prev), next(_next)
00026         {
00027         }
00028     };
00029 
00036     OE_LIST_ENTRY *insert_between(OE_LIST_ENTRY *_prev, OE_LIST_ENTRY *_next, const _UserType &_data)
00037     {
00038         // allocate new entry, and initinalize its "data", "prev" and "next" fields
00039         OE_LIST_ENTRY *new_entry = new OE_LIST_ENTRY(_data, _prev, _next);
00040 
00041         if (_prev == NULL)
00042             m_first = new_entry;    // adjust for inserting as very first element, or at the beginning of the list
00043         else
00044             _prev->next = new_entry;// normal insertion in the middle of the list
00045 
00046         if (next == NULL)
00047             m_last = new_entry;     // adjust for inserting as very first element, or at the end of the list
00048         else
00049             next->_prev = new_entry;// normal insertion in the middle of the list
00050     }
00051 
00057     void remove_entry(OE_LIST_ENTRY *entry)
00058     {
00059         OE_ASSERT(entry);
00060 
00061         if (entry->_prev == NULL)
00062             m_first = entry->_next; // adjust for removing very first entry in the list
00063         else
00064             entry->_prev->_next = entry->_next; // normal deletion from the middle of the list
00065 
00066         if (entry->_next == NULL)
00067             m_last = entry->_prev;  // adjust for removing very last entry in the list
00068         else
00069             entry->_next->_prev = entry->_prev; // normal deletion from the middle of the list
00070 
00071         // deallocate the entry we just disconnected from the list
00072         delete entry;
00073     }
00074 
00075 public:
00079     class iterator {
00080         OE_LIST_ENTRY   *p_entry;   
00087         iterator(OE_LIST_ENTRY *naked_entry)
00088         :p_entry(naked_entry)
00089         {
00090         }
00091 
00092     public:
00096         iterator()
00097         :p_entry(NULL)
00098         {
00099         }
00100 
00104         void move_next()
00105         {
00106             OE_ASSERT(p_entry != NULL);
00107             p_entry = p_entry->next;
00108         }
00109 
00113         void move_prev()
00114         {
00115             OE_ASSERT(p_entry != NULL);
00116             p_entry = p_entry->prev;
00117         }
00118 
00120         bool is_valid()
00121         {
00122             return p_entry != NULL;
00123         }
00124 
00128         iterator get_next()
00129         {
00130             OE_ASSERT(p_entry != NULL);
00131             return iterator(p_entry->next); // construct iterator from raw pointer
00132         }
00133 
00137         iterator get_prev()
00138         {
00139             OE_ASSERT(p_entry != NULL);
00140             return iterator(p_entry->prev);     // construct iterator from raw pointer
00141         }
00142 
00148         bool is_first()
00149         {
00150             OE_ASSERT(p_entry != NULL);
00151             return p_entry->prev != NULL;
00152         }
00153 
00159         bool is_last()
00160         {
00161             OE_ASSERT(p_entry != NULL);
00162             return p_entry->prev != NULL;
00163         }
00164     };
00165 
00167     OE_LIST()
00168     :first(NULL), last(null), size(0)
00169     {
00170     }
00171 
00173     ~OE_LIST()
00174     {
00175         clear(); // destroy all elements
00176     }
00177 
00181     iterator get_first()
00182     {
00183         return iterator(m_first);
00184     }
00185 
00189     iterator get_last()
00190     {
00191         return iterator(m_last);
00192     }
00193 
00199     void insert_before(iterator pos, const _UserType &data)
00200     {
00201         OE_ASSERT(pos.is_valid());
00202         insert_between(pos->prev->, pos, data);
00203     }
00204 
00210     void insert_after(iterator pos, const _UserType &data)
00211     {
00212         OE_ASSERT(pos.is_valid());
00213         insert_between(pos, pos->next, data);
00214     }
00215 
00220     void prepend(const _UserType &data)
00221     {
00222         insert_between(NULL, first, data);
00223     }
00224 
00229     void append(const _UserType &data)
00230     {
00231         insert_between(last, NULL, data);
00232     }
00233 
00234     void delete_first();    
00235     void delete_last();     
00236     void clear();           
00245     void copy_from(OE_LIST *src);
00246 
00248     uint32 num_entries();
00249 
00251     void seek_entry(uint32 num);
00252 
00254     _UserType &get_entry(uint32 num);
00255 
00259     _UserType &get_cur();
00260 
00262     uint32 is_last();
00263 
00264 private:
00265     OE_LIST_ENTRY   *first; 
00266     OE_LIST_ENTRY   *last;  
00267     uint32           size;  
00268 };
00269 
00273 template<typename _UserType>
00274 class OE_FIFO {
00275     struct OE_FIFO_ENTRY {
00276         _UserType        dat;
00277         OE_FIFO_ENTRY   *next;
00278     };
00279 public:
00280     OE_FIFO();
00281     ~OE_FIFO();
00282     void add(const _UserType &dat);
00283     const _UserType &get();
00284     void clear();
00285     uint32 queue_size;
00286 private:
00287     OE_FIFO_ENTRY *first;
00288     OE_FIFO_ENTRY *last;
00289 };
00290 
00291 #include "services/messaging.h"
00292 
00294 struct OE_MESSAGE {
00295     uint32 type;        // Type of the message
00296     void *dat;          // Data of the message
00297 };
00298 
00300 class OE_MESSAGE_INHERIT {
00301 public:
00303     virtual void pass_message(OE_MESSAGE *message) =0;
00304 };
00305 
00307 class OE_EMULATOR {
00308 public:
00309     virtual void do_frame(void);
00310     virtual void pass_message(OE_MESSAGE *message) =0;
00311 };
00312 
00314 enum OE_INTYPE_DEVICE
00315 {
00316     OE_INTYPE_KEYBOARD          = 1,    
00317     OE_INTYPE_MOUSE             = 2,    
00318     OE_INTYPE_BUTTON            = 3,    
00319     OE_INTYPE_ANALOG_AXIS       = 4     
00320 };
00321 
00323 struct OE_KB_EVENT {
00324     uint32 keynum;                  
00325     uint32 type;                    
00326     uint32 mods;                    
00327     uint32 time;                    
00328 };
00329 
00331 struct OE_INPUT_MOUSE {
00332     uint32 xmove;           
00333     uint32 ymove;           
00334     uint32 buttons[5];      
00335     uint32 scroll_wheel;    
00336 };
00337 
00339 struct OE_INPUT_BUTTON {
00340     uint32 identifier;
00341     uint8 status;
00342 };
00343 
00345 struct OE_INPUT_ANALOG_AXIS {
00346     uint32 value;                   
00347 };
00348 
00350 struct OE_INPUT_EVENT {
00351     uint32 type;                    
00352     void *dat;                      
00353 };
00354 
00358 struct OE_INPUT {
00359     OE_LIST<OE_INPUT_EVENT> inputs; 
00360     uint32 timebase;                
00361 };
00362 
00363 /* --- implementation section -- */
00364 
00365 // ******************
00366 // ** FIFO SECTION **
00367 // ******************
00368 template<typename _UserType>
00369 OE_FIFO<_UserType>::OE_FIFO()
00370 {
00371     first = last = NULL;
00372     queue_size = 0;
00373 }
00374 
00375 template<typename _UserType>
00376 OE_FIFO<_UserType>::~OE_FIFO()
00377 {
00378     clear();
00379 }
00380 
00381 template<typename _UserType>
00382 void OE_FIFO<_UserType>::clear(void)
00383 {
00384     OE_FIFO_ENTRY *foo;
00385     OE_FIFO_ENTRY *next;
00386     if (first != NULL) {    // Unallocate all things
00387         next = first;
00388         for (;;) {
00389             foo = next;
00390             next = foo->next;
00391             free(foo);
00392             queue_size--;
00393             if ((next == NULL) || (queue_size == 0)) break;
00394         }
00395     }
00396     first = last = NULL;
00397     queue_size = 0;
00398 }
00399 
00400 template<typename _UserType>
00401 void OE_FIFO<_UserType>::add(const _UserType &dat)
00402 {
00403     if (first == NULL) {
00404         first = last = (OE_FIFO_ENTRY *)malloc(sizeof(OE_FIFO_ENTRY));
00405         first->next = NULL;
00406         first->dat = dat;
00407         queue_size++;
00408     }
00409     else {
00410         last->next = (OE_FIFO_ENTRY *)malloc(sizeof(OE_FIFO_ENTRY));
00411         last = last->next;
00412         last->dat = dat;
00413         queue_size++;
00414     }
00415 }
00416 
00417 template<typename _UserType>
00418 const _UserType &OE_FIFO<_UserType>::get()
00419 {
00420     if (first == NULL) return NULL;
00421     OE_FIFO_ENTRY *foo;
00422     queue_size--;
00423     void *fr = (void *)first->dat;
00424     foo = first;
00425     first = first->next;
00426     free(foo);
00427     return fr;
00428 }
00429 
00430 #endif

Generated on Sat Sep 9 03:50:42 2006 for Openem APIs by  doxygen 1.4.7