Last active
August 29, 2015 14:14
-
-
Save catchouli/ddad80b00272967ae1c7 to your computer and use it in GitHub Desktop.
Signal/Socket C++ test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Signal<int> sig; | |
sig.connect(staticFunctionTest); | |
sig.connect([](int i){ printf("lambda function test %d\n", i); }); | |
sig.connect(&test, &Test::test); | |
sig.emit(5); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template <typename... Args> | |
class Connection | |
{ | |
public: | |
virtual void emit(Args... args) = 0; | |
}; | |
template <typename... Args> | |
class Connection_FunctionPointer | |
: public Connection<Args...> | |
{ | |
public: | |
typedef void FunctionType(Args...); | |
typedef FunctionType* PointerType; | |
Connection_FunctionPointer(PointerType functionPtr) | |
: mFunctionPtr(functionPtr) | |
{ | |
} | |
void emit(Args... args) override | |
{ | |
mFunctionPtr(args...); | |
} | |
PointerType mFunctionPtr; | |
}; | |
template <typename T, typename... Args> | |
class Connection_MemberFunctionPointer | |
: public Connection<Args...> | |
{ | |
public: | |
typedef void (T::*PointerType)(Args...); | |
Connection_MemberFunctionPointer(T* objectPtr, PointerType functionPtr) | |
: mObjectPtr(objectPtr), mFunctionPtr(functionPtr) | |
{ | |
} | |
void emit(Args... args) override | |
{ | |
(mObjectPtr->*mFunctionPtr)(args...); | |
} | |
T* mObjectPtr; | |
PointerType mFunctionPtr; | |
}; | |
template <typename... Args> | |
class Signal | |
{ | |
public: | |
typedef Connection<Args...> ConnectionType; | |
void connect(typename Connection_FunctionPointer<Args...>::PointerType fp) | |
{ | |
mConnections.push_back(std::make_shared<Connection_FunctionPointer<Args...>>(fp)); | |
} | |
template <typename T> | |
void connect(T* objectPtr, typename Connection_MemberFunctionPointer<T, Args...>::PointerType fp) | |
{ | |
mConnections.push_back(std::make_shared<Connection_MemberFunctionPointer<T, Args...>>(objectPtr, fp)); | |
} | |
void emit(Args... args) | |
{ | |
for (auto& connection : mConnections) | |
{ | |
connection->emit(args...); | |
} | |
} | |
std::vector<std::shared_ptr<ConnectionType>> mConnections; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment