CrystalSpace

Public API Reference

Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | File List | 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 #include "iutil/event.h"
00026 #include "hashr.h"
00027 #include "hashhandlers.h"
00028 #include "csendian.h"
00029 #include "weakref.h"
00030 #include "cseventq.h"
00031 #include "strset.h"
00032 
00037 class csEventAttributeIterator;
00038 
00039 class csEvent;
00040 
00041 SCF_VERSION (csEvent, 0, 0, 1);
00042 
00049 class CS_CSUTIL_EXPORT csEvent : public iEvent
00050 {
00051 private:
00052   struct attribute
00053   {
00054     union
00055     {
00056       int64 intVal;
00057       double doubleVal;
00058       char* bufferVal;
00059       iBase* ibaseVal;
00060     };
00061     csEventAttributeType type;
00062     size_t dataSize;
00063     attribute (csEventAttributeType t) { type = t; }
00064     ~attribute () 
00065     { 
00066       if (type == csEventAttrDatabuffer) 
00067         delete[] bufferVal; 
00068       else if ((type == csEventAttrEvent) || (type == csEventAttriBase))
00069         ibaseVal->DecRef();
00070     }
00071   };
00072   csHash<attribute*, csStringID> attributes;
00073   friend class csEventAttributeIterator;
00074 
00075   size_t count;
00076 
00077   bool CheckForLoops(iEvent *current, iEvent *e);
00078 
00079   template <class T>
00080   bool InternalAddInt (const char* name, T value)
00081   {
00082     if (attributes.In (GetKeyID (name))) return false;
00083     attribute* object = new attribute (csEventAttrInt); 
00084     object->intVal = (int64)value;                              
00085     attributes.Put (GetKeyID (name), object);                           
00086     count++;                                                    
00087     return true;                                                
00088   }
00089 
00090   template <class T>
00091   bool InternalAddUInt (const char* name, T value)
00092   {
00093     if (attributes.In (GetKeyID (name))) return false;
00094     attribute* object = new attribute (csEventAttrUInt);        
00095     object->intVal = (int64)value;                              
00096     attributes.Put (GetKeyID (name), object);                           
00097     count++;                                                    
00098     return true;                                                
00099   }
00100 
00101   csEventError InternalReportMismatch (attribute* attr) const
00102   {
00103     switch (attr->type)
00104     {
00105       case csEventAttrInt:
00106         return csEventErrMismatchInt;
00107       case csEventAttrUInt:
00108         return csEventErrMismatchUInt;
00109       case csEventAttrFloat:
00110         return csEventErrMismatchFloat;
00111       case csEventAttrDatabuffer:
00112         return csEventErrMismatchBuffer;
00113       case csEventAttrEvent:
00114         return csEventErrMismatchEvent;
00115       case csEventAttriBase:
00116         return csEventErrMismatchIBase;
00117       default:
00118         break;
00119     }
00120     return csEventErrUhOhUnknown;
00121   }
00122 
00123   template <class T>
00124   csEventError InternalRetrieveInt (const char* name, T& value) const
00125   {                                                             
00126     attribute* object = attributes.Get (GetKeyID (name), 0);
00127     if (!object) return csEventErrNotFound;
00128     if ((object->type == csEventAttrInt) || (object->type == csEventAttrUInt))
00129     {                                                                   
00130       value = (T)object->intVal;
00131       const T rangeMin = (T)(1 << (sizeof(T) * 8 - 1));
00132       const T rangeMax = ~rangeMin;
00133       if ((object->intVal < rangeMin) || (object->intVal > rangeMax))
00134         return csEventErrLossy;
00135       else
00136         return csEventErrNone;
00137     }
00138     else
00139     {
00140       return InternalReportMismatch (object);
00141     }
00142   }
00143 
00144   template <class T>
00145   csEventError InternalRetrieveUint (const char* name, T& value) const
00146   {                                                             
00147     attribute* object = attributes.Get (GetKeyID (name), 0);
00148     if (!object) return csEventErrNotFound;
00149     if ((object->type == csEventAttrInt) || (object->type == csEventAttrUInt))
00150     {                                                                   
00151       value = (T)object->intVal;
00152       const T rangeMax = (T)~0;
00153       if ((uint64)object->intVal > rangeMax)
00154         return csEventErrLossy;
00155       else
00156         return csEventErrNone;                                                  
00157     }
00158     else
00159     {
00160       return InternalReportMismatch (object);
00161     }
00162   }
00163 
00164   static char const* GetTypeName (csEventAttributeType t);
00165   static csStringID GetKeyID (const char* key);
00166   static const char* GetKeyName (csStringID id);
00167 protected:
00168   virtual csRef<iEvent> CreateEvent();
00169 
00170 public:
00172   csEvent ();
00173 
00178   csEvent (csEvent const&);
00179 
00181   csEvent (csTicks, int type, int x, int y, int button, int modifiers);
00182 
00184   csEvent (csTicks, int type, int n, int x, int y, int button, int modifiers);
00185 
00187   csEvent (csTicks, int type, int code, void* info = 0);
00188 
00190   virtual ~csEvent ();
00191 
00193 
00194 #define CS_CSEVENT_ADDINT(type)                                 \
00195   virtual bool Add (const char* name, type value)               \
00196   { return InternalAddInt (name, value); }
00197   CS_CSEVENT_ADDINT(int8)
00198   CS_CSEVENT_ADDINT(int16)
00199   CS_CSEVENT_ADDINT(int32)
00200   CS_CSEVENT_ADDINT(int64)
00201 #undef CS_CSEVENT_ADDINT
00202 #define CS_CSEVENT_ADDUINT(type)                                \
00203   virtual bool Add (const char* name, type value)               \
00204   { return InternalAddUInt (name, value); }
00205   CS_CSEVENT_ADDUINT(uint8)
00206   CS_CSEVENT_ADDUINT(uint16)
00207   CS_CSEVENT_ADDUINT(uint32)
00208   CS_CSEVENT_ADDUINT(uint64)
00209 #undef CS_CSEVENT_ADDUINT
00210   virtual bool Add (const char *name, float v);
00211   virtual bool Add (const char *name, double v);
00212   virtual bool Add (const char *name, const char *v);
00213   virtual bool Add (const char *name, const void *v, size_t size);
00214   virtual bool Add (const char *name, bool v);
00215   virtual bool Add (const char *name, iEvent* v);
00216   virtual bool Add (const char *name, iBase* v);
00217 
00219 #define CS_CSEVENT_FINDINT(T)                                           \
00220   virtual csEventError Retrieve (const char* name, T& value) const      \
00221   { return InternalRetrieveInt (name, value); }
00222   CS_CSEVENT_FINDINT(int8)
00223   CS_CSEVENT_FINDINT(int16)
00224   CS_CSEVENT_FINDINT(int32)
00225 #undef CS_CSEVENT_FINDINT
00226   virtual csEventError Retrieve (const char* name, int64& value) const
00227   {                                                             
00228     attribute* object = attributes.Get (GetKeyID (name), 0);
00229     if (!object) return csEventErrNotFound;
00230     if ((object->type == csEventAttrInt) || (object->type == csEventAttrUInt))
00231     {                                                                   
00232       value = object->intVal;
00233       return csEventErrNone;
00234     }
00235     else
00236     {
00237       return InternalReportMismatch (object);
00238     }
00239   }
00240 
00241 #define CS_CSEVENT_FINDUINT(T)                                          \
00242   virtual csEventError Retrieve (const char* name, T& value) const      \
00243   { return InternalRetrieveUint (name, value); }
00244   CS_CSEVENT_FINDUINT(uint8)
00245   CS_CSEVENT_FINDUINT(uint16)
00246   CS_CSEVENT_FINDUINT(uint32)
00247 #undef CS_CSEVENT_FINDUINT
00248   virtual csEventError Retrieve (const char* name, uint64& value) const
00249   {                                                             
00250     attribute* object = attributes.Get (GetKeyID (name), 0);
00251     if (!object) return csEventErrNotFound;
00252     if ((object->type == csEventAttrInt) || (object->type == csEventAttrUInt))
00253     {                                                                   
00254       value = (uint64)object->intVal;
00255       return csEventErrNone;
00256     }
00257     else
00258     {
00259       return InternalReportMismatch (object);
00260     }
00261   }
00262 
00263   virtual csEventError Retrieve (const char *name, float &v) const;
00264   virtual csEventError Retrieve (const char *name, double &v) const;
00265   virtual csEventError Retrieve (const char *name, const char *&v) const;
00266   virtual csEventError Retrieve (const char *name, const void *&v,
00267         size_t &size) const;
00268   virtual csEventError Retrieve (const char *name, bool &v) const;
00269   virtual csEventError Retrieve (const char *name, csRef<iEvent> &v) const;
00270   virtual csEventError Retrieve (const char *name, csRef<iBase> &v) const;
00271 
00272   virtual bool AttributeExists (const char* name);
00273   virtual csEventAttributeType GetAttributeType (const char* name);
00274 
00275   virtual bool Remove (const char *name);
00276   virtual bool RemoveAll ();
00277 
00278   virtual csRef<iEventAttributeIterator> GetAttributeIterator();
00279 
00280   virtual bool Print (int level = 0);
00281 
00282   SCF_DECLARE_IBASE;
00283 };
00284 
00292 class CS_CSUTIL_EXPORT csPoolEvent : public csEvent
00293 {
00294   typedef csEvent superclass;
00295   friend class csEventQueue;
00296   friend class csEvent;
00297 
00298 private:
00299   // As per the XML pool, keep a reference to the pool container obejct
00300   // and this also allows our overridden DecRef() to place the event back
00301   // into the pool when users are done with it.
00302   csWeakRef<csEventQueue> pool;
00303 
00304   // The next event in the pool, or null if the event is in use.
00305   csPoolEvent *next;
00306 
00307   // The 'real' DecRef() call that deletes the event, should in theory only be
00308   // called from csEventQueue.
00309   void Free () { csEvent::DecRef(); }
00310 
00311 protected:
00312   virtual csRef<iEvent> CreateEvent();
00313 
00314 public:
00316   csPoolEvent (csEventQueue *q);
00317 
00319   virtual void DecRef ();
00320 };
00321 
00325 class csEventAttributeIterator : public iEventAttributeIterator
00326 {
00327   csHash<csEvent::attribute*, csStringID>::GlobalIterator iterator;                                                              
00328 public:
00329   SCF_DECLARE_IBASE;
00330   
00331   csEventAttributeIterator (
00332     csHash<csEvent::attribute*, csStringID>::GlobalIterator& iter) : 
00333     iterator(iter)
00334   {
00335     SCF_CONSTRUCT_IBASE(0);
00336   }
00337 
00338   virtual ~csEventAttributeIterator()
00339   {
00340     SCF_DESTRUCT_IBASE();
00341   }
00342 
00343   virtual bool HasNext()
00344   {
00345     return iterator.HasNext();
00346   }
00347   virtual const char* Next();
00348   virtual void Reset()
00349   {
00350     iterator.Reset();
00351   }
00352 };
00353 
00354 #endif // __CS_CSEVENT_H__

Generated for Crystal Space by doxygen 1.3.9.1