From 23a4f6aa162cf42eae31cc6bb4486949f4aaafa7 Mon Sep 17 00:00:00 2001 From: ancarola Date: Tue, 22 Jan 2019 23:52:04 +0100 Subject: Here we go --- engine/flatland.cpp | 2 +- engine/flatserial.cpp | 6 +-- engine/focusable.cpp | 4 +- engine/include/core/object.hpp | 42 +++++++++++++++ engine/include/core/priority.hpp | 63 ++++++++++++++++++++++ engine/include/core/signal.hpp | 108 +++++++++++++++++++++++++++++++++++++ engine/include/core/task.hpp | 38 +++++++++++++ engine/include/object.hpp | 42 --------------- engine/include/signal.hpp | 112 --------------------------------------- engine/include/task.hpp | 38 ------------- engine/object.cpp | 4 +- engine/signal.cpp | 62 +++++++++------------- 12 files changed, 285 insertions(+), 236 deletions(-) create mode 100644 engine/include/core/object.hpp create mode 100644 engine/include/core/priority.hpp create mode 100644 engine/include/core/signal.hpp create mode 100644 engine/include/core/task.hpp delete mode 100644 engine/include/object.hpp delete mode 100644 engine/include/signal.hpp delete mode 100644 engine/include/task.hpp (limited to 'engine') 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(this, &SDL_EventCollector::collect, 0, true, 0); + checker = new flat::core::task(this, &SDL_EventCollector::collect, 0, true, 0); /* Eraser task, post-process, minimum priority */ - eraser = new FlatTask(this, &SDL_EventCollector::erase, 0, false, 0xff); + eraser = new flat::core::task(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(this, &Focusable::serial_precall, 0); + event_trigger = new flat::core::task(this, &Focusable::serial_precall, 0); } Focusable::~Focusable() diff --git a/engine/include/core/object.hpp b/engine/include/core/object.hpp new file mode 100644 index 0000000..1992d2a --- /dev/null +++ b/engine/include/core/object.hpp @@ -0,0 +1,42 @@ +#pragma once + +#include +#include +#include +#include + +#include "types.hpp" + +namespace flat +{ + namespace core + { + class object; + } +} + +class flat::core::object +{ + std::string id; + + /* Common list of objects */ + static std::list all_objects; + +public: + + object(); + ~object(); + + void set_id(const std::string&); + + const std::string& get_id() const; + + /* Static accessors to allObject */ + + static bool is_allocated(object*); + + static std::vector& get_by_id(const std::string& id, std::vector&); + + static std::string random_id(uint8_t length = 8); +}; + 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 + +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 + using prior_set = std::set; + + + /* 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 + using prior_ptr_set = std::set; + } +}; 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 +#include +#include +#include +#include "object.hpp" +#include "task.hpp" +#include "types.h" +#include +#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 stack; + + /* Listeners list */ + std::list listeners; + + /* Synchronous task checking for signals */ + task * checker; + + /* Channel mapping */ + static std::map 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 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& 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 + static listener create( const (T::*method(const object*, void*))& fct, + T* ptr, + const std::initializer_list& filters = {}) + { + return listener(std::bind(fct, ptr), filters); + } + + typedef std::function callback_t; + }; + + } +} + diff --git a/engine/include/core/task.hpp b/engine/include/core/task.hpp new file mode 100644 index 0000000..9422a23 --- /dev/null +++ b/engine/include/core/task.hpp @@ -0,0 +1,38 @@ +#pragma once + +#include +#include + +namespace flat { + namespace core { + class task { + public: + enum class priority : unsigned { + max = 0, + high = 1, + none = 2, + low = 3, + min = 4, + }; + + // to pass a member function (method) use std::bind(f, obj) + task(std::function callback, priority p = priority::none); + task() = delete; + + inline void operator()() const { m_callback(); } + friend bool operator<(const task& lhs, const task& rhs); + + private: + const priority m_priority; + std::function m_callback; + }; + + struct job : public std::multiset { + inline void add_task(task t) { + this->insert(t); + } + + void invoke_tasks(); + }; + } +} \ No newline at end of file diff --git a/engine/include/object.hpp b/engine/include/object.hpp deleted file mode 100644 index 1992d2a..0000000 --- a/engine/include/object.hpp +++ /dev/null @@ -1,42 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -#include "types.hpp" - -namespace flat -{ - namespace core - { - class object; - } -} - -class flat::core::object -{ - std::string id; - - /* Common list of objects */ - static std::list all_objects; - -public: - - object(); - ~object(); - - void set_id(const std::string&); - - const std::string& get_id() const; - - /* Static accessors to allObject */ - - static bool is_allocated(object*); - - static std::vector& get_by_id(const std::string& id, std::vector&); - - static std::string random_id(uint8_t length = 8); -}; - 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 -#include -#include -#include -#include "object.hpp" -#include "task.hpp" -#include "types.h" -#include - -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 listeners; - - /* Synchronous task checking for signals */ - task_s * checker; - - /* Channel mapping */ - static std::map 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 stack_t; -}; - -/* Listener class */ -class flat::core::listener : virtual public flat::core::object -{ - std::list filters; - - bool check_in_filters(const std::string&) const; - - std::function m_callback; - - flat::core::channel * parent = 0; - -public: - - listener( std::function m_callback, - const std::initializer_list& 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/include/task.hpp b/engine/include/task.hpp deleted file mode 100644 index 9422a23..0000000 --- a/engine/include/task.hpp +++ /dev/null @@ -1,38 +0,0 @@ -#pragma once - -#include -#include - -namespace flat { - namespace core { - class task { - public: - enum class priority : unsigned { - max = 0, - high = 1, - none = 2, - low = 3, - min = 4, - }; - - // to pass a member function (method) use std::bind(f, obj) - task(std::function callback, priority p = priority::none); - task() = delete; - - inline void operator()() const { m_callback(); } - friend bool operator<(const task& lhs, const task& rhs); - - private: - const priority m_priority; - std::function m_callback; - }; - - struct job : public std::multiset { - inline void add_task(task t) { - this->insert(t); - } - - void invoke_tasks(); - }; - } -} \ No newline at end of file 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::get_by_id(const string& id, vector& 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 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( 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 m_callback, - const std::initializer_list& filters) +listener::listener( callback_t m_callback, + const initializer_list& 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); } -- cgit v1.2.1