source: nrEngine/include/Event.h @ 19

Revision 19, 3.2 KB checked in by art, 13 years ago (diff)
Line 
1/***************************************************************************
2 *                                                                         *
3 *   (c) Art Tevs, MPI Informatik Saarbruecken                             *
4 *       mailto: <tevs@mpi-sb.mpg.de>                                      *
5 *                                                                         *
6 *   This program is free software; you can redistribute it and/or modify  *
7 *   it under the terms of the GNU General Public License as published by  *
8 *   the Free Software Foundation; either version 2 of the License, or     *
9 *   (at your option) any later version.                                   *
10 *                                                                         *
11 ***************************************************************************/
12
13
14#ifndef _NR_EVENT__H_
15#define _NR_EVENT__H_
16
17//----------------------------------------------------------------------------------
18// Includes
19//----------------------------------------------------------------------------------
20#include "Prerequisities.h"
21#include "Priority.h"
22#include "Exception.h"
23
24namespace nrEngine{
25
26        //! Base untemplated class used for the event instancies
27        /**
28         * Event is a base untemplated class.
29         * Each new type of event should be derived from the templated event class
30         * EventT, which does store some runtime type information about the event.
31         *
32         * \ingroup event
33         **/
34        class _NRExport Event {
35                public:
36
37                        /**
38                         * Release used memory
39                         **/
40                        virtual ~Event();
41
42                        /**
43                         * Get the priority of this event message.
44                         **/
45                        const Priority& getPriority();
46
47                        /**
48                         * Check whenever this class is of the same type
49                         * as the given one. The function is templated,
50                         * so you can check for every kind of classes
51                         * if the type of an object the same on as given one.
52                         **/
53                        template<class U> bool same_as()
54                        {
55                                // we try a dynamic cast if it fails, so types are different
56                                U* ptr = dynamic_cast<U*>(this);
57                                if (ptr == NULL) return false;
58
59                                return true;
60                        }
61
62        protected:
63                        /**
64                        * Create new instance of a base class Event.
65                        *
66                        * @param typeName Unique type name for this class.
67                        *
68                        * NOTE: You can not directly derive new classes from
69                        * this one, because the constructor is private and can
70                        * only be accessed from friend class EventT<>.
71                        * We implemented it in this way to hide the internal
72                        * runtime typechecking from the user.
73                        **/
74                        Event(Priority prior);
75
76
77                private:
78
79                        //! Priority of the message
80                        Priority mPriority;
81
82        };
83
84        /**
85        * Cast a certain event to another one. As casting we use
86        * dynamic_cast<T>() which will return 0 if casting
87        * can not be done of this kind of objects.
88        *
89        * If casting could not be done, so an exception will be thrown, which
90        * you have to catch.
91         *
92         * \ingroup event
93        **/
94        template<class T>
95        static T* event_cast(Event* base)
96        {
97                T* ptr = NULL;
98                ptr = dynamic_cast<T*>(base);
99                return ptr;
100        }
101
102        /**
103        * Same as event_cast() but cast now between
104        * smart pointers.
105         *
106         * \ingroup event
107        **/
108        template<class T>
109        static SharedPtr<T> event_shared_cast(SharedPtr<Event> base)  throw (Exception)
110        {
111                SharedPtr<T> ptr;
112                ptr = boost::dynamic_pointer_cast<T, Event>(base);
113                return ptr;
114        }
115
116
117}; // end namespace
118
119#endif
Note: See TracBrowser for help on using the repository browser.