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
38namespace Pt {
39
65class Event
66{
67 public:
70 virtual ~Event()
71 {}
72
75 Event& clone(Allocator& allocator) const
76 { return onClone(allocator); }
77
80 void destroy(Allocator& allocator)
81 { onDestroy(allocator); }
82
85 const std::type_info& typeInfo() const
86 { return onTypeInfo(); }
87
88 protected:
92 {}
93
96 virtual Event& onClone(Allocator& allocator) const = 0;
97
100 virtual void onDestroy(Allocator& allocator) = 0;
101
104 virtual const std::type_info& onTypeInfo() const = 0;
105
106 public:
109 template <typename EventT>
110 static Event& copyConstruct(const EventT& ev, Allocator& allocator)
111 {
112 void* mem = allocator.allocate( sizeof(EventT) );
113 EventT* pev = new (mem) EventT(ev);
114 return *(pev);
115 }
116
119 template <typename EventT>
120 static void destruct(EventT& ev, Allocator& allocator)
121 {
122 ev.~EventT();
123 allocator.deallocate(&ev, sizeof(EventT));
124 }
125};
126
142template <typename T>
143class BasicEvent : public Event
144{
145 protected:
146 BasicEvent()
147 {}
148
149 virtual const std::type_info& onTypeInfo() const
150 { return typeid(T); }
151
152 virtual Event& onClone(Allocator& allocator) const
153 {
154 void* pEvent = allocator.allocate(sizeof(T));
155 return *(new (pEvent)T(*static_cast<const T*>(this)));
156 }
157
158 virtual void onDestroy(Allocator& allocator)
159 {
160 this->~BasicEvent();
161 allocator.deallocate(this, sizeof(T));
162 }
163};
164
165} // namespace Pt
166
167#endif
Allocator interface.
Definition Allocator.h:82
virtual void * allocate(std::size_t size)
Allocates size bytes of memory.
Definition Allocator.h:96
virtual void deallocate(void *p, std::size_t)
Deallocates memory of size bytes.
Definition Allocator.h:103
virtual Event & onClone(Allocator &allocator) const
Clones this event using an allocator.
Definition Event.h:152
virtual const std::type_info & onTypeInfo() const
Returns the type info for this class of events.
Definition Event.h:149
virtual void onDestroy(Allocator &allocator)
Destroys this event using an allocator.
Definition Event.h:158
virtual void onDestroy(Allocator &allocator)=0
Destroys this event using an allocator.
virtual Event & onClone(Allocator &allocator) const =0
Clones this event using an allocator.
static void destruct(EventT &ev, Allocator &allocator)
Destructs an event using an allocator.
Definition Event.h:120
Event()
Constructor.
Definition Event.h:91
virtual ~Event()
Destructor.
Definition Event.h:70
void destroy(Allocator &allocator)
Destroys this event using an allocator.
Definition Event.h:80
static Event & copyConstruct(const EventT &ev, Allocator &allocator)
Copies an event using an allocator.
Definition Event.h:110
const std::type_info & typeInfo() const
Returns the type info for this class of events.
Definition Event.h:85
Event & clone(Allocator &allocator) const
Clones this event using an allocator.
Definition Event.h:75
virtual const std::type_info & onTypeInfo() const =0
Returns the type info for this class of events.
Core module.
Definition Allocator.h:33