From 79daeb5a7536d00d0273158daff0ffac3c12291c Mon Sep 17 00:00:00 2001 From: Michael Fabian 'Xaymar' Dirks Date: Thu, 8 Nov 2018 08:07:49 +0100 Subject: [PATCH] util-event: Template event handler class --- CMakeLists.txt | 2 ++ source/util-event.cpp | 20 +++++++++++++ source/util-event.hpp | 65 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 source/util-event.cpp create mode 100644 source/util-event.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 5f9f2285..e161f196 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -264,6 +264,8 @@ SET(PROJECT_PRIVATE "${PROJECT_SOURCE_DIR}/source/strings.h" "${PROJECT_SOURCE_DIR}/source/utility.h" "${PROJECT_SOURCE_DIR}/source/utility.cpp" + "${PROJECT_SOURCE_DIR}/source/util-event.hpp" + "${PROJECT_SOURCE_DIR}/source/util-event.cpp" "${PROJECT_SOURCE_DIR}/source/util-math.h" "${PROJECT_SOURCE_DIR}/source/util-math.cpp" "${PROJECT_SOURCE_DIR}/source/util-memory.h" diff --git a/source/util-event.cpp b/source/util-event.cpp new file mode 100644 index 00000000..12fb7c77 --- /dev/null +++ b/source/util-event.cpp @@ -0,0 +1,20 @@ +/* +* Modern effects for a modern Streamer +* Copyright (C) 2017 Michael Fabian Dirks +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA +*/ + +#include "util-event.hpp" diff --git a/source/util-event.hpp b/source/util-event.hpp new file mode 100644 index 00000000..815f50b6 --- /dev/null +++ b/source/util-event.hpp @@ -0,0 +1,65 @@ +/* + * Modern effects for a modern Streamer + * Copyright (C) 2017 Michael Fabian Dirks + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#pragma once +#include +#include + +namespace util { + template + class event { + std::list> listeners; + + public: + void add(std::function listener) + { + listeners.push_back(listener); + } + + void remove(std::function listener) + { + listeners.remove(listener); + } + + // Not valid without the extra template. + template + void operator()(_args... args) + { + for (auto& l : listeners) { + l(args...); + } + } + + operator bool() + { + return !listeners.empty(); + } + + public: + bool empty() + { + return listeners.empty(); + } + + void clear() + { + listeners.clear(); + } + }; +}; // namespace util