Event.h
1 /*
2  * Copyright (C) 2008-2013 by Marc Boris Duerner
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 #ifndef PT_EVENT_H
30 #define PT_EVENT_H
31 
32 #include <Pt/Api.h>
33 #include <Pt/Types.h>
34 #include <Pt/Allocator.h>
35 #include <typeinfo>
36 #include <new>
37 
38 namespace Pt {
39 
49 class Event
50 {
51  public:
54  virtual ~Event()
55  {}
56 
59  Event& clone(Allocator& allocator) const
60  { return onClone(allocator); }
61 
64  void destroy(Allocator& allocator)
65  { onDestroy(allocator); }
66 
69  const std::type_info& typeInfo() const
70  { return onTypeInfo(); }
71 
72  protected:
76  {}
77 
80  virtual Event& onClone(Allocator& allocator) const = 0;
81 
84  virtual void onDestroy(Allocator& allocator) = 0;
85 
88  virtual const std::type_info& onTypeInfo() const = 0;
89 
90  public:
93  template <typename EventT>
94  static Event& copyConstruct(const EventT& ev, Allocator& allocator)
95  {
96  void* mem = allocator.allocate( sizeof(EventT) );
97  EventT* pev = new (mem) EventT(ev);
98  return *(pev);
99  }
100 
103  template <typename EventT>
104  static void destruct(EventT& ev, Allocator& allocator)
105  {
106  ev.~EventT();
107  allocator.deallocate(&ev, sizeof(EventT));
108  }
109 };
110 
111 template <typename T>
112 class BasicEvent : public Event
113 {
114  protected:
115  BasicEvent()
116  {}
117 
118  virtual const std::type_info& onTypeInfo() const
119  { return typeid(T); }
120 
121  virtual Event& onClone(Allocator& allocator) const
122  {
123  void* pEvent = allocator.allocate(sizeof(T));
124  return *(new (pEvent)T(*static_cast<const T*>(this)));
125  }
126 
127  virtual void onDestroy(Allocator& allocator)
128  {
129  this->~BasicEvent();
130  allocator.deallocate(this, sizeof(T));
131  }
132 };
133 
134 } // namespace Pt
135 
136 #endif
virtual void * allocate(std::size_t size)
Allocates size bytes of memory.
Definition: Allocator.h:96
virtual const std::type_info & onTypeInfo() const =0
Returns the type info for this class of events.
Base class for all event types.
Definition: Event.h:49
virtual ~Event()
Destructor.
Definition: Event.h:54
Event()
Constructor.
Definition: Event.h:75
virtual Event & onClone(Allocator &allocator) const =0
Clones this event using an allocator.
virtual void onDestroy(Allocator &allocator)=0
Destroys this event using an allocator.
static Event & copyConstruct(const EventT &ev, Allocator &allocator)
Copies an event using an allocator.
Definition: Event.h:94
void destroy(Allocator &allocator)
Destroys this event using an allocator.
Definition: Event.h:64
static void destruct(EventT &ev, Allocator &allocator)
Destructs an event using an allocator.
Definition: Event.h:104
virtual void deallocate(void *p, std::size_t)
Deallocates memory of size bytes.
Definition: Allocator.h:103
Allocator interface.
Definition: Allocator.h:81
const std::type_info & typeInfo() const
Returns the type info for this class of events.
Definition: Event.h:69
Event & clone(Allocator &allocator) const
Clones this event using an allocator.
Definition: Event.h:59