From e8b68b84eb78515c49008ddc83f84e9187df31f7 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Sat, 4 Apr 2015 09:37:03 -0400 Subject: [PATCH] queue: Add an on_remove() notification to the QNotifier class Signed-off-by: Anna Schumaker --- core/queue.cpp | 6 +++++- include/core/queue.h | 18 ++++++++++++------ tests/core/queue.cpp | 25 +++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/core/queue.cpp b/core/queue.cpp index a6958a31..d0b03c5d 100644 --- a/core/queue.cpp +++ b/core/queue.cpp @@ -14,6 +14,7 @@ static class DefaultNotifier : public QNotifier { public: DefaultNotifier() {}; + void on_remove() {}; void on_track_added(unsigned int pos) {}; void on_track_removed(unsigned int pos) {}; void on_track_updated(unsigned int pos) {}; @@ -29,7 +30,10 @@ Queue :: Queue() {} Queue :: ~Queue() -{} +{ + if (has_flag(Q_NOTIFY_REMOVE)) + _notify->on_remove(); +} void Queue :: write(File &file) { diff --git a/include/core/queue.h b/include/core/queue.h index d47032f8..f0aec1a6 100644 --- a/include/core/queue.h +++ b/include/core/queue.h @@ -16,10 +16,11 @@ * Enum defining flags that effect a Queue's behavior. */ enum queue_flags { - Q_ENABLED = (1 << 0), /**< Queue is enabled. */ - Q_RANDOM = (1 << 1), /**< Queue will pick songs randomly. */ - Q_REPEAT = (1 << 2), /**< Queue will not remove songs when picked. */ - Q_NO_SORT = (1 << 3), /**< Queue will not be sorted. */ + Q_ENABLED = (1 << 0), /**< Queue is enabled. */ + Q_RANDOM = (1 << 1), /**< Queue will pick songs randomly. */ + Q_REPEAT = (1 << 2), /**< Queue will not remove songs when picked. */ + Q_NO_SORT = (1 << 3), /**< Queue will not be sorted. */ + Q_NOTIFY_REMOVE = (1 << 4), /**< Queue will send a notification when removed. */ }; @@ -47,6 +48,11 @@ class QNotifier { public: QNotifier() {}; /**< Notifier constructor. */ + /** + * Called when a queue is removed and destroyed. + */ + virtual void on_remove() = 0; + /** * Called when a track is added to a queue. * @@ -108,8 +114,8 @@ public: */ Queue(unsigned int); - Queue(); /**< Default queue constructor. */ - ~Queue(); /**< Queue destructor. */ + Queue(); /**< Default queue constructor. */ + virtual ~Queue(); /**< Queue destructor. */ /** * Write a queue to disk. diff --git a/tests/core/queue.cpp b/tests/core/queue.cpp index 6b2b1e9d..a0a0b062 100644 --- a/tests/core/queue.cpp +++ b/tests/core/queue.cpp @@ -8,6 +8,7 @@ #include +unsigned int count_destroyed = 0; unsigned int count_add = 0; unsigned int count_del = 0; unsigned int count_updated = 0; @@ -19,6 +20,7 @@ Track *TRACK_NULL = NULL; static class TestNotifier : public QNotifier { public: TestNotifier() : QNotifier() {} + void on_remove() { count_destroyed++; } void on_track_added(unsigned int i) { count_add++; } void on_track_removed(unsigned int i) { count_del++; } void on_track_updated(unsigned int i) { count_updated++; last_update = i; } @@ -81,6 +83,28 @@ void test_flags() test_equal(q.has_flag(Q_RANDOM), true); test_equal(q.has_flag(Q_REPEAT), true); test_equal(q.has_flag(Q_NO_SORT), false); + test_equal(q.has_flag(Q_NOTIFY_REMOVE), false); +} + + +unsigned int _test_destructor_null(unsigned int i) +{ + TestQueue *q = new TestQueue(0); + delete q; + return (count_destroyed == 0) ? LOOP_PASSED : LOOP_FAILED; +} + +unsigned int _test_destructor_flag(unsigned int i) +{ + TestQueue *q = new TestQueue(Q_NOTIFY_REMOVE); + delete q; + return (count_destroyed == (i + 1)) ? LOOP_PASSED : LOOP_FAILED; +} + +void test_destructor() +{ + test_for_each(0, 10, 1, _test_destructor_null); + test_for_each(0, 10, 1, _test_destructor_flag); } @@ -362,6 +386,7 @@ int main(int argc, char **argv) test :: run("Queue Default Constructor Test", test_default); test :: run("Queue Constructor Test", test_constructor); test :: run("Queue Flag Test", test_flags); + test :: run("Queue Destructor Test", test_destructor); test :: run("Queue Add and Remove Test", test_add_remove); test :: run("Queue Track Updated Test", test_updated); test :: run("Queue Pick Next Test", test_next);