summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorancarola <raffaele.ancarola@epfl.ch>2019-01-23 21:04:10 +0100
committerancarola <raffaele.ancarola@epfl.ch>2019-01-23 21:04:10 +0100
commite37cdd6179535c46f0f46ea491801984d7b8bc2b (patch)
tree1b4a250e1bead782101b0d449b0b7685386b05ff
parentSignal finalized? (diff)
parentUpdate job interface to allow safe ownership delegation (diff)
downloadflatland-e37cdd6179535c46f0f46ea491801984d7b8bc2b.tar.gz
flatland-e37cdd6179535c46f0f46ea491801984d7b8bc2b.zip
Merge remote-tracking branch 'nao/master'
No conflits, YEEESS
-rw-r--r--.gitignore2
-rwxr-xr-xbuild/test/task_testbin118424 -> 0 bytes
-rw-r--r--engine/include/core/priority.hpp17
-rw-r--r--engine/include/core/task.hpp22
-rw-r--r--engine/task.cpp31
m---------lib/libmm0
-rw-r--r--makefile69
-rw-r--r--ninja/rules.ninja4
-rw-r--r--test/task_test.cpp35
9 files changed, 63 insertions, 117 deletions
diff --git a/.gitignore b/.gitignore
index c3e0a43..a054592 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,5 +1,5 @@
# build files
-bin
+build
**/*.o
# ninja build files
diff --git a/build/test/task_test b/build/test/task_test
deleted file mode 100755
index dba86da..0000000
--- a/build/test/task_test
+++ /dev/null
Binary files differ
diff --git a/engine/include/core/priority.hpp b/engine/include/core/priority.hpp
index 1219d26..ae4a6db 100644
--- a/engine/include/core/priority.hpp
+++ b/engine/include/core/priority.hpp
@@ -16,29 +16,24 @@ namespace flat {
};
class prioritized {
- private:
- const priority_t m_priority;
-
public:
- prioritized(priority_t priority = priority_t::none)
- : m_priority(priority) {}
+ const priority_t priority;
- const priority_t priority() const {
- return m_priority;
- }
+ prioritized(priority_t p = priority_t::none)
+ : priority(p) {}
};
struct prioritize {
bool operator()(const prioritized& lhs, const prioritized& rhs) {
- return lhs.priority() < rhs.priority();
+ return lhs.priority < rhs.priority;
}
- bool operator()(const std::weak_ptr<prioritized> lhs, const std::weak_ptr<prioritized>& rhs) {
+ bool operator()(const std::weak_ptr<prioritized> lhs, const std::weak_ptr<prioritized> rhs) {
if (auto l = lhs.lock()) {
if (auto r = rhs.lock()) {
// if both valid, check their priority
// in case they are the same, left is prioritized
- return l->priority() < r->priority();
+ return l->priority < r->priority;
} else {
// if right is expired, left is prioritized
return true;
diff --git a/engine/include/core/task.hpp b/engine/include/core/task.hpp
index d886fcd..77e1da6 100644
--- a/engine/include/core/task.hpp
+++ b/engine/include/core/task.hpp
@@ -2,7 +2,9 @@
#include "priority.hpp"
+#include <unordered_set>
#include <functional>
+#include <variant>
#include <memory>
namespace flat {
@@ -13,6 +15,7 @@ namespace flat {
class task : public prioritized {
public:
+ using ptr = std::shared_ptr<task>;
using callback = std::function<void(void)>;
task() = delete;
@@ -24,9 +27,24 @@ namespace flat {
callback m_callback;
};
- struct job : private queue<std::weak_ptr<task>> {
- std::shared_ptr<task> make_task(task::callback f, priority_t p = priority_t::none);
+ class job : protected queue<std::weak_ptr<task>> {
+ public:
+ /// add a task function owned by the job object
+ void add_task(task::callback f, priority_t p);
+
+ /// add a task function not owned by the job object (weak_ptr)
+ std::shared_ptr<task> delegate_task(task::callback f, priority_t p = priority_t::none);
+
+ /// add a task methods not owned by the job object (weak_ptr),
+ /// this allows to make the task die when the owner object goes out of scope
+ template<typename R, typename T>
+ inline std::shared_ptr<task> delegate_task(R T::*mf, T& obj, priority_t p = priority_t::none) {
+ return delegate_task(std::bind(mf, obj), p);
+ }
+
+ /// run tasks
void invoke_tasks();
+ inline void operator()() { invoke_tasks(); }
};
}
}
diff --git a/engine/task.cpp b/engine/task.cpp
index 7f1b08b..9cfbec4 100644
--- a/engine/task.cpp
+++ b/engine/task.cpp
@@ -2,6 +2,8 @@
#include <functional>
#include <memory>
+#include <algorithm>
+#include <cassert>
#include <vector>
@@ -14,31 +16,32 @@ namespace flat {
task::task(task::callback f, priority_t p)
: m_callback(f) {}
- std::shared_ptr<task> job::make_task(task::callback f, priority_t p) {
-
+ std::shared_ptr<task> job::delegate_task(task::callback f, priority_t p) {
auto shared = std::make_shared<task>(f, p);
insert(shared);
+
return shared;
}
- void job::invoke_tasks() {
- //std::for_each(begin(), end(), [&](auto tp) {
+ void job::add_task(task::callback f, priority_t p) {
+ insert(std::make_shared<task>(f, p));
+ }
- vector<weak_ptr<task>> to_erase;
+ void job::invoke_tasks() {
+ // expired tasks to remove
+ std::vector<job::value_type> to_erase;
- for (auto tp : (*this)) {
- if (tp.expired())
+ for (auto tp : *this) {
+ // check that the task has not expired
+ if (std::shared_ptr<task> t = tp.lock())
+ std::invoke(*t);
+ else
to_erase.push_back(tp);
}
- for (auto tp : to_erase)
+ // delete expired tasks
+ for (auto tp : to_erase) {
erase(tp);
-
- for (auto tp : (*this)) {
- // check that the task has not expired
- // run task
- auto t = tp.lock();
- std::invoke(*t);
}
}
diff --git a/lib/libmm b/lib/libmm
-Subproject e6595cf9c527e97198806c48aa0a14ae8e895e8
+Subproject ba03d198b82733043919fda918a80f4bb81800a
diff --git a/makefile b/makefile
deleted file mode 100644
index 86f67a9..0000000
--- a/makefile
+++ /dev/null
@@ -1,69 +0,0 @@
-# Makefile for symkit 1.0 by Raffaele Ancarola
-# C++ library for physics symulations
-
-NAME := flatland
-BIN := bin/lib$(NAME).so
-CC := g++
-CFLAGS := -Wall -std=c++17
-
-DIRS := engine
-BACKUP := backup
-INSTALL_DIR := /usr/lib
-
-INCLUDES := $(patsubst %,%/include,$(DIRS))
-
-# other required libraries
-LIBS := -lSDL2 #add libraries
-
-SRC := $(wildcard $(patsubst %,%/*.cpp,$(DIRS)))
-OBJ := $(patsubst %.cpp,bin/%.o,$(SRC))
-
-.PHONY: dirs clean install backup restore
-all: $(BIN)
-
-# builds all binaries into the shared library
-
-$(BIN): dirs $(OBJ)
- @printf "\nAssembling binaries\n\n"
- $(CC) $(CFLAGS) -shared -o $@ $(OBJ) -I $(INCLUDES) $(LIBS)
- @printf "\nCompilation successfully completed\n"
-
-# compile all sources
-
-$(OBJ): bin/%.o : %.cpp $(SRC)
- @printf "\nCompiling $<\n"
- $(CC) $(CFLAGS) -c $< -fPIC -o $@ -I $(INCLUDES) $(LIBS)
-
-# phony commands implementation
-
-# install the compiled library into your system
-# be careful with this command because it could not work
-install:
- chmod +x install.sh
- ./install.sh $(BIN) $(INSTALL_DIR)
-
-# generate all necessaries directories
-dirs:
- mkdir -p bin $(patsubst %,bin/%,$(DIRS))
- mkdir -p $(INCLUDES) $(BACKUP)
- @printf "Default directories created\n"
-
-# clean all binaries
-clean:
- rm -rfv bin/*
- @printf "Binary files cleaned\n"
-
-# backup the project in backup/symkit.zip
-backup:
- mkdir -p $(BACKUP)
- rm -rfv $(BACKUP)/*
- zip -r $(BACKUP)/symkit.zip $(DIRS)
- @printf "Backup completed\n"
-
-# restore the last backup, backup/symkit.zip must be present
-restore:
- unzip $(BACKUP)/symkit.zip -d $(BACKUP)
- rm -rfv $(DIRS)
- mv $(patsubst %,$(BACKUP)/%,$(DIRS)) .
- @printf "Backup restored\n"
-
diff --git a/ninja/rules.ninja b/ninja/rules.ninja
index 5897422..6674672 100644
--- a/ninja/rules.ninja
+++ b/ninja/rules.ninja
@@ -1,7 +1,7 @@
includes = -I lib/include -I engine/include
-cflags = -Wall -pedantic -fPIC -std=c++17 -DDEBUG $includes
+cflags = -g -Wall -pedantic -fPIC -std=c++17 -DDEBUG $includes
-libs = #-lSDL2 lib/libmm/build/libmm.a lib/libwsdl2/build/libwsdl2.a
+libs = -lSDL2 lib/libmm/build/libmm.a lib/libwsdl2/build/libwsdl2.a
lflags = $libs
flags = -fdiagnostics-color
diff --git a/test/task_test.cpp b/test/task_test.cpp
index e578bfd..04bb23d 100644
--- a/test/task_test.cpp
+++ b/test/task_test.cpp
@@ -6,21 +6,19 @@
using namespace flat::core;
// test class
-struct message {
-
- message(job& m_job, bool date)
- {
- if (date)
- mytask = m_job.make_task(std::bind(&message::print_date, *this));
- else
- mytask = m_job.make_task(std::bind(&message::print_motd, *this));
- }
-
- std::shared_ptr<task> mytask;
+class message {
+private:
+ task::ptr mytask;
std::string motd = "today we have no motd!";
std::string date = "1 Jan 1970";
+public:
+ message(job& job) {
+ // add an example job
+ mytask = job.delegate_task(&message::print_motd, *this);
+ }
+
void print_date() {
std::cout << date << std::endl;
}
@@ -49,26 +47,27 @@ int main(int argc, char *argv[]) {
job f_job;
// test a function
- auto ciao_fn_task = f_job.make_task(hello);
+ auto hello_fn_task = f_job.delegate_task(hello);
// test a function ad make the pointer go out of scope
{
- auto hello_fn_task = f_job.make_task(ciao);
+ auto ciao_fn_task = f_job.delegate_task(ciao);
}
- f_job.invoke_tasks();
+ f_job();
std::cout << std::endl;
std::cout << "Testing methods" << std::endl;
- std::cout << " should print once: today we have no motd!" << std::endl;
+ std::cout << "should print once: today we have no motd!" << std::endl;
+
job m_job;
// test a method
- message m(m_job, false);
+ message m(m_job);
// test a method of an object that goes out of scope
{
- message out(m_job, true);
+ message out(m_job);
}
// invoke tasks
@@ -76,7 +75,7 @@ int main(int argc, char *argv[]) {
//
// hello!
// hello world!
- m_job.invoke_tasks();
+ m_job();
return 0;
}