Idea is to do signals and slots like behaviour without needing special macros or a moc pre-processor.
Observer pattern - perhaps should use observer terminology.
How to do:
require a map of slot name to slot function
eg string -> funcptr
usually moc will create this, but why not do it in a function that registers slots
and then why not call that at the time a signal and slot are being connected.
eg:
#define SIGNAL(x) #x
#define SLOT(x) #x, x
typedef void (*Func)(void);
typedef struct { Object *o, int id } Slot;
// standard base class for doing signals and slots
class Object
{
public:
Object() {
slotListCount = 0;
}
~Object() {
// deregister connections to this
}
void connect(Object *obj1, String s1, Object *obj2, String s2, Func f2) {
int slotId = obj2->registerSlot(s2, f2);
obj2->addConnection(s1, obj2, slotId);
}
void addConnection(String s1, Object *obj2, int slotId) {
Signal s = { obj2, slotId };
signalMap[s1] += s;
}
int registerSlot(String s, Func f) {
slotMap[s] = f;
slotList[slotListCount] = f;
slotListCount++;
return slotListCount;
}
void raiseSignal(String s) {
foreach (Slot slot, signalMap.find(s)) {
slot.o->invokeSlotById(slot.id);
}
}
void invokeSlotByName(String s) {
Func f = slotNameMap[s];
f();
}
void invokeSlotById(int id) {
Func f = slotList[id];
f();
}
Map<String,Slot> signalMap;
Map<String,Func> slotNameMap;
Func slotList[];
int slotListCount;
};
// class using signals and slots
class Foo : public Object
{
public:
Foo() {
connect(
}
~Foo() {
}
void someFunc() {
}
void barFunc() {
}
};