diff options
author | ancarola <raffaele.ancarola@epfl.ch> | 2019-01-23 19:43:09 +0100 |
---|---|---|
committer | ancarola <raffaele.ancarola@epfl.ch> | 2019-01-23 19:43:09 +0100 |
commit | 40a2e6ae3d17e271f9eaa07a53d934fc5d5457db (patch) | |
tree | 0da332dbff1d621306d5bfc4d1e54fccb795a206 /engine/include/core | |
parent | Task debugging (diff) | |
download | flatland-40a2e6ae3d17e271f9eaa07a53d934fc5d5457db.tar.gz flatland-40a2e6ae3d17e271f9eaa07a53d934fc5d5457db.zip |
Labelled, signal
Diffstat (limited to 'engine/include/core')
-rw-r--r-- | engine/include/core/labelled.hpp | 26 | ||||
-rw-r--r-- | engine/include/core/object.hpp | 38 | ||||
-rw-r--r-- | engine/include/core/signal.hpp | 60 |
3 files changed, 70 insertions, 54 deletions
diff --git a/engine/include/core/labelled.hpp b/engine/include/core/labelled.hpp new file mode 100644 index 0000000..7363e8d --- /dev/null +++ b/engine/include/core/labelled.hpp @@ -0,0 +1,26 @@ +#pragma once + +#include <list> +#include <vector> +#include <string> +#include <initializer_list> + +#include "types.hpp" + +namespace flat +{ + namespace core + { + class labelled; + } +} + +struct flat::core::labelled +{ + const std::string label; + + labelled(const std::string& label = ""); + + static std::string random_label(uint8_t length = 8); +}; + diff --git a/engine/include/core/object.hpp b/engine/include/core/object.hpp index 1992d2a..cd5da5e 100644 --- a/engine/include/core/object.hpp +++ b/engine/include/core/object.hpp @@ -1,42 +1,12 @@ #pragma once -#include <list> -#include <vector> -#include <string> -#include <initializer_list> - -#include "types.hpp" - namespace flat { namespace core { - class object; + struct object + { + // it exists + }; } } - -class flat::core::object -{ - std::string id; - - /* Common list of objects */ - static std::list<object*> 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<object*>& get_by_id(const std::string& id, std::vector<object*>&); - - static std::string random_id(uint8_t length = 8); -}; - diff --git a/engine/include/core/signal.hpp b/engine/include/core/signal.hpp index 2393bc7..4270aa5 100644 --- a/engine/include/core/signal.hpp +++ b/engine/include/core/signal.hpp @@ -8,73 +8,92 @@ #include "task.hpp" #include "types.h" #include <functional> +#include <memory> #include "priority.hpp" +#include "labelled.hpp" namespace flat { namespace core { - class signal : virtual public object, public prioritized + class signal : virtual public labelled, virtual public prioritized { public: object * sender; - void * data; // TODO, avoid this void pointer + package m_package; priority_t prior; signal( object * sender, const std::string& id = "", - void * data, + void * data = 0, priority_t prior = priority_t::none); /* Alias to flat::core::channel::emit() */ bool emit(const std::string& channel) const; + + struct package + { + package(void *data) : data(data) {} + + template<class T> + T * get() { + + return dynamic_cast<T>(data); + } + + void * data; + }; }; /* Channel class */ - class channel : virtual public object + class channel : virtual public labelled { /* Post processing signal stacking */ queue<signal> stack; /* Listeners list */ - std::list<listener*> listeners; + std::list<std::weak_ptr<listener>> listeners; /* Synchronous task checking for signals */ - task * checker; + task::ptr checker; /* Channel mapping */ - static std::map<std::string, channel*> channels; + static std::map<std::string, std::weak_ptr<channel>> channels; public: channel(const std::string& id = "", priority_t task_priority = priority_t::none); ~channel(); + + void start(); + bool map(); void emit(const signal&); - void connect(const listener*); - void disconnect(const listener*); + bool connect(listener::ptr l); + void disconnect(listener::ptr l); - static channel * find_channel(const std::string&); + static ptr find(const std::string&); + + static ptr create(const string& id, priority_t prior); - void post_processing(); + void check_and_call(); + + typedef shared_ptr<channel> ptr; }; /* Listener class */ - class listener : virtual public object + class listener { - std::list<std::string> filters; bool check_in_filters(const std::string&) const; callback_t m_callback; - - channel * parent; public: @@ -93,14 +112,15 @@ namespace flat /* 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 = {}) + static ptr make( const (T::*method(const object*, void*))& fct, + T* obj, + const std::initializer_list<std::string>& filters = {}) { - return listener(std::bind(fct, ptr), filters); + return new listener(std::bind(fct, ptr), filters); } - typedef std::function<void(const object*, void*)> callback_t; + typedef std::function<void(const object*, signal::package)> callback_t; + typedef shared_ptr<listener> ptr; }; } |