Skip to content

Commit

Permalink
Merge branch 'dev' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
castle055 committed Apr 5, 2024
2 parents 280236e + bd7e0c8 commit 01e2543
Show file tree
Hide file tree
Showing 10 changed files with 292 additions and 264 deletions.
35 changes: 18 additions & 17 deletions include/async/event_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace cydui::async {

std::unordered_map<
str,
std::deque<cydui::async::listener_t*>
std::deque<std::shared_ptr<cydui::async::listener_t>>
> event_listeners {};
private TEST_PUBLIC:
std::mutex event_mutex;
Expand Down Expand Up @@ -49,25 +49,27 @@ namespace cydui::async {
std::scoped_lock lk {event_mutex};
std::swap(front_event_queue, back_event_queue);
}
std::vector<listener_t*> get_listeners_for_event(const std::string &ev_type) {
std::vector<listener_t*> listeners {};
std::vector<std::shared_ptr<listener_t>> get_listeners_for_event(const std::string &ev_type) {
std::vector<std::shared_ptr<listener_t>> listeners {};

{ // Make copy of listeners with the mutex
std::scoped_lock lk {listeners_mutex};
if (event_listeners.contains(ev_type)) {
std::deque<listener_t*> &ev_listeners = event_listeners[ev_type];
listeners.reserve(ev_listeners.size());
for (const auto &item: ev_listeners) {
listeners.push_back(item);
}
if (event_listeners.contains(ev_type)) {
std::deque<std::shared_ptr<listener_t>> &ev_listeners = event_listeners[ev_type];
listeners.reserve(ev_listeners.size());
for (const auto &item: ev_listeners) {
listeners.emplace_back(item);
}
}

return listeners;
}
void process_event(const event_t &ev) {
ev->status = PROCESSING;
std::vector<listener_t*> listeners = get_listeners_for_event(ev->type);
//{ // Make copy of listeners with the mutex
// std::scoped_lock lk {listeners_mutex};
listeners_mutex.lock();
std::vector<std::shared_ptr<listener_t>> listeners = get_listeners_for_event(ev->type);
listeners_mutex.unlock();
//}

// Iterate over copy of listeners list. This should allow any listener to modify the listeners list (ie: removing themselves)
for (auto &listener: listeners) {
Expand All @@ -78,11 +80,9 @@ namespace cydui::async {
ev->status = CONSUMED;
}
void process_all_events(std::queue<event_t> &ev_queue) {
event_t ev;
while (!ev_queue.empty()) {
ev = ev_queue.front();
process_event(ev_queue.front());
ev_queue.pop();
process_event(ev);
}
}
protected TEST_PUBLIC: /// @name Bus Interface
Expand All @@ -99,10 +99,11 @@ namespace cydui::async {
}

listener_t* on_event_raw(const str &event_type, const Listener &l_) {
std::scoped_lock lk {listeners_mutex};
if (!event_listeners.contains(event_type))
event_listeners.insert({event_type, {}});
auto* l = new listener_t {this, event_type, l_};
event_listeners[event_type].push_back(l);
event_listeners[event_type].emplace_back(l);
return l;
}
template<typename T>
Expand All @@ -119,6 +120,7 @@ namespace cydui::async {
void remove_listener(const listener_t &listener) {
if (listener.get_id() == 0) return;
const str &event_type = listener.event_type;
std::scoped_lock lk {listeners_mutex};
if (event_listeners.contains(event_type)) {
for (auto l = event_listeners[event_type].begin(); l != event_listeners[event_type].end(); l++) {
if ((*l)->get_id() == listener.get_id()) {
Expand All @@ -139,7 +141,6 @@ namespace cydui::async {
auto listeners = get_listeners_for_event(item.first);
for (auto &l: listeners) {
l->remove();
delete l;
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions include/async/events.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ namespace cydui::async {

class listener_t {
private TEST_PUBLIC:
long ID;
std::unique_ptr<u8> ID;
bool active = true;
public:
listener_t(): event_queue(nullptr) {
Expand All @@ -90,15 +90,15 @@ namespace cydui::async {
: event_queue(event_queue_),
event_type(std::move(type)),
func(new Listener {std::move(c)}) {
ID = (long) new u8;
ID = std::unique_ptr<u8>(new u8);
}
//
//listener_t(const listener_t &other) {
// ID = other.get_id();
//}

[[nodiscard]] long get_id() const {
return ID;
return (long)(u8 *)ID.get();
}

event_queue_t* const event_queue;
Expand All @@ -112,7 +112,7 @@ namespace cydui::async {
}

void operator()(event_t ev) const {
if (nullptr != func) {
if (nullptr != func && active) {
func->operator()(ev);
}
}
Expand Down
12 changes: 5 additions & 7 deletions include/components/component.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ namespace cydui::components {

virtual void redraw() = 0;
virtual void
get_fragment(cydui::layout::Layout* layout, cydui::compositing::compositing_node_t* compositing_node) = 0;
get_fragment(cydui::layout::Layout* layout, std::unique_ptr<cydui::compositing::compositing_node_t>& compositing_node) = 0;

virtual component_state_t* create_state_instance() = 0;

Expand All @@ -113,15 +113,14 @@ namespace cydui::components {
if (s->window == nullptr) return s;
for (auto &l: listeners) {
auto &[ev_type, handler] = l;
s->window->on_event_raw(ev_type, handler.handler);
subscribed_listeners.push_back(s->window->on_event_raw(ev_type, handler.handler));
}
return s;
});
}
void clear_subscribed_listeners() {
for (auto &item: subscribed_listeners) {
item->remove();
delete item;
}
subscribed_listeners.clear();
}
Expand Down Expand Up @@ -254,11 +253,10 @@ namespace cydui::components {
}

void
get_fragment(cydui::layout::Layout* layout, cydui::compositing::compositing_node_t* compositing_node) override {
get_fragment(cydui::layout::Layout* layout, std::unique_ptr<cydui::compositing::compositing_node_t>& compositing_node) override {
for (auto &child: children) {
auto* child_node = new cydui::compositing::compositing_node_t;
compositing_node->children.push_back(child_node);
child->get_fragment(layout, child_node);
compositing_node->children.emplace_back(new compositing::compositing_node_t{});
child->get_fragment(layout, compositing_node->children.back());
}

compositing_node->id = (unsigned long) (this->state.value());
Expand Down

0 comments on commit 01e2543

Please sign in to comment.