summaryrefslogtreecommitdiffstats
path: root/test/signal_test.cpp
blob: a34ed217262d4e83766404c8bc2d6e199b191657 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "core/signal.hpp"
#include "core/task.hpp"
#include "debug.hpp"

#include <iostream>


using namespace flat::core;

struct custom_type
{
    const char f;

    custom_type(char _f) : f(_f) {
        npdebug("instanciated custom_type");
    }

    custom_type(const custom_type& other) : f(other.f) {
        npdebug("copy constructed custom_type (bad)");
    }

    custom_type(custom_type&& other) : f(other.f) {
        npdebug("move constructed custom_type (efficient)");
    }

    ~custom_type() {
        npdebug("deleted custom_type instance");
    }
};


void got_signal(int x, double y) {
    std::cout << "got signal with x=" <<  x << " and y=" << y << std::endl;
}


class test_emitter
{
private:
    channel& m_chan; 

public:
    test_emitter(channel& ch) : m_chan(ch) {}

    void send_str(const std::string& msg) {
        std::cout << "emitting signal with msg=" << msg << std::endl;
        m_chan.emit(signal(msg));
    }

    void send_num(int n) {
        std::cout << "emitting signal with n=" << n << std::endl;
        m_chan.emit(signal(n));
    }

    void send_custom(const custom_type& c) {
        std::cout << "emitting custom_type" << std::endl;
        m_chan.emit(signal(c));
    }
};


class test_listener
{
private:
    template<typename ...Ts>
    using listener_of = typename std::shared_ptr<listener<Ts...>>;

    listener_of<std::string> str_lis;
    listener_of<int> num_lis;
    listener_of<custom_type> cus_lis;

public:
    test_listener(channel& ch)
        : str_lis(ch.connect(&test_listener::got_string, this)),
          num_lis(ch.connect(&test_listener::got_number, this)),
          cus_lis(ch.connect(&test_listener::got_custom, this))
    {}

    void got_string(std::string msg) {
        std::cout << "got signal with msg=" << msg << std::endl;
    }

    void got_number(int n) {
        std::cout << "got signal with n=" << n << std::endl;
    }

    void got_custom(custom_type c) {
        std::cout << "got signal with custom_type" << std::endl;
    }
};


int main() {
    // create a job to propagate signals
    job broadcaster;
    
    // create a channel
    channel chan(broadcaster);

    // test with a function
    auto fun_lis = chan.connect(got_signal);
    std::cout << "emitting signal with x=100, y=293.0" << std::endl;
    chan.emit(signal(100, 293.0));

    // test with a closure
    // TODO: fix
    // auto lambda_lis = chan.connect([](char a) {
    //     npdebug("got signal with a=", a);
    // });
    // chan.emit(signal('f'));

    // call job to propagate signals
    broadcaster();

    // test with members
    test_emitter e(chan);
    test_listener l(chan);

    e.send_str("hello world!");
    e.send_num(42);
    e.send_custom(custom_type('e'));

    // call job to propagate signals
    broadcaster();

    return 0;
}