diff options
Diffstat (limited to 'engine')
-rw-r--r-- | engine/flatland.cpp | 2 | ||||
-rw-r--r-- | engine/flatserial.cpp | 6 | ||||
-rw-r--r-- | engine/focusable.cpp | 4 | ||||
-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 | ||||
-rw-r--r-- | engine/object.cpp | 4 | ||||
-rw-r--r-- | engine/signal.cpp | 62 |
10 files changed, 205 insertions, 156 deletions
diff --git a/engine/flatland.cpp b/engine/flatland.cpp index 628331b..97829a4 100644 --- a/engine/flatland.cpp +++ b/engine/flatland.cpp @@ -9,7 +9,7 @@ using namespace std; -#include "flattask.h" +#include "task.hpp" #include "signal.h" #include "flatwindow.h" #include "flatexception.h" diff --git a/engine/flatserial.cpp b/engine/flatserial.cpp index 7259bf1..826e001 100644 --- a/engine/flatserial.cpp +++ b/engine/flatserial.cpp @@ -1,13 +1,13 @@ #include "flatserial.h" -#include "flattask.h" +#include "task.hpp" SDL_EventCollector::SDL_EventCollector() { /* Checker task, pre-process, maximum priority */ - checker = new FlatTask<SDL_EventCollector>(this, &SDL_EventCollector::collect, 0, true, 0); + checker = new flat::core::task<SDL_EventCollector>(this, &SDL_EventCollector::collect, 0, true, 0); /* Eraser task, post-process, minimum priority */ - eraser = new FlatTask<SDL_EventCollector>(this, &SDL_EventCollector::erase, 0, false, 0xff); + eraser = new flat::core::task<SDL_EventCollector>(this, &SDL_EventCollector::erase, 0, false, 0xff); } SDL_EventCollector::~SDL_EventCollector() diff --git a/engine/focusable.cpp b/engine/focusable.cpp index df26ea9..d6fa84b 100644 --- a/engine/focusable.cpp +++ b/engine/focusable.cpp @@ -1,10 +1,10 @@ #include "serial/focusable.h" -#include "flattask.h" +#include "task.hpp" #include "flatserial.h" Focusable::Focusable(bool focused) : focused(focused) { - event_trigger = new FlatTask<Focusable>(this, &Focusable::serial_precall, 0); + event_trigger = new flat::core::task<Focusable>(this, &Focusable::serial_precall, 0); } Focusable::~Focusable() 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&); -}; - diff --git a/engine/object.cpp b/engine/object.cpp index 990c403..b6a22fd 100644 --- a/engine/object.cpp +++ b/engine/object.cpp @@ -33,7 +33,7 @@ string object::random_id(uint8_t length) { string out; - for (Uint8 i = 0; i < length; ++i) + for (uint8_t i = 0; i < length; ++i) out += (char)(rand() % 93 + 33); return out; @@ -54,7 +54,7 @@ vector<object*>& object::get_by_id(const string& id, vector<object*>& l) { for (object * obj : object::all_objects) { - if (id == obj->getID()) + if (id == obj->get_id()) l.push_back(obj); } diff --git a/engine/signal.cpp b/engine/signal.cpp index 98f133e..5f0b356 100644 --- a/engine/signal.cpp +++ b/engine/signal.cpp @@ -1,17 +1,11 @@ #include "signal.hpp" -#include "task.hpp" using namespace std; using namespace flat::core; -bool channel::sig_prior_cmp::operator()(const signal& s, const signal& g) const -{ - return s.prior <= g.prior; -} - map<string, channel*> channel::channels; -channel::channel(const string& id, task::priority prior) +channel::channel(const string& id, prior_t prior) { channel * other = find_channel(id); @@ -20,10 +14,9 @@ channel::channel(const string& id, task::priority prior) ; /* Initialize task, post-process, fifth priority */ - checker = new task<channel>( this, - &channel::post_processing, - 0, - prior); + checker = new task( this, + &channel::post_processing, + prior); string ID = (id.empty()) ? object::random_id() : id; @@ -42,18 +35,10 @@ channel::~channel() void channel::emit(const signal& sig) { - if (!sig.priority) { - - /* Execute immediately */ - for (auto lis : listeners) - lis->execute(sig); - - } else - /* Insert in order of priority */ - stack.insert(sig); + stack.insert(sig); } -void channel::connect(listener* l) +void channel::connect(const listener* l) { /* Control not to double */ for (auto lis : listeners) @@ -65,7 +50,7 @@ void channel::connect(listener* l) listeners.push_back(l); } -void channel::disconnect(listener* l) +void channel::disconnect(const listener* l) { listeners.remove(l); } @@ -81,15 +66,16 @@ channel * channel::find_channel(const string& id) return (it == channels.end()) ? 0 : (*it).second; } -void channel::post_processing(void*) +void channel::post_processing() { if (!stack.empty()) { + // TODO, maybe it exists pop /* for each listener_s, catch signal */ for (const auto& signal : stack) { for (auto l : listeners) - l->execute(signal); + l->invoke(signal); } stack.clear(); @@ -100,7 +86,11 @@ void channel::post_processing(void*) /* signal class */ -signal::signal(flat::core::object *sender, const string& id, void *data, Uint8 priority) +signal::signal( object *sender, + const string& id, + void *data, + prior_t priority) + : sender(sender), data(data), priority(priority) { set_id(id); @@ -123,10 +113,10 @@ bool signal::emit(const string& channel) const /* listener_s class */ -listener::listener( std::function<void()> m_callback, - const std::initializer_list<std::string>& filters) +listener::listener( callback_t m_callback, + const initializer_list<string>& filters) - : m_callback(m_callback), filters(filters) + : m_callback(m_callback), filters(filters), parent(0) { } @@ -137,7 +127,7 @@ listener::~listener() parent->disconnect(this); } -bool listener::check_in_filters(const std::string& filter) const +bool listener::check_in_filters(const string& filter) const { for (const auto& f : filters) { @@ -148,17 +138,17 @@ bool listener::check_in_filters(const std::string& filter) const return false; } -void listener::add_filter(const std::string& f) +void listener::add_filter(const string& f) { filters.push_back(f); } -void listener::remove_filter(const std::string& f) +void listener::remove_filter(const string& f) { filters.remove(f); } -bool listener::connect(const std::string& chan) +bool listener::connect(const string& chan) { channel * c = channel::find_channel(chan); @@ -170,7 +160,7 @@ bool listener::connect(const std::string& chan) return c != 0; } -bool listener_s::disconnect(const std::string& chan) +bool listener_s::disconnect(const string& chan) { channel * c = channel::find_channel(chan); @@ -182,10 +172,10 @@ bool listener_s::disconnect(const std::string& chan) return c != 0; } -void listener::invoke(const flat::core::signal&) +void listener::invoke(const signal&) { - if ((!sig.get_id().empty() && check_in_filters(sig.get_id())) || - (sig.get_id().empty() && filters.empty())) + if ( (!sig.get_id().empty() && check_in_filters(sig.get_id())) || + (sig.get_id().empty() && filters.empty())) m_callback(sig.sender); } |