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 ++++--------- 3 files changed, 25 insertions(+), 60 deletions(-) (limited to 'engine/include/core') 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); } -- cgit v1.2.1