Newer
Older
invertedlogic / InvertedLogic / iLFramework / toolkit / src / SignalSlot.cpp
@John Ryland John Ryland on 10 Nov 2019 3 KB rename
#include <iostream>
#include <fstream>
#include "SignalSlot.h"
#include "Test.h"


BEGIN_NAMESPACE


int funcCalledCount = 0;
int funcCalledIndex = 0;
int funcParameter = 0;
String funcParameterPtr;


class A
{
public:
	virtual ~A() {}

    virtual int foo(int p)
    {
		funcCalledCount++; funcCalledIndex = 1; funcParameter = p; funcParameterPtr = "0";
		return 0;
    }

	virtual int bar(const char* p)
    {
		funcCalledCount++; funcCalledIndex = 2; funcParameter = 0;
		if (p)
			funcParameterPtr = p;
		return 0;
    }

    virtual int toot(int p)
    {
		funcCalledCount++; funcCalledIndex = 3; funcParameter = p; funcParameterPtr = "0";
		return 0;
    }

	void test(int p)
	{
		funcCalledCount++; funcCalledIndex = 4; funcParameter = p; funcParameterPtr = "0";
		testWasCalled();
	}

	Signal testWasCalled;
};


class B : public A
{
public:
    virtual int foo(int p)
    {
		funcCalledCount++; funcCalledIndex = 5; funcParameter = p; funcParameterPtr = "0";
		return 0;
    }
};


class C
{
public:
    virtual int foo2(int p)
	{
		funcCalledCount++; funcCalledIndex = 6; funcParameter = p; funcParameterPtr = "0";
		return 0;
	}
};


class D : public C, public A
{
public:
    virtual int foo(int p)
    {
		funcCalledCount++; funcCalledIndex = 7; funcParameter = p; funcParameterPtr = "0";
		return 0;
    }
};


void foo(int p)
{
	funcCalledCount++; funcCalledIndex = 8; funcParameter = p; funcParameterPtr = "0";
}


UNIT_TEST(SignalSlotTests, 0)
{
	A *a = new A();
	A *b = new B();
	A *d = new D();
	
	//static function
	Slot d1(&foo);
	
	//member function
	Slot d2(a, &A::foo);
	
	//member function + automatic variant casting
	Slot d3(b, &A::bar);

	//member function + subclass instance
	Slot d3b(b, &A::foo);

	//member function + subclass instance + multiple inheritance
	Slot d4(d, &A::foo);
	
	d1(100); //"foo(100)"
	
	CHECK(funcCalledCount == 1);
	CHECK(funcCalledIndex == 8);
	CHECK(funcParameter == 100);
	CHECK(funcParameterPtr == "0");

	d2(200); //"A::foo(200)"

	CHECK(funcCalledCount == 2);
	CHECK(funcCalledIndex == 1);
	CHECK(funcParameter == 200);
	CHECK(funcParameterPtr == "0");

	d3(-3.010f); //"A::bar("-3.01")"

	CHECK(funcCalledCount == 3);
	CHECK(funcCalledIndex == 2);
	CHECK(funcParameter == 0);
	CHECK(funcParameterPtr == "-3.01");
	//printf("param: --%s--\n", funcParameterPtr.c_str());

	d3b(300); //"B::foo("300")"

	CHECK(funcCalledCount == 4);
	CHECK(funcCalledIndex == 5);
	CHECK(funcParameter == 300);
	CHECK(funcParameterPtr == "0");
	
	d4(400); //"D::foo(400)"

	CHECK(funcCalledCount == 5);
	CHECK(funcCalledIndex == 7);
	CHECK(funcParameter == 400);
	CHECK(funcParameterPtr == "0");

	connect(a->testWasCalled, d1);
	connect(a->testWasCalled, a, &A::toot);
	Slot s0(a, &A::foo, 100);
	connect(a->testWasCalled, s0);//
	Slot s1(a, &A::test);
	connect(a->testWasCalled, s1);

	SharedPtr<Slot> s2(new Slot(d3));
	a->testWasCalled.addConnection(s2);//
	a->test(0);

	CHECK(funcCalledCount == 11);
	CHECK(funcCalledIndex == 2);
	CHECK(funcParameter == 0);
	CHECK(funcParameterPtr == "");
	//printf("param: --%s--\n", funcParameterPtr.c_str());

	delete a;
	delete b;
	delete d;
}


END_NAMESPACE