From dd3fccac307527d3b1285f6ac7966b6d527627cb Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Fri, 2 Jan 2015 15:58:17 +0100 Subject: Added new header containing definitions for the event system --- src/core/managed/Events.cpp | 23 ++++++ src/core/managed/Events.hpp | 176 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 199 insertions(+) create mode 100644 src/core/managed/Events.cpp create mode 100644 src/core/managed/Events.hpp (limited to 'src/core') diff --git a/src/core/managed/Events.cpp b/src/core/managed/Events.cpp new file mode 100644 index 0000000..6036e9d --- /dev/null +++ b/src/core/managed/Events.cpp @@ -0,0 +1,23 @@ +/* + Ousía + Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel + + 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 3 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, see . +*/ + +#include "Events.hpp" + +namespace ousia { +} + diff --git a/src/core/managed/Events.hpp b/src/core/managed/Events.hpp new file mode 100644 index 0000000..90dabec --- /dev/null +++ b/src/core/managed/Events.hpp @@ -0,0 +1,176 @@ +/* + Ousía + Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel + + 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 3 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, see . +*/ + +/** + * @file Events.hpp + * + * Contains classes and structures used for event handling within the Manager + * and Managed classes. + * + * @author Andreas Stöckel (astoecke@techfak.uni-bielefeld.de) + */ + +#ifndef _OUSIA_EVENTS_HPP_ +#define _OUSIA_EVENTS_HPP_ + +#include + +namespace ousia { + +// Forward declarations +class Managed; +class Event; + +using EventId = size_t; + +/** + * EventType is an enum containing all possible node events. New event types + * should be added here. + */ +enum class EventType : int { + /** + * Generic update event which may be triggered if some important property + * of the node is changed. + */ + UPDATE, + + /** + * The NAME_CHANGE event is used to inform listeners that the name of the + * node has changed. + */ + NAME_CHANGE, + + /** + * The ADD_CHILD event is used to inform listeners that the node got a new + * child in any of its child node lists. + */ + ADD_CHILD, + + /** + * The DELETE_CHILD event is used to inform listeners that the node got a + * new child in any of its child node lists. + */ + DELETE_CHILD +}; + +/** + * Definition of the EventHandler function. + * + * @param event is a reference to the object holding the event data. + * @param owner is a reference to the managed object that was given in the + * registerEventHandler function. + */ +using EventHandler = void (*)(const Event &event, Managed* owner); + +/** + * The Event class and its child classes are responsible for containing the + * actual event data which further describes the event to the event handlers. + */ +struct Event { + /** + * Actual event type. + */ + EventType type; + + /** + * Node on which the event was triggered. + */ + Managed *sender; + + /** + * Constructor of the Event class. + * + * @param type is the actual event type. + */ + Event(EventType type) : type(type), sender(nullptr){}; + + Event(const Event &) = delete; + Event &operator=(const Event &) = delete; +}; + +/** + * Event used when the name of a node has changed. + */ +struct NameChangeEvent : public Event { + /** + * Reference to a string containing the old name of the node. + */ + const std::string &oldName; + + /** + * Reference to a string containing the new name of the node. + */ + const std::string &newName; + + /** + * Constructor of the NameChangeEvent class. + * + * @param oldName is the old name of the node. The given string is not + * copied, only the reference is passed around. + * @param newName is the new name of the node. The given string is not + * copied, only the reference is passed around. + */ + NameChangeEvent(const std::string &oldName, const std::string &newName) + : Event(EventType::NAME_CHANGE), + oldName(oldName), + newName(newName) + { + } +}; + +/** + * Struct containing the data which describes a single registered event handler. + */ +struct EventHandlerDescriptor { + /** + * Event type. + */ + EventType type; + + /** + * Reference to the event handler containing the events. + */ + EventHandler handler; + + /** + * Owner object. + */ + Managed *owner; + + /** + * Constructor of the EventHandlerDescriptor struct. + * + * @param type is the event type for which the handler is registered. + * @param handler is the function pointer which is going to be called once + * the associated event handler has fired. + * @param owner is a user-specified object which owns the method that is + * going to be called. This can be used to make sure that the method which + * handles the events has access to its owned object as long as the event + * handler lives. + */ + EventHandlerDescriptor(EventType type, EventHandler handler, Managed *owner) + : type(type), + handler(handler), + owner(owner) + { + } +}; +} + +#endif /* _OUSIA_EVENTS_HPP_ */ + -- cgit v1.2.3