diff options
author | ancarola <raffaele.ancarola@epfl.ch> | 2019-01-22 23:52:04 +0100 |
---|---|---|
committer | ancarola <raffaele.ancarola@epfl.ch> | 2019-01-22 23:52:04 +0100 |
commit | 23a4f6aa162cf42eae31cc6bb4486949f4aaafa7 (patch) | |
tree | 51f1abe2b3ab642784d30cd8682100e69d77d78f /engine/include | |
parent | Add include headers of libraries (diff) | |
download | flatland-23a4f6aa162cf42eae31cc6bb4486949f4aaafa7.tar.gz flatland-23a4f6aa162cf42eae31cc6bb4486949f4aaafa7.zip |
Here we go
Diffstat (limited to 'engine/include')
-rw-r--r-- | engine/include/core/object.hpp (renamed from engine/include/object.hpp) | 0 | ||||
-rw-r--r-- | engine/include/core/priority.hpp | 63 | ||||
-rw-r--r-- | engine/include/core/signal.hpp | 108 | ||||
-rw-r--r-- | engine/include/core/task.hpp (renamed from engine/include/task.hpp) | 0 | ||||
-rw-r--r-- | engine/include/signal.hpp | 112 |
5 files changed, 171 insertions, 112 deletions
diff --git a/engine/include/object.hpp b/engine/include/core/object.hpp index 1992d2a..1992d2a 100644 --- a/engine/include/object.hpp +++ b/engine/include/core/object.hpp diff --git a/engine/include/core/priority.hpp b/engine/include/core/priority.hpp new file mode 100644 index 0000000..7a23803 --- /dev/null +++ b/engine/include/core/priority.hpp @@ -0,0 +1,63 @@ +#pragma once + +#include <set> + +namespace flat +{ + namespace core + { + enum class prior_t : unsigned + { + max = 0, + higher = 1, + high = 2, + none = 3, + low = 4, + lower = 5, + min = 6, + }; + + class prioritized + { + const prior_t m_priority; + + public: + + prioritized(prior_t m_priority = prior_t::none) : m_priority(m_priority) {} + + const prior priority() const + { + return m_priority; + } + }; + + struct prior_criteria + { + bool operator()(const prioritized& a, const prioritized& b) const + { + return a.priority() < b.priority(); + } + }; + + /* Compiler will complain if don't pass a non prioritized object */ + + template <class T> + using prior_set = std::set<T, prior_criteria>; + + + /* Equivalent but with pointers */ + + struct prior_ptr_criteria + { + bool operator()(const prioritized* a, const prioritized* b) const + { + return a->priority() < b->priority(); + } + }; + + /* Compiler will complain if don't pass a non prioritized object */ + + template <class T> + using prior_ptr_set = std::set<T*, prior_criteria>; + } +}; diff --git a/engine/include/core/signal.hpp b/engine/include/core/signal.hpp new file mode 100644 index 0000000..1057f3f --- /dev/null +++ b/engine/include/core/signal.hpp @@ -0,0 +1,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; + }; + + } +} + diff --git a/engine/include/task.hpp b/engine/include/core/task.hpp index 9422a23..9422a23 100644 --- a/engine/include/task.hpp +++ b/engine/include/core/task.hpp diff --git a/engine/include/signal.hpp b/engine/include/signal.hpp deleted file mode 100644 index db6638b..0000000 --- a/engine/include/signal.hpp +++ /dev/null @@ -1,112 +0,0 @@ -#pragma once - -#include <map> -#include <list> -#include <set> -#include <initializer_list> -#include "object.hpp" -#include "task.hpp" -#include "types.h" -#include <functional> - -namespace flat -{ - namespace core - { - class signal; - class channel; - } -} - -class flat::core::signal : virtual public flat::core::object -{ - -public: - - flat::core::object * sender; - priority prior; - - signal( flat::core::object * sender, - const std::string& id = "", - priority prior = 5); - - /* Alias to flat::core::channel::emit() */ - bool emit(const std::string& channel) const; - - enum class priority : uint8_t - { - instant = 0, - highest, - high, - none, - low, - lowest - }; -}; - - -/* Channel class */ -class flat::core::channel : virtual public flat::core::object -{ - /* Post processing signal stacking */ - stack_t stack; - - /* Listeners list */ - std::list<flat::core::listener> listeners; - - /* Synchronous task checking for signals */ - task_s * checker; - - /* Channel mapping */ - static std::map<std::string, channel*> channels; - -public: - - flat::core::channel(const std::string& id = "", flat::core::task::priority prior = flat::core::task::priority::none); - ~flat::core::channel(); - - void emit(const flat::core::signal&); - - void connect(flat::core::listener_s*); - void disconnect(flat::core::listener_s*); - - static flat::core::channel * find_channel(const std::string&); - - void post_processing(void*); - - /* Functor for order comparison */ - struct sig_prior_cmp { - - bool operator()(const flat::core::signal&, const flat::core::signal&) const; - }; - - typedef std::set<flat::core::signal, sig_prior_cmp> stack_t; -}; - -/* Listener class */ -class flat::core::listener : virtual public flat::core::object -{ - std::list<std::string> filters; - - bool check_in_filters(const std::string&) const; - - std::function<void(const flat::core::object*)> m_callback; - - flat::core::channel * parent = 0; - -public: - - listener( std::function<void()> 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 flat::core::signal&); -}; - |