-
Notifications
You must be signed in to change notification settings - Fork 0
/
object.h
210 lines (174 loc) · 8.04 KB
/
object.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#ifndef OBJECT_H
#define OBJECT_H
// PUT
#include <put/cxxutils/posix_helpers.h>
#include <put/application.h>
// STL
#include <functional>
#include <map>
#include <new>
struct ProtoObject
{
inline ProtoObject(void) noexcept : self(this) { } // make object valid
inline ~ProtoObject(void) noexcept { self = nullptr; } // invalidate object
inline bool valid(void) const { return this == self; } // test object validity
void* self;
};
class Object : private ProtoObject
{
public:
template<class ObjType, typename RType, typename... ArgTypes>
using mslot_t = RType(ObjType::*)(ArgTypes...); // member slot
template<typename RType, typename... ArgTypes>
using fslot_t = std::function<RType(ArgTypes...) noexcept>; // function slot
template<typename RType, typename... ArgTypes>
using fpslot_t = RType(*)(ArgTypes...); // function pointer slot
template<typename... ArgTypes>
class signal : private std::multimap<ProtoObject*, fslot_t<void, ProtoObject*, ArgTypes...>>
{
public:
typedef std::multimap<ProtoObject*, fslot_t<void, ProtoObject*, ArgTypes...>> storage_t;
// inline signal(void) noexcept = default;
// inline ~signal(void) noexcept = default;
bool invocation(ArgTypes&... args) noexcept
{
if(!storage_t::empty() && // ensure that invalid signals are ignored
Application::ms_signal_queue.lock()) // multithread protection
{
for(typename storage_t::iterator pos = storage_t::begin(); pos != storage_t::end(); ) // will iterate through all connected slots
{
if(pos->first == nullptr || pos->first->valid()) // IF no object OR object is valid
{
Application::ms_signal_queue.emplace(std::bind(pos->second, pos->first, std::forward<ArgTypes>(args)...));
++pos; // iterate
}
else // IF has object AND object is invalid
pos = storage_t::erase(pos); // erase and iterate
}
Application::step(); // inform execution stepper
return Application::ms_signal_queue.unlock();
}
return false;
}
// connect to a member of an object
template<class ObjType, typename RType>
inline void connect(ObjType* obj, mslot_t<ObjType, RType, ArgTypes...> slot) noexcept
{
storage_t::emplace(static_cast<ProtoObject*>(obj),
[slot](ProtoObject* p, ArgTypes... args) noexcept
{ if(p->valid()) (static_cast<ObjType*>(p)->*slot)(args...); }); // if ProtoObject is valid (not deleted), call slot
}
// connect to a function that accept the object pointer as the first argument
template<class ObjType, typename RType>
inline void connect(ObjType* obj, fslot_t<RType, ObjType*, ArgTypes...> slot) noexcept
{
storage_t::emplace(static_cast<ProtoObject*>(obj),
[slot](ProtoObject* p, ArgTypes... args) noexcept
{ if(p->valid()) slot(static_cast<ObjType*>(p), args...); }); // if ProtoObject is valid (not deleted), call slot
}
// connect to a function and ignore the object
template<typename RType>
inline void connect(fslot_t<RType, ArgTypes...> slot) noexcept
{
storage_t::emplace(nullptr,
[slot](ProtoObject*, ArgTypes... args) noexcept
{ slot(args...); });
}
// connect to another signal
inline void connect(signal<ArgTypes...>& other) noexcept
{
storage_t::emplace(nullptr,
[&other](ProtoObject*, ArgTypes... args) noexcept
{ other.invocation(args...); });
}
inline void disconnect(Object* obj) noexcept
{ storage_t::erase(obj); }
inline void disconnect(void) noexcept
{ storage_t::clear(); }
};
inline Object(void) noexcept = default;
inline ~Object(void) noexcept = default;
// connect to a member of an object
template<class ObjType, typename RType, typename... ArgTypes>
static inline void connect(signal<ArgTypes...>& sig, ObjType* obj, mslot_t<ObjType, RType, ArgTypes...> slot) noexcept
{ sig.connect(obj, slot); }
// connect to another signal
template<typename... ArgTypes>
static inline void connect(signal<ArgTypes...>& sig1, signal<ArgTypes...>& sig2) noexcept
{ sig1.connect(sig2); }
// connect to a function that accept the object pointer as the first argument
template<class ObjType, typename RType, typename... ArgTypes>
static inline void connect(signal<ArgTypes...>& sig, ObjType* obj, fslot_t<RType, ObjType*, ArgTypes...> slot) noexcept
{ sig.connect(obj, slot); }
template<class ObjType, typename RType, typename... ArgTypes>
static inline void connect(signal<ArgTypes...>& sig, ObjType* obj, fpslot_t<RType, ObjType*, ArgTypes...> slot) noexcept
{ sig.connect(obj, fslot_t<RType, ArgTypes...>(slot)); }
// connect to a lambda function
template<typename Lambda, typename... ArgTypes>
static inline void connect(signal<ArgTypes...>& sig, Lambda&& slot) noexcept
{ sig.connect(fslot_t<void, ArgTypes...>(slot)); }
// connect to a function and ignore the object
template<typename RType, typename... ArgTypes>
static inline void connect(signal<ArgTypes...>& sig, fslot_t<RType, ArgTypes...> slot) noexcept
{ sig.connect(slot); }
template<typename RType, typename... ArgTypes>
static inline void connect(signal<ArgTypes...>& sig, fpslot_t<RType, ArgTypes...> slot) noexcept
{ sig.connect(fslot_t<RType, ArgTypes...>(slot)); }
// disconnect all connections from signal
template<typename... ArgTypes>
static inline void disconnect(signal<ArgTypes...>& sig) noexcept
{ sig.disconnect(); }
// disconnect all connections from signal to object
template<typename... ArgTypes>
static inline void disconnect(signal<ArgTypes...>& sig, Object* obj) noexcept
{ sig.disconnect(obj); }
// enqueue a function call without a signal
template<class ObjType, typename RType, typename... ArgTypes>
static inline bool singleShot(ObjType* obj, mslot_t<ObjType, RType, ArgTypes...> slot, ArgTypes&... args) noexcept
{
if(obj->valid() && // ensure object is valid
Application::ms_signal_queue.lock()) // multithread protection
{
Application::ms_signal_queue.emplace(std::bind(slot, obj, std::forward<ArgTypes>(args)...));
Application::step(); // inform execution stepper
return Application::ms_signal_queue.unlock();
}
return false;
}
template<typename RType, template<typename, typename...> class FuncType, typename... ArgTypes>
static inline bool singleShot(FuncType<RType, ArgTypes...> slot, ArgTypes&... args) noexcept
{
if(Application::ms_signal_queue.lock()) // multithread protection
{
Application::ms_signal_queue.emplace(std::bind(slot, std::forward<ArgTypes>(args)...));
Application::step(); // inform execution stepper
return Application::ms_signal_queue.unlock();
}
return false;
}
// enqueue a call to the functions connected to the signal with /copies/ of the arguments
template<template<typename, typename...> class FuncType, typename... ArgTypes>
static inline bool singleShot_copy(FuncType<ArgTypes...>& sig, ArgTypes... args) noexcept
{ return singleShot(sig, args...); }
template<class ObjType, typename RType, typename... ArgTypes>
static inline bool singleShot_copy(ObjType* obj, mslot_t<ObjType, RType, ArgTypes...> slot, ArgTypes... args) noexcept
{ return singleShot(obj, slot, args...); }
// enqueue a call to the functions connected to the signal
template<typename... ArgTypes>
static inline bool enqueue(signal<ArgTypes...>& sig, ArgTypes&... args) noexcept
{ return sig.invocation(args...);}
// enqueue a call to the functions connected to the signal with /copies/ of the arguments
template<typename... ArgTypes>
static inline bool enqueue_copy(signal<ArgTypes...>& sig, ArgTypes... args) noexcept
{ return sig.invocation(args...);}
void operator delete(void* ptr) noexcept
{
static_cast<ProtoObject*>(ptr)->self = nullptr; // invalidate object
if(Application::ms_signal_queue.lock()) // multithread protection
{
Application::ms_signal_queue.emplace([ptr](void) { ::operator delete(ptr); });
Application::ms_signal_queue.unlock();
}
}
};
#endif // OBJECT_H