From fd9ecd4e9c7cbe76bc60197a7183ac3ca4e8d0f8 Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Wed, 23 Jan 2019 00:30:10 +0100 Subject: Integrate prioritized into task, rename prior_t to priority_t There was a bunch of code in core/priority.hpp that has been reduced. It works the same as before, just with less stuff. task.cpp is buildable without errors with: $ ninja build/engine/task.o --- engine/include/core/priority.hpp | 60 ++++++++++++---------------------------- engine/include/core/signal.hpp | 6 ++-- engine/include/core/task.hpp | 19 ++++--------- engine/signal.cpp | 6 ++-- engine/task.cpp | 11 ++------ lib/libmm | 2 +- 6 files changed, 32 insertions(+), 72 deletions(-) diff --git a/engine/include/core/priority.hpp b/engine/include/core/priority.hpp index 5f03bc9..0f00006 100644 --- a/engine/include/core/priority.hpp +++ b/engine/include/core/priority.hpp @@ -2,12 +2,10 @@ #include -namespace flat -{ - namespace core - { - enum class prior_t : unsigned - { +namespace flat { + namespace core { + + enum class priority_t : unsigned { max = 0, higher = 1, high = 2, @@ -17,47 +15,23 @@ namespace flat min = 6, }; - class prioritized - { - const prior_t m_priority; + class prioritized { + private: + const priority_t m_priority; public: + prioritized(priority_t priority = priority_t::none) + : m_priority(priority) {} - prioritized(prior_t m_priority = prior_t::none) : m_priority(m_priority) {} - - const prior priority() const - { + const priority_t 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::multiset; - - - /* Equivalent but with pointers */ - - struct prior_ptr_criteria - { - bool operator()(const prioritized* a, const prioritized* b) const - { - return a->priority() < b->priority(); - } - }; + }; + + bool operator<(const prioritized& lhs, const prioritized& rhs) { + return lhs.priority() < rhs.priority(); + } + } +} - /* Compiler will complain if don't pass a non prioritized object */ - template - using prior_ptr_set = std::multiset; - } -}; diff --git a/engine/include/core/signal.hpp b/engine/include/core/signal.hpp index 1057f3f..755f3ed 100644 --- a/engine/include/core/signal.hpp +++ b/engine/include/core/signal.hpp @@ -22,12 +22,12 @@ namespace flat object * sender; void * data; // TODO, avoid this void pointer - priority prior; + priority_t prior; signal( object * sender, const std::string& id = "", void * data, - prior_t prior = prior_t::none); + priority_t prior = priority_t::none); /* Alias to flat::core::channel::emit() */ bool emit(const std::string& channel) const; @@ -51,7 +51,7 @@ namespace flat public: - channel(const std::string& id = "", prior_t priority = prior_t::none); + channel(const std::string& id = "", priority_t priority = priority_t::none); ~channel(); void emit(const signal&); diff --git a/engine/include/core/task.hpp b/engine/include/core/task.hpp index 9422a23..b6ddad1 100644 --- a/engine/include/core/task.hpp +++ b/engine/include/core/task.hpp @@ -1,34 +1,25 @@ #pragma once +#include "priority.hpp" + #include #include namespace flat { namespace core { - class task { + class task : public prioritized { 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; + task(std::function callback, priority_t p = priority_t::none); 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) { + inline auto add_task(task t) { this->insert(t); } diff --git a/engine/signal.cpp b/engine/signal.cpp index 5f0b356..3804e22 100644 --- a/engine/signal.cpp +++ b/engine/signal.cpp @@ -1,11 +1,11 @@ -#include "signal.hpp" +#include "core/signal.hpp" using namespace std; using namespace flat::core; map channel::channels; -channel::channel(const string& id, prior_t prior) +channel::channel(const string& id, priority_t prior) { channel * other = find_channel(id); @@ -89,7 +89,7 @@ void channel::post_processing() signal::signal( object *sender, const string& id, void *data, - prior_t priority) + priority_t priority) : sender(sender), data(data), priority(priority) { diff --git a/engine/task.cpp b/engine/task.cpp index c09fa8b..f879530 100644 --- a/engine/task.cpp +++ b/engine/task.cpp @@ -1,17 +1,12 @@ -#include "task.hpp" +#include "core/task.hpp" #include namespace flat { namespace core { - bool operator<(const task& lhs, const task& rhs) { - // task::priority::max is 0, a higher number is lower priority - return lhs.m_priority < rhs.m_priority; - } - - task::task(std::function callback, task::priority p) - : m_priority(p), m_callback(callback) {} + task::task(std::function callback, priority_t p) + : m_callback(callback) {} void job::invoke_tasks() { for (const task& t : *this) { diff --git a/lib/libmm b/lib/libmm index e6595cf..ba03d19 160000 --- a/lib/libmm +++ b/lib/libmm @@ -1 +1 @@ -Subproject commit e6595cf9c527e97198806c48aa0a14ae8e895e87 +Subproject commit ba03d198b82733043919fda918a80f4bb81800a7 -- cgit v1.2.1