Reputation: 55
I am trying to implement a Signal
template in C++.
Here is what I have so far:
Main.cpp
:
//Developed by Trofimov Yaroslav on 02.04.18
#include <iostream>
#include "Signal.h"
void f1() {
std::cout << "here in f1" << std::endl;
}
void f2() {
std::cout << "F2 F2 F2" << std::endl;
}
typedef void (* VoidResultDelegate)();
int main(void) {
Signal<VoidResultDelegate> signalVoid;
signalVoid.addListener(f1);
signalVoid.addListener(f1);
signalVoid.invoke();
signalVoid.removeListener(f2);
signalVoid.invoke();
return 0;
}
Signal.h
:
//Developed by Trofimov Yaroslav on 02.04.18
#ifndef _SIGNAL_H_TROFIMOV_
#define _SIGNAL_H_TROFIMOV_
#include "LinkedList.h"
template<typename FunctionType>
class Signal {
LinkedList<FunctionType> _delegates;
public:
Signal<FunctionType>(void)
: _delegates(LinkedList<FunctionType>()) {
}
~Signal<FunctionType>(void) {
}
void addListener(const FunctionType& delegated) {
_delegates.add(delegated);
}
void removeListener(const FunctionType& delegated) {
_delegates.remove(delegated);
}
void invoke() {
_delegates.startIteration();
while(_delegates.hasNext()) {
_delegates.next()();
}
}
};
#endif
LinkedList.h
:
//Developed by Trofimov Yaroslav on 30.03.2018
#ifndef _LINKED_LIST_H_TROFIMOV_
#define _LINKED_LIST_H_TROFIMOV_
#include <string>
#include <iostream>
#include <typeinfo>
template<typename T>
class LinkedList {
template<typename T>
struct Node {
T _data;
Node* _next;
Node()
: _next(0){}
~Node<T>() {
if(_next) {
delete _next; _next = 0;
}
}
};
Node<T>* _head;
Node<T>* _tail;
Node<T>* _iterator;
public:
LinkedList<T>()
: _head(0), _tail(0), _iterator(0) {
};
~LinkedList<T>() {
delete _head; _head = 0;
}
void add(const T& element) {
if(!_head) {
_head = new Node<T>;
_head->_data = element;
_tail = _head;
return;
}
Node<T>* newNode = new Node<T>;
newNode->_data = element;
_tail->_next = newNode;
_tail = newNode;
return;
}
void remove(const T& element) {
if(!_head) {
return;
}
if(_head->_data == element) {
_head = _head->_next;
return;
}
Node<T>* previous = _head;
Node<T>* current = _head->_next;
while(current) {
if(current->_data == element) {
previous->_next = current->_next;
return;
}
previous = current;
current = current->_next;
}
}
void startIteration() {
_iterator = _head;
}
bool hasNext() {
return (_iterator)?true:false;
}
T& next() {
T& res = _iterator->_data;
_iterator = _iterator->_next;
return res;
}
};
#endif
So, what I would like to add is a generic way to pass parameters. Suppose, that now instead of typedef void (* VoidResultDelegate)();
I have typedef void (* VoidResultDelegate)(int i);
that means I would like int
parameter to appear somehow in the Signal::invoke
methods parameter list and be passed here _delegates.next()();
this way _delegates.next()(i);
or something alike.
Is it possible at all in C++?
What I am thinking about is passing another typename
argument to the Signal
which would denote the type of the argument accepted by the Signal::invoke
and passed to the elements in the linked list in the call _delegates.next()();
. But the problem with such an approach that I can not control the number of the arguments (it will be only one argument). And no one (by no one I mean a compiler of course) enforces me to pass the correct parameter as a typename
to the Signal
template. In the example above I could have passed bool
typename
instead of int
and no one would notice it until the error.
Here is the updated after the answer version:
Main.cpp
:
//Developed by Trofimov Yaroslav on 02.04.18
#include <iostream>
#include "Signal.h"
void f1(int i) {
std::cout << "here in f1" << std::endl;
}
void f2(int i) {
std::cout << "F2 F2 F2" << std::endl;
}
typedef void (* VoidResultDelegate)(int i);
int main(void) {
Signal<VoidResultDelegate, int> signalVoid;
signalVoid.addListener(f1);
signalVoid.addListener(f2);
signalVoid.invoke(-1);
signalVoid.removeListener(f1);
signalVoid.invoke(-1);
return 0;
}
Signal.h
:
//Developed by Trofimov Yaroslav on 02.04.18
#ifndef _SIGNAL_H_TROFIMOV_
#define _SIGNAL_H_TROFIMOV_
#include "LinkedList.h"
template<typename FunctionType, typename... Args>
class Signal {
LinkedList<FunctionType> _delegates;
public:
Signal<FunctionType, parameter>(void)
: _delegates(LinkedList<FunctionType>()) {
}
~Signal<FunctionType, parameter>(void) {
}
void addListener(const FunctionType& delegated) {
_delegates.add(delegated);
}
void removeListener(const FunctionType& delegated) {
_delegates.remove(delegated);
}
void invoke(Args&& ... args) {
_delegates.startIteration();
while(_delegates.hasNext()) {
(_delegates.next())(std::forward<Args>(args)...);
}
}
};
#endif
LinkedList.h
:
//Developed by Trofimov Yaroslav on 30.03.2018
#ifndef _LINKED_LIST_H_TROFIMOV_
#define _LINKED_LIST_H_TROFIMOV_
#include <string>
#include <iostream>
#include <typeinfo>
template<typename T>
class LinkedList {
template<typename T>
struct Node {
T _data;
Node* _next;
Node()
: _next(0){}
~Node<T>() {
if(_next) {
delete _next; _next = 0;
}
}
};
Node<T>* _head, _tail, _iterator;
public:
LinkedList<T>()
: _head(0), _tail(0), _iterator(0) {
};
~LinkedList<T>() {
delete _head; _head = 0;
}
void add(const T& element) {
if(!_head) {
_head = new Node<T>;
_head->_data = element;
_tail = _head;
return;
}
Node<T>* newNode = new Node<T>;
newNode->_data = element;
_tail->_next = newNode;
_tail = newNode;
return;
}
void remove(const T& element) {
if(!_head) {
return;
}
if(_head->_data == element) {
_head = _head->_next;
return;
}
Node<T>* previous = _head;
Node<T>* current = _head->_next;
while(current) {
if(current->_data == element) {
previous->_next = current->_next;
return;
}
previous = current;
current = current->_next;
}
}
void startIteration() {
_iterator = _head;
}
bool hasNext() {
return (_iterator)?true:false;
}
T& next() {
T& res = _iterator->_data;
_iterator = _iterator->_next;
return res;
}
};
#endif
Upvotes: 0
Views: 46
Reputation: 12928
You can make a partial specialization for your Signal
class to get the return type and arguments in separate template parameters.
// Declare the template without any definition
template<typename FunctionType>
class Signal;
// Add partial specialization
template<typename ReturnType, typename... Args>
class Signal<ReturnType(*)(Args...)> {
// Now you have access to return type and arguments
// Other things...
void invoke(Args... args) {
_delegates.startIteration();
while(_delegates.hasNext()) {
_delegates.next()(args...);
}
}
}
I left out perfect forwarding from the invoke
function to keep it simple. With it it would look like this.
void invoke(Args&&... args) {
_delegates.startIteration();
while(_delegates.hasNext()) {
_delegates.next()(std::forward<Args>(args)...);
}
}
Upvotes: 1