#define EASYDELEGATE_FORCE_INLINE
#include <set>
#include <unordered_set>
#include <iostream>
using namespace std;
unsigned int myStaticIntMethod(const char *str, const float &flt, const double &dbl)
{
cout << "myStaticIntMethod: " << str << "," << flt << "," << dbl << endl;
return 5;
}
void myStaticVoidMethod(const float &flt, const char *str, const double &dbl)
{
cout << "myStaticVoidMethod: " << flt << "," << str << "," << dbl << endl;
}
class MyCustomClass
{
public:
unsigned int myMemberMethod(const char *str, const float &flt, const double &dbl)
{
cout << "MyCustomClass::myMemberMethod: " << str << "," << flt << "," << dbl << endl;
return 2;
}
};
int main(int argc, char *argv[])
{
MyEventType myDelegateSet;
MyCustomClass *myCustomClassInstance = new MyCustomClass();
myDelegateSet.insert(myDelegateSet.end(), new MyEventType::StaticDelegateType(myStaticIntMethod));
myDelegateSet.insert(myDelegateSet.end(), new MyEventType::MemberDelegateType<MyCustomClass>(&MyCustomClass::myMemberMethod, myCustomClassInstance));
myDelegateSet += new MyEventType::StaticDelegateType(myStaticIntMethod);
cout << "------------- CALLING VIA .invoke() ---------------" << endl;
myDelegateSet.invoke("Foo", 3.14, 3.14159);
cout << "------------- CALLING VIA .invoke(), Getting Returns ---------------" << endl;
MyEventType::ReturnSetType myReturnValues;
myDelegateSet.invoke(myReturnValues, "Foo", 3.14, 3.14159);
for (auto it = myReturnValues.begin(); it != myReturnValues.end(); it++)
cout << *it << endl;
cout << "------- CUSTOM ITERATION --------" << endl;
for (auto it = myDelegateSet.begin(); it != myDelegateSet.end(); it++)
cout << (*it)->invoke("Foo", 3.14, 3.14159) << endl;
cout << "-------------- REMOVING STATIC LISTENERS -----------------" << endl;
myDelegateSet.removeDelegateByMethod(myStaticIntMethod);
myDelegateSet.invoke("Foo", 3.14, 3.14159);
cout << "-------------- REMOVING MEMBER LISTENERS -----------------" << endl;
myDelegateSet.push_back(new MyEventType::StaticDelegateType(myStaticIntMethod));
myDelegateSet.removeDelegateByMethod(&MyCustomClass::myMemberMethod);
myDelegateSet.invoke("Foo", 3.14, 3.14159);
cout << "-------------- REMOVING MEMBER LISTENERS VIA THIS -----------------" << endl;
myDelegateSet.push_back(new MyEventType::MemberDelegateType<MyCustomClass>(&MyCustomClass::myMemberMethod, myCustomClassInstance));
myDelegateSet.removeDelegateByThisPointer(myCustomClassInstance);
myDelegateSet.invoke("Foo", 3.14, 3.14159);
cout << "-------------- REMOVING DELEGATE VIA ADDRESS -----------------" << endl;
MyEventType::MemberDelegateType<MyCustomClass> *delegateToRemove = new MyEventType::MemberDelegateType<MyCustomClass>(&MyCustomClass::myMemberMethod, myCustomClassInstance);
myDelegateSet.push_back(delegateToRemove);
myDelegateSet.removeDelegate(delegateToRemove, false);
myDelegateSet.invoke("Foo", 3.14, 3.14159);
cout << "---------- Removed Delegate is still usable ------------" << endl;
delegateToRemove->invoke("Foo", 3.14, 3.14159);
#ifndef EASYDELEGATE_NO_DEFERRED_CALLING
cout << "---------- DEFERRED CALLERS ---------------" << endl;
MyCachedIntMemberDelegateType *cachedMemberDelegate = new MyCachedIntMemberDelegateType(&MyCustomClass::myMemberMethod, myCustomClassInstance, "Cached", 3.14, 3.14159);
MyCachedVoidStaticDelegateType *cachedStaticDelegate = new MyCachedVoidStaticDelegateType(myStaticVoidMethod, 8.15f, "Cached", 3.14f);
unordered_set<EasyDelegate::IDeferredCaller *> delegates;
delegates.insert(delegates.end(), cachedMemberDelegate);
delegates.insert(delegates.end(), cachedStaticDelegate);
for (auto it = delegates.begin(); it != delegates.end(); it++)
{
cout << "Invoking Delegate " << endl;
(*it)->genericDispatch();
}
#endif
MyEventType::StaticDelegateType staticDelegateReference(myStaticIntMethod);
VoidEventType::StaticDelegateType staticVoidDelegateReference(myStaticVoidMethod);
MyEventType::MemberDelegateType<MyCustomClass> memberDelegateReference(&MyCustomClass::myMemberMethod, myCustomClassInstance);
cout << (staticDelegateReference.hasSameMethodAs(&staticDelegateReference)) << endl;
cout << (staticDelegateReference.hasSameMethodAs(&memberDelegateReference)) << endl;
cout << (memberDelegateReference.hasSameMethodAs(&memberDelegateReference)) << endl;
cout << (memberDelegateReference.hasSameMethodAs(&staticDelegateReference)) << endl;
cout << (staticDelegateReference.hasSameMethodAs(&staticVoidDelegateReference)) << endl;
cout << (memberDelegateReference.hasSameMethodAs(&staticVoidDelegateReference)) << endl;
#ifndef EASYDELEGATE_NO_DEFERRED_CALLING
delete cachedMemberDelegate;
delete cachedStaticDelegate;
#endif
delete myCustomClassInstance;
return 0;
}