CrystalSpace

Public API Reference

Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

csevent.h

Go to the documentation of this file.
00001 /*
00002     Crystal Space 3D engine: Event class interface
00003     Written by Andrew Zabolotny <bit@eltech.ru>, Jonathan Tarbox, 
00004       Frank Richter
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Library General Public License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public
00017     License along with this library; if not, write to the Free
00018     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019 */
00020 
00021 #ifndef __CS_CSEVENT_H__
00022 #define __CS_CSEVENT_H__
00023 
00024 #include "csextern.h"
00025 
00026 #include "csutil/hash.h"
00027 #include "csutil/strset.h"
00028 #include "csutil/weakref.h"
00029 
00030 #include "iutil/event.h"
00031 
00032 class csEventQueue;
00033 
00038 class csEventAttributeIterator;
00039 
00040 SCF_VERSION (csEvent, 0, 0, 1);
00041 
00048 class CS_CRYSTALSPACE_EXPORT csEvent : public iEvent
00049 {
00050 private:
00051   struct attribute
00052   {
00053     union
00054     {
00055       int64 intVal;
00056       double doubleVal;
00057       char* bufferVal;
00058       iBase* ibaseVal;
00059     };
00060     csEventAttributeType type;
00061     size_t dataSize;
00062     attribute (csEventAttributeType t) { type = t; }
00063     ~attribute () 
00064     { 
00065       if (type == csEventAttrDatabuffer) 
00066         delete[] bufferVal; 
00067       else if ((type == csEventAttrEvent) || (type == csEventAttriBase))
00068         ibaseVal->DecRef();
00069     }
00070   };
00071   csHash<attribute*, csStringID> attributes;
00072   friend class csEventAttributeIterator;
00073 
00074   size_t count;
00075 
00076   bool CheckForLoops(iEvent *current, iEvent *e);
00077 
00078   template <class T>
00079   bool InternalAddInt (const char* name, T value)
00080   {
00081     if (attributes.In (GetKeyID (name))) return false;
00082     attribute* object = new attribute (csEventAttrInt); 
00083     object->intVal = (int64)value;                              
00084     attributes.Put (GetKeyID (name), object);                           
00085     count++;                                                    
00086     return true;                                                
00087   }
00088 
00089   template <class T>
00090   bool InternalAddUInt (const char* name, T value)
00091   {
00092     if (attributes.In (GetKeyID (name))) return false;
00093     attribute* object = new attribute (csEventAttrUInt);        
00094     object->intVal = (int64)value;                              
00095     attributes.Put (GetKeyID (name), object);                           
00096     count++;                                                    
00097     return true;                                                
00098   }
00099 
00100   csEventError InternalReportMismatch (attribute* attr) const
00101   {
00102     switch (attr->type)
00103     {
00104       case csEventAttrInt:
00105         return csEventErrMismatchInt;
00106       case csEventAttrUInt:
00107         return csEventErrMismatchUInt;
00108       case csEventAttrFloat:
00109         return csEventErrMismatchFloat;
00110       case csEventAttrDatabuffer:
00111         return csEventErrMismatchBuffer;
00112       case csEventAttrEvent:
00113         return csEventErrMismatchEvent;
00114       case csEventAttriBase:
00115         return csEventErrMismatchIBase;
00116       default:
00117         break;
00118     }
00119     return csEventErrUhOhUnknown;
00120   }
00121 
00122   template <class T>
00123   csEventError InternalRetrieveInt (const char* name, T& value) const
00124   {                                                             
00125     attribute* object = attributes.Get (GetKeyID (name), 0);
00126     if (!object) return csEventErrNotFound;
00127     if ((object->type == csEventAttrInt) || (object->type == csEventAttrUInt))
00128     {                                                                   
00129       value = (T)object->intVal;
00130       const T rangeMin = (T)(1 << (sizeof(T) * 8 - 1));
00131       const T rangeMax = ~rangeMin;
00132       if ((object->intVal < rangeMin) || (object->intVal > rangeMax))
00133         return csEventErrLossy;
00134       else
00135         return csEventErrNone;
00136     }
00137     else
00138     {
00139       return InternalReportMismatch (object);
00140     }
00141   }
00142 
00143   template <class T>
00144   csEventError InternalRetrieveUint (const char* name, T& value) const
00145   {                                                             
00146     attribute* object = attributes.Get (GetKeyID (name), 0);
00147     if (!object) return csEventErrNotFound;
00148     if ((object->type == csEventAttrInt) || (object->type == csEventAttrUInt))
00149     {                                                                   
00150       value = (T)object->intVal;
00151       const T rangeMax = (T)~0;
00152       if ((uint64)object->intVal > rangeMax)
00153         return csEventErrLossy;
00154       else
00155         return csEventErrNone;                                                  
00156     }
00157     else
00158     {
00159       return InternalReportMismatch (object);
00160     }
00161   }
00162 
00163   static char const* GetTypeName (csEventAttributeType t);
00164   static csStringID GetKeyID (const char* key);
00165   static const char* GetKeyName (csStringID id);
00166 protected:
00167   virtual csRef<iEvent> CreateEvent();
00168 
00169 public:
00171   csEvent ();
00172 
00177   csEvent (csEvent const&);
00178 
00180   csEvent (csTicks, int type, int x, int y, uint button, uint32 modifiers);
00181 
00183   csEvent (csTicks, int type, uint n, int x, int y, uint32 axesChanged, 
00184     uint button, uint32 modifiers);
00185 
00187   csEvent (csTicks, int type, uint n, const int32 *axes, uint8 numAxes, 
00188            uint32 axesChanged, uint8 button, uint32 modifiers);
00189 
00191   csEvent (csTicks, int type, uint code, intptr_t info = 0);
00192 
00194   virtual ~csEvent ();
00195 
00197 
00198 #define CS_CSEVENT_ADDINT(type)                                 \
00199   virtual bool Add (const char* name, type value)               \
00200   { return InternalAddInt (name, value); }
00201   CS_CSEVENT_ADDINT(int8)
00202   CS_CSEVENT_ADDINT(int16)
00203   CS_CSEVENT_ADDINT(int32)
00204   CS_CSEVENT_ADDINT(int64)
00205 #undef CS_CSEVENT_ADDINT
00206 #define CS_CSEVENT_ADDUINT(type)                                \
00207   virtual bool Add (const char* name, type value)               \
00208   { return InternalAddUInt (name, value); }
00209   CS_CSEVENT_ADDUINT(uint8)
00210   CS_CSEVENT_ADDUINT(uint16)
00211   CS_CSEVENT_ADDUINT(uint32)
00212   CS_CSEVENT_ADDUINT(uint64)
00213 #undef CS_CSEVENT_ADDUINT
00214   virtual bool Add (const char *name, float v);
00215   virtual bool Add (const char *name, double v);
00216   virtual bool Add (const char *name, const char *v);
00217   virtual bool Add (const char *name, const void *v, size_t size);
00218   virtual bool Add (const char *name, bool v);
00219   virtual bool Add (const char *name, iEvent* v);
00220   virtual bool Add (const char *name, iBase* v);
00221 
00223 #define CS_CSEVENT_FINDINT(T)                                           \
00224   virtual csEventError Retrieve (const char* name, T& value) const      \
00225   { return InternalRetrieveInt (name, value); }
00226   CS_CSEVENT_FINDINT(int8)
00227   CS_CSEVENT_FINDINT(int16)
00228   CS_CSEVENT_FINDINT(int32)
00229 #undef CS_CSEVENT_FINDINT
00230   virtual csEventError Retrieve (const char* name, int64& value) const
00231   {                                                             
00232     attribute* object = attributes.Get (GetKeyID (name), 0);
00233     if (!object) return csEventErrNotFound;
00234     if ((object->type == csEventAttrInt) || (object->type == csEventAttrUInt))
00235     {                                                                   
00236       value = object->intVal;
00237       return csEventErrNone;
00238     }
00239     else
00240     {
00241       return InternalReportMismatch (object);
00242     }
00243   }
00244 
00245 #define CS_CSEVENT_FINDUINT(T)                                          \
00246   virtual csEventError Retrieve (const char* name, T& value) const      \
00247   { return InternalRetrieveUint (name, value); }
00248   CS_CSEVENT_FINDUINT(uint8)
00249   CS_CSEVENT_FINDUINT(uint16)
00250   CS_CSEVENT_FINDUINT(uint32)
00251 #undef CS_CSEVENT_FINDUINT
00252   virtual csEventError Retrieve (const char* name, uint64& value) const
00253   {                                                             
00254     attribute* object = attributes.Get (GetKeyID (name), 0);
00255     if (!object) return csEventErrNotFound;
00256     if ((object->type == csEventAttrInt) || (object->type == csEventAttrUInt))
00257     {                                                                   
00258       value = (uint64)object->intVal;
00259       return csEventErrNone;
00260     }
00261     else
00262     {
00263       return InternalReportMismatch (object);
00264     }
00265   }
00266 
00267   virtual csEventError Retrieve (const char *name, float &v) const;
00268   virtual csEventError Retrieve (const char *name, double &v) const;
00269   virtual csEventError Retrieve (const char *name, const char *&v) const;
00270   virtual csEventError Retrieve (const char *name, const void *&v,
00271         size_t &size) const;
00272   virtual csEventError Retrieve (const char *name, bool &v) const;
00273   virtual csEventError Retrieve (const char *name, csRef<iEvent> &v) const;
00274   virtual csEventError Retrieve (const char *name, csRef<iBase> &v) const;
00275 
00276   virtual bool AttributeExists (const char* name);
00277   virtual csEventAttributeType GetAttributeType (const char* name);
00278 
00279   virtual bool Remove (const char *name);
00280   virtual bool RemoveAll ();
00281 
00282   virtual csRef<iEventAttributeIterator> GetAttributeIterator();
00283 
00284   virtual bool Print (int level = 0);
00285 
00286   SCF_DECLARE_IBASE;
00287 };
00288 
00296 class CS_CRYSTALSPACE_EXPORT csPoolEvent : public csEvent
00297 {
00298   typedef csEvent superclass;
00299   friend class csEventQueue;
00300   friend class csEvent;
00301 
00302 private:
00303   // As per the XML pool, keep a reference to the pool container obejct
00304   // and this also allows our overridden DecRef() to place the event back
00305   // into the pool when users are done with it.
00306   csWeakRef<csEventQueue> pool;
00307 
00308   // The next event in the pool, or null if the event is in use.
00309   csPoolEvent *next;
00310 
00311   // The 'real' DecRef() call that deletes the event, should in theory only be
00312   // called from csEventQueue.
00313   void Free () { csEvent::DecRef(); }
00314 
00315 protected:
00316   virtual csRef<iEvent> CreateEvent();
00317 
00318 public:
00320   csPoolEvent (csEventQueue *q);
00321 
00323   virtual void DecRef ();
00324 };
00325 
00329 class csEventAttributeIterator : public iEventAttributeIterator
00330 {
00331   csHash<csEvent::attribute*, csStringID>::GlobalIterator iterator;                                                              
00332 public:
00333   SCF_DECLARE_IBASE;
00334   
00335   csEventAttributeIterator (
00336     csHash<csEvent::attribute*, csStringID>::GlobalIterator& iter) : 
00337     iterator(iter)
00338   {
00339     SCF_CONSTRUCT_IBASE(0);
00340   }
00341 
00342   virtual ~csEventAttributeIterator()
00343   {
00344     SCF_DESTRUCT_IBASE();
00345   }
00346 
00347   virtual bool HasNext()
00348   {
00349     return iterator.HasNext();
00350   }
00351   virtual const char* Next();
00352   virtual void Reset()
00353   {
00354     iterator.Reset();
00355   }
00356 };
00357 
00358 #endif // __CS_CSEVENT_H__

Generated for Crystal Space by doxygen 1.4.4