summaryrefslogtreecommitdiffstats
path: root/engine/include/core/signal.hpp
blob: 8d7aeeff45ccb40f6e991a2da92ff7b12304c9f3 (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
128
129
130
131
132
133
134
#pragma once

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

namespace flat::core
{
    class signal : virtual public labelled, virtual public prioritized
    {
    
    public:

        struct package
        {
            package(void *data) : data(data) {}

            template<class T>
            T * get() {

                return reinterpret_cast<T*>(data);
            }

            void * data;
        };
    
        object *m_sender;
        package m_package;
    
        signal(object * sender, 
               const std::string& id = "", 
               void * data = 0,
               priority_t prior = priority_t::none);
    };
        
    /* Listener class */
    class listener
    {
    public:

        using callback = std::function<void(const object*, signal::package)>;
        using ptr = std::shared_ptr<listener>;

        listener(callback 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 a functor */
        template<typename R, typename T>
        static ptr create(  R T::*mf,
                            T& obj,
                            const std::initializer_list<std::string>& filters = {}) {
            return std::make_shared<listener>(std::bind(mf, obj), filters);
        }

    private:

        callback m_callback;

        std::list<std::string> m_filters;
    
        bool check_in_filters(const std::string&) const;
    };

    /* Channel class */
    class channel : virtual public labelled, public std::enable_shared_from_this<channel>
    {
    private:
        /* Post processing signal stacking */
        queue<signal> m_stack;
    
        /* Listeners list */
        std::list<std::weak_ptr<listener>> m_listeners;
    
        /* Synchronous task checking for signals */
        task::ptr m_checker;
        
        /* Channel mapping */
        static std::map<std::string, std::weak_ptr<channel>> m_channels;

        bool m_mapped;
     
    public:

        using ptr = std::shared_ptr<channel>;
    
        channel(const std::string& id = "");
        ~channel();

        void start(priority_t task_priority = priority_t::none);
        bool map();
    
        void emit(const signal&);

        bool connect(listener::ptr l);
        void disconnect(listener::ptr l);

        bool connect(listener* l);
        void disconnect(listener* l);

        listener::ptr connect(listener::callback f,
            const std::initializer_list<std::string>& filters = {});

        template<typename R, typename T>
        inline listener::ptr connect(R T::*mf, T* obj,
            const std::initializer_list<std::string>& filters = {})
        {
            using namespace std::placeholders;
            return connect(std::bind(mf, obj, _1, _2), filters);
        }
       
        static ptr find(const std::string&); 

        static ptr create(const std::string& id, priority_t prior = priority_t::none);
    
        void check_and_call();
    };
}