用C ++实现Signal模板

时间:2018-04-03 00:01:21

标签: c++ function types parameters signals

我正在尝试用C ++实现Signal模板。

这是我到目前为止所做的:

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

所以,我想添加的是传递参数的通用方法。假设现在而不是typedef void (* VoidResultDelegate)();我有typedef void (* VoidResultDelegate)(int i);这意味着我希望int参数以某种方式显示在Signal::invoke方法参数列表中并在此处传递{{1}这种方式_delegates.next()();或类似的东西。

C ++中是否可以使用?

我正在考虑的是将另一个_delegates.next()(i);参数传递给typename,它将表示Signal接受的参数的类型,并传递给链接列表中的元素。电话Signal::invoke。但是这种方法的问题是我无法控制参数的数量(它只会是一个参数)。并且没有人(当然,我的意思是编译器)强制我将_delegates.next()();作为typename传递给Signal模板。在上面的示例中,我可以通过bool typename而不是int,在错误发生之前没有人会注意到它。

以下是答案版本后的更新:

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

1 个答案:

答案 0 :(得分:1)

您可以对Signal类进行部分特化,以便在单独的模板参数中获取返回类型和参数。

// 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...);
        }
    }
}

我从invoke函数中遗漏了perfect forwarding以保持简单。有了它,它看起来就像这样。

void invoke(Args&&... args) {
    _delegates.startIteration();
    while(_delegates.hasNext()) {
        _delegates.next()(std::forward<Args>(args)...);
    }
}