summaryrefslogtreecommitdiff
path: root/louloulibs/utils/timed_events.hpp
diff options
context:
space:
mode:
authorVasudev Kamath <vasudev@copyninja.info>2016-10-23 21:09:41 +0530
committerVasudev Kamath <vasudev@copyninja.info>2016-10-23 21:09:41 +0530
commit4e4de7284e6e4d89d182ea459823bbec1e408842 (patch)
tree47e0ed5216b48649b138f168f61fddca2b0c076a /louloulibs/utils/timed_events.hpp
parentdfb3a6edfacf2f16a8a63690b3e8058b6295d1a3 (diff)
parenteda4b75b1cff83336e87da90efca9fd6b4ced2c7 (diff)
downloadbiboumi-4e4de7284e6e4d89d182ea459823bbec1e408842.tar.gz
biboumi-4e4de7284e6e4d89d182ea459823bbec1e408842.tar.bz2
biboumi-4e4de7284e6e4d89d182ea459823bbec1e408842.tar.xz
biboumi-4e4de7284e6e4d89d182ea459823bbec1e408842.zip
Updated version 3.0 from 'upstream/3.0'
with Debian dir 0f18938e98f5a466f36719f60cef0490163ab845
Diffstat (limited to 'louloulibs/utils/timed_events.hpp')
-rw-r--r--louloulibs/utils/timed_events.hpp132
1 files changed, 132 insertions, 0 deletions
diff --git a/louloulibs/utils/timed_events.hpp b/louloulibs/utils/timed_events.hpp
new file mode 100644
index 0000000..6e28206
--- /dev/null
+++ b/louloulibs/utils/timed_events.hpp
@@ -0,0 +1,132 @@
+#pragma once
+
+#include <functional>
+#include <string>
+#include <chrono>
+#include <vector>
+
+using namespace std::literals::chrono_literals;
+
+namespace utils {
+static constexpr std::chrono::milliseconds no_timeout = std::chrono::milliseconds(-1);
+}
+
+class TimedEventsManager;
+
+/**
+ * A callback with an associated date.
+ */
+
+class TimedEvent
+{
+ friend class TimedEventsManager;
+public:
+ /**
+ * An event the occurs only once, at the given time_point
+ */
+ explicit TimedEvent(std::chrono::steady_clock::time_point&& time_point,
+ std::function<void()> callback, const std::string& name="");
+ explicit TimedEvent(std::chrono::milliseconds&& duration,
+ std::function<void()> callback, const std::string& name="");
+
+ explicit TimedEvent(TimedEvent&&) = default;
+ TimedEvent& operator=(TimedEvent&&) = default;
+ ~TimedEvent() = default;
+
+ TimedEvent(const TimedEvent&) = delete;
+ TimedEvent& operator=(const TimedEvent&) = delete;
+
+ /**
+ * Whether or not this event happens after the other one.
+ */
+ bool is_after(const TimedEvent& other) const;
+ bool is_after(const std::chrono::steady_clock::time_point& time_point) const;
+ /**
+ * Return the duration difference between now and the event time point.
+ * If the difference would be negative (i.e. the event is expired), the
+ * returned value is 0 instead. The value cannot then be negative.
+ */
+ std::chrono::milliseconds get_timeout() const;
+ void execute() const;
+ const std::string& get_name() const;
+
+private:
+ /**
+ * The next time point at which the event is executed.
+ */
+ std::chrono::steady_clock::time_point time_point;
+ /**
+ * The function to execute.
+ */
+ std::function<void()> callback;
+ /**
+ * Whether or not this events repeats itself until it is destroyed.
+ */
+ bool repeat;
+ /**
+ * This value is added to the time_point each time the event is executed,
+ * if repeat is true. Otherwise it is ignored.
+ */
+ std::chrono::milliseconds repeat_delay;
+ /**
+ * A name that is used to identify that event. If you want to find your
+ * event (for example if you want to cancel it), the name should be
+ * unique.
+ */
+ std::string name;
+};
+
+/**
+ * A class managing a list of TimedEvents.
+ * They are sorted, new events can be added, removed, fetch, etc.
+ */
+
+class TimedEventsManager
+{
+public:
+ ~TimedEventsManager() = default;
+
+ TimedEventsManager(const TimedEventsManager&) = delete;
+ TimedEventsManager(TimedEventsManager&&) = delete;
+ TimedEventsManager& operator=(const TimedEventsManager&) = delete;
+ TimedEventsManager& operator=(TimedEventsManager&&) = delete;
+
+ /**
+ * Return the unique instance of this class
+ */
+ static TimedEventsManager& instance();
+ /**
+ * Add an event to the list of managed events. The list is sorted after
+ * this call.
+ */
+ void add_event(TimedEvent&& event);
+ /**
+ * Returns the duration, in milliseconds, between now and the next
+ * available event. If the event is already expired (the duration is
+ * negative), 0 is returned instead (as in “it's not too late, execute it
+ * now”)
+ * Returns a negative value if no event is available.
+ */
+ std::chrono::milliseconds get_timeout() const;
+ /**
+ * Execute all the expired events (if their expiration time is exactly
+ * now, or before now). The event is then removed from the list. If the
+ * event does repeat, its expiration time is updated and it is reinserted
+ * in the list at the correct position.
+ * Returns the number of executed events.
+ */
+ std::size_t execute_expired_events();
+ /**
+ * Remove (and thus cancel) all the timed events with the given name.
+ * Returns the number of canceled events.
+ */
+ std::size_t cancel(const std::string& name);
+ /**
+ * Return the number of managed events.
+ */
+ std::size_t size() const;
+
+private:
+ std::vector<TimedEvent> events;
+ explicit TimedEventsManager() = default;
+};