Event.h

00001 /***************************************************************************
00002  *                                                                         *
00003  *   (c) Art Tevs, MPI Informatik Saarbruecken                             *
00004  *       mailto: <tevs@mpi-sb.mpg.de>                                      *
00005  *                                                                         *
00006  *   This program is free software; you can redistribute it and/or modify  *
00007  *   it under the terms of the GNU General Public License as published by  *
00008  *   the Free Software Foundation; either version 2 of the License, or     *
00009  *   (at your option) any later version.                                   *
00010  *                                                                         *
00011  ***************************************************************************/
00012 
00013 
00014 #ifndef _NR_EVENT__H_
00015 #define _NR_EVENT__H_
00016 
00017 //----------------------------------------------------------------------------------
00018 // Includes
00019 //----------------------------------------------------------------------------------
00020 #include "Prerequisities.h"
00021 #include "Priority.h"
00022 #include "Exception.h"
00023 
00024 namespace nrEngine{
00025 
00026         /**
00027         * Macro for definining event type information.
00028         * Use this macro in all event definitions of classes
00029         * derived from Event.
00030         *
00031         * \ingroup event 
00032         **/
00033         #define META_Event(type) \
00034                 public:\
00035                         virtual const char* getEventType() const { return #type; }
00036                 
00037         
00038         //! Base untemplated class used for the event instancies
00039         /**
00040          * Event is a base untemplated class.
00041          * Each new type of event should be derived from the templated event class
00042          * EventT, which does store some runtime type information about the event.
00043          *
00044          * \ingroup event
00045          **/
00046         class _NRExport Event {
00047                 
00048                 META_Event(Event)
00049                 
00050                 public:
00051 
00052                         /**
00053                          * Release used memory
00054                          **/
00055                         virtual ~Event();
00056 
00057                         /**
00058                          * Get the priority of this event message.
00059                          **/
00060                         const Priority& getPriority();
00061                         
00062                         /**
00063                          * Check whenever this class is of the same type
00064                          * as the given one. The function is templated,
00065                          * so you can check for every kind of classes
00066                          * if the type of an object the same on as given one.
00067                          **/
00068                         template<class U> bool same_as()
00069                         {
00070                                 // we try a dynamic cast if it fails, so types are different
00071                                 U* ptr = dynamic_cast<U*>(this);
00072                                 if (ptr == NULL) return false;
00073 
00074                                 return true;
00075                         }
00076 
00077                 protected:
00078                         /**
00079                         * Create new instance of a base class Event.
00080                         *
00081                         * @param prior Set priority for this event
00082                         *
00083                         * NOTE: You can not directly derive new classes from
00084                         * this one, because the constructor is private and can
00085                         * only be accessed from friend class EventT<>.
00086                         * We implemented it in this way to hide the internal
00087                         * runtime typechecking from the user.
00088                         **/
00089                         Event(Priority prior);
00090 
00091                 private:
00092 
00093                         //! Priority of the message
00094                         Priority mPriority;
00095         };
00096 
00097         /**
00098         * Cast a certain event to another one. As casting we use
00099         * dynamic_cast<T>() which will return 0 if casting
00100         * can not be done of this kind of objects.
00101         *
00102         * If casting could not be done, so an exception will be thrown, which
00103         * you have to catch.
00104          *
00105          * \ingroup event
00106         **/
00107         template<class T>
00108         static T* event_cast(Event* base)
00109         {
00110                 T* ptr = NULL;
00111                 ptr = dynamic_cast<T*>(base);
00112                 return ptr;
00113         }
00114 
00115         /**
00116         * Same as event_cast() but cast now between
00117         * smart pointers.
00118          *
00119          * \ingroup event
00120         **/
00121         template<class T>
00122         static SharedPtr<T> event_shared_cast(SharedPtr<Event> base)  throw (Exception)
00123         {
00124                 SharedPtr<T> ptr;
00125                 ptr = boost::dynamic_pointer_cast<T, Event>(base);
00126                 return ptr;
00127         }
00128 
00129 
00130 }; // end namespace
00131 
00132 #endif

Generated on Wed Sep 12 23:19:42 2007 for nrEngine by  doxygen 1.5.1