queue: Create a notifier class

This class will be used to push queue changes directly to the GUI.
Currently changes get mapped through the old callbacks system, which can
lead to several inefficiencies because the GUI has to look up each queue
structure in a list.

This patch implements a basic QNotifier class and provides a function
for setting a Queue's notifier.

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-04-03 10:30:46 -04:00
parent 8453b58ae3
commit c5598293d6
3 changed files with 43 additions and 4 deletions

View File

@ -11,12 +11,18 @@
#include <sstream>
static class DefaultNotifier : public QNotifier {
public:
DefaultNotifier() {};
} def_notify;
Queue :: Queue(unsigned int flags)
: _cur(-1), _flags(flags), _length(0)
: _cur(-1), _flags(flags), _length(0), _notify(&def_notify)
{}
Queue :: Queue()
: _cur(-1), _flags(0), _length(0)
: _cur(-1), _flags(0), _length(0), _notify(&def_notify)
{}
Queue :: ~Queue()
@ -56,6 +62,11 @@ bool Queue :: has_flag(queue_flags flag)
return (_flags & flag) == (unsigned int)flag;
}
void Queue :: set_notifier(QNotifier *notify)
{
_notify = notify;
}
/*
* Returns:
* 0: lhs == rhs

View File

@ -39,6 +39,16 @@ enum sort_t {
};
/**
* Class to assist in notifying the GUI of queue changes.
*/
class QNotifier {
public:
QNotifier() {}; /**< Notifier constructor. */
};
/**
* Struct for passing sort parameters.
*/
@ -64,6 +74,7 @@ protected:
unsigned int _cur; /**< The index of the last track played. */
unsigned int _flags; /**< Mask of queue_flags. */
unsigned int _length; /**< Total runtime of the queue. */
QNotifier *_notify; /**< Notification object associated with this queue. */
unsigned int find_sorted_id(Track *);
unsigned int _add_at(Track *, unsigned int);
@ -117,6 +128,14 @@ public:
bool has_flag(queue_flags);
/**
* Set a notifier for this queue.
*
* @param notify notifier instance to associate with this queue.
*/
void set_notifier(QNotifier *);
/**
* Add a track to the queue, possibly matching the
* current sort order.

View File

@ -16,12 +16,19 @@ unsigned int expected = 0;
Track *TRACK_NULL = NULL;
static class TestNotifier : public QNotifier {
public:
TestNotifier() : QNotifier() {}
} test_notifier;
class TestQueue : public Queue {
public:
TestQueue() : Queue() {}
TestQueue(unsigned int f) : Queue(f) {}
TestQueue() : Queue() { set_notifier(&test_notifier); }
TestQueue(unsigned int f) : Queue(f) { set_notifier(&test_notifier); }
unsigned int get_cur() { return _cur; }
unsigned int get_flags() { return _flags; }
QNotifier *get_notify() { return _notify; }
std::vector <sort_info> get_sorder() { return _sort_order; };
};
@ -39,6 +46,7 @@ void test_default()
test_equal(q.length(), (unsigned)0);
test_equal(q.get_sorder().size(), (size_t)0);
test_equal(q.next(), (Track *)NULL);
test_equal(q.get_notify(), &test_notifier);
}
void test_constructor()
@ -51,6 +59,7 @@ void test_constructor()
test_equal(q.length(), (unsigned)0);
test_equal(q.get_sorder().size(), (size_t)0);
test_equal(q.next(), (Track *)NULL);
test_equal(q.get_notify(), &test_notifier);
}
void test_flags()