summaryrefslogtreecommitdiffstats
path: root/engine/include/core/signal.hpp
blob: 1057f3f802a86e336acccc7865bbc5c12623c59c (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
#pragma once

#include <map>
#include <list>
#include <set>
#include <initializer_list>
#include "object.hpp"
#include "task.hpp"
#include "types.h"
#include <functional>
#include "priority.hpp"

namespace flat
{
    namespace core
    {

    class signal : virtual public object, public prioritized
    {
    
    public:
    
        object * sender;
        void * data; // TODO, avoid this void pointer
        priority prior;
    
        signal(     object * sender, 
                    const std::string& id = "", 
                    void * data,
                    prior_t prior = prior_t::none);

        /* Alias to flat::core::channel::emit() */
        bool emit(const std::string& channel) const;
    };

    
    /* Channel class */
    class channel : virtual public object
    {
        /* Post processing signal stacking */
        prior_set<signal> stack;
    
        /* Listeners list */
        std::list<listener*> listeners;
    
        /* Synchronous task checking for signals */
        task * checker;
        
        /* Channel mapping */
        static std::map<std::string, channel*> channels;    
     
    public:
    
        channel(const std::string& id = "", prior_t priority = prior_t::none);
        ~channel();
    
        void emit(const signal&);

        void connect(const listener*);
        void disconnect(const listener*);
       
        static channel * find_channel(const std::string&); 
    
        void post_processing();
    };
    
    /* Listener class */
    class listener : virtual public object
    {

        std::list<std::string> filters;
    
        bool check_in_filters(const std::string&) const;
    
        callback_t m_callback;

        channel * parent;
    
    public:
    
        listener(   callback_t m_callback,
                    const std::initializer_list<std::string>& filters = {});
    
        ~listener();

        void add_filter(const std::string&);
        void remove_filter(const std::string&);

        bool connect(const std::string&);
        bool disconnect(const std::string&);

        void invoke(const signal&);

        /* Allow to safely bind e functor */
        template<class T>
        static listener create(  const (T::*method(const object*, void*))& fct,
                                  T* ptr,
                                  const std::initializer_list<std::string>& filters = {})
        {
            return listener(std::bind(fct, ptr), filters);
        }

        typedef std::function<void(const object*, void*)> callback_t;
    };

    }
}