summaryrefslogtreecommitdiff
path: root/src/utils
diff options
context:
space:
mode:
authorFlorent Le Coz <louiz@louiz.org>2014-05-30 15:42:01 +0200
committerFlorent Le Coz <louiz@louiz.org>2014-05-30 15:42:01 +0200
commit5cca518c2d946144f4fee1b15dcfd3884850dcb1 (patch)
tree392f1935cf9bd8b21a9346f03a4f934e63e9e94a /src/utils
parenta63faf6fa95017dbbfeaf0ff43fdb526c4ae7068 (diff)
downloadbiboumi-5cca518c2d946144f4fee1b15dcfd3884850dcb1.tar.gz
biboumi-5cca518c2d946144f4fee1b15dcfd3884850dcb1.tar.bz2
biboumi-5cca518c2d946144f4fee1b15dcfd3884850dcb1.tar.xz
biboumi-5cca518c2d946144f4fee1b15dcfd3884850dcb1.zip
Timed events can have a name, and can be canceled based on their name
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/timed_events.cpp18
-rw-r--r--src/utils/timed_events.hpp20
-rw-r--r--src/utils/timed_events_manager.cpp20
3 files changed, 51 insertions, 7 deletions
diff --git a/src/utils/timed_events.cpp b/src/utils/timed_events.cpp
index 10ef4c1..5010a3f 100644
--- a/src/utils/timed_events.cpp
+++ b/src/utils/timed_events.cpp
@@ -1,20 +1,22 @@
#include <utils/timed_events.hpp>
TimedEvent::TimedEvent(std::chrono::steady_clock::time_point&& time_point,
- std::function<void()> callback):
+ std::function<void()> callback, const std::string& name):
time_point(std::move(time_point)),
callback(callback),
repeat(false),
- repeat_delay(0)
+ repeat_delay(0),
+ name(name)
{
}
TimedEvent::TimedEvent(std::chrono::milliseconds&& duration,
- std::function<void()> callback):
+ std::function<void()> callback, const std::string& name):
time_point(std::chrono::steady_clock::now() + duration),
callback(callback),
repeat(true),
- repeat_delay(std::move(duration))
+ repeat_delay(std::move(duration)),
+ name(name)
{
}
@@ -22,7 +24,8 @@ TimedEvent::TimedEvent(TimedEvent&& other):
time_point(std::move(other.time_point)),
callback(std::move(other.callback)),
repeat(other.repeat),
- repeat_delay(std::move(other.repeat_delay))
+ repeat_delay(std::move(other.repeat_delay)),
+ name(std::move(other.name))
{
}
@@ -52,3 +55,8 @@ void TimedEvent::execute()
{
this->callback();
}
+
+const std::string& TimedEvent::get_name() const
+{
+ return this->name;
+}
diff --git a/src/utils/timed_events.hpp b/src/utils/timed_events.hpp
index 9be747d..f601cae 100644
--- a/src/utils/timed_events.hpp
+++ b/src/utils/timed_events.hpp
@@ -25,9 +25,9 @@ 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);
+ std::function<void()> callback, const std::string& name="");
explicit TimedEvent(std::chrono::milliseconds&& duration,
- std::function<void()> callback);
+ std::function<void()> callback, const std::string& name="");
explicit TimedEvent(TimedEvent&&);
~TimedEvent();
@@ -43,6 +43,7 @@ public:
*/
std::chrono::milliseconds get_timeout() const;
void execute();
+ const std::string& get_name() const;
private:
/**
@@ -62,6 +63,12 @@ private:
* if repeat is true. Otherwise it is ignored.
*/
const 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.
+ */
+ const std::string name;
TimedEvent(const TimedEvent&) = delete;
TimedEvent& operator=(const TimedEvent&) = delete;
@@ -99,6 +106,15 @@ public:
* 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::list<TimedEvent> events;
diff --git a/src/utils/timed_events_manager.cpp b/src/utils/timed_events_manager.cpp
index c3e260c..a03444e 100644
--- a/src/utils/timed_events_manager.cpp
+++ b/src/utils/timed_events_manager.cpp
@@ -53,3 +53,23 @@ std::size_t TimedEventsManager::execute_expired_events()
return count;
}
+std::size_t TimedEventsManager::cancel(const std::string& name)
+{
+ std::size_t res = 0;
+ for (auto it = this->events.begin(); it != this->events.end();)
+ {
+ if (it->get_name() == name)
+ {
+ it = this->events.erase(it);
+ res++;
+ }
+ else
+ ++it;
+ }
+ return res;
+}
+
+std::size_t TimedEventsManager::size() const
+{
+ return this->events.size();
+}