From 6ee68397fd3e7f63fac7324d312b6634195ce4f4 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Sun, 13 Apr 2014 18:38:49 -0400 Subject: [PATCH] queue: Improve the constructor to take multiple flags at once It really should have been like this from the beginning. Signed-off-by: Anna Schumaker --- include/queue.h | 6 ++++-- lib/audio.cpp | 5 +---- lib/playlist.cpp | 4 +--- lib/queue.cpp | 8 ++++---- tests/src/queue.cpp | 16 ++++++++++++++++ 5 files changed, 26 insertions(+), 13 deletions(-) diff --git a/include/queue.h b/include/queue.h index 66303f7d..95399aea 100644 --- a/include/queue.h +++ b/include/queue.h @@ -13,10 +13,12 @@ enum queue_flags { Q_ENABLED = (1 << 0), Q_RANDOM = (1 << 1), Q_REPEAT = (1 << 2), - Q_NEVER_SORT = (1 << 3), + Q_NO_SORT = (1 << 3), Q_DISABLE_CHANGED_SIZE = (1 << 31), }; +static const unsigned int Q_FLAG_MASK = Q_ENABLED | Q_RANDOM | Q_REPEAT | Q_NO_SORT | Q_DISABLE_CHANGED_SIZE; + struct sort_info { sort_t field; bool ascending; @@ -35,7 +37,7 @@ protected: public: Queue(); - Queue(queue_flags); + Queue(unsigned int); ~Queue(); void write(File &); void read(File &); diff --git a/lib/audio.cpp b/lib/audio.cpp index 9557b4a0..2a9f316f 100644 --- a/lib/audio.cpp +++ b/lib/audio.cpp @@ -19,7 +19,7 @@ static bool o_pause_enabled = false; static unsigned int o_pause_count = 0; static bool o_should_pause = false; -static Queue o_recently_played(Q_ENABLED); +static Queue o_recently_played(Q_ENABLED | Q_REPEAT | Q_NO_SORT | Q_DISABLE_CHANGED_SIZE); static File f_cur_track("cur_track", FILE_TYPE_DATA); static void parse_error(GstMessage *error) @@ -117,9 +117,6 @@ void audio :: init(int *argc, char ***argv) { GstBus *bus; - o_recently_played.set_flag(Q_REPEAT); - o_recently_played.set_flag(Q_NEVER_SORT); - o_recently_played.set_flag(Q_DISABLE_CHANGED_SIZE); gst_init(argc, argv); ocarina_player = gst_element_factory_make("playbin", "ocarina_player"); diff --git a/lib/playlist.cpp b/lib/playlist.cpp index cf338dae..567cf926 100644 --- a/lib/playlist.cpp +++ b/lib/playlist.cpp @@ -8,7 +8,7 @@ static std::set empty_set; static Index playlist_db("playlist.db", false); -static Queue playlist_pq(Q_ENABLED); +static Queue playlist_pq(Q_ENABLED | Q_REPEAT | Q_NO_SORT); static std::string cur_pq; static void import_ban_track(unsigned int track_id) @@ -24,8 +24,6 @@ void playlist :: init() playlist_pq.add_sort(SORT_ARTIST); playlist_pq.add_sort(SORT_YEAR); playlist_pq.add_sort(SORT_TRACK); - playlist_pq.set_flag(Q_REPEAT); - playlist_pq.set_flag(Q_NEVER_SORT); get_callbacks()->on_library_import_ban = import_ban_track; diff --git a/lib/queue.cpp b/lib/queue.cpp index 186cb520..c153fb5d 100644 --- a/lib/queue.cpp +++ b/lib/queue.cpp @@ -18,8 +18,8 @@ Queue :: Queue() { } -Queue :: Queue(queue_flags f) - : _cur(-1), _flags(f), _length(0) +Queue :: Queue(unsigned int f) + : _cur(-1), _flags(f & Q_FLAG_MASK), _length(0) { } @@ -253,7 +253,7 @@ void Queue :: _add_sort(sort_t field, bool ascending) void Queue :: add_sort(sort_t field, bool ascending) { - if (_flags & Q_NEVER_SORT) + if (_flags & Q_NO_SORT) return; _add_sort(field, ascending); @@ -266,7 +266,7 @@ void Queue :: add_sort(sort_t field, bool ascending) void Queue :: reset_sort(sort_t field, bool ascending) { - if (_flags & Q_NEVER_SORT) + if (_flags & Q_NO_SORT) return; if (_sort_order.front().field != field) _sort_order.clear(); diff --git a/tests/src/queue.cpp b/tests/src/queue.cpp index 31cbca59..706e6f68 100644 --- a/tests/src/queue.cpp +++ b/tests/src/queue.cpp @@ -4,15 +4,19 @@ #include #include "test.h" + class TestQueue : public Queue { public: + TestQueue() : Queue() {} + TestQueue(unsigned int f) : Queue(f) {} unsigned int get_cur() { return _cur; } unsigned int get_flags() { return _flags; } unsigned int get_length() { return _length; } std::list get_sorder() { return _sort_order; }; }; + void test_default() { TestQueue q; @@ -24,8 +28,20 @@ void test_default() test :: equal(q.next(), (Track *)NULL); } +void test_constructor(unsigned int flags) +{ + TestQueue q(flags | (1 << 30)); + + test :: equal(q.get_cur(), (unsigned)-1); + test :: equal(q.get_flags(), flags); + test :: equal(q.get_length(), (unsigned)0); + test :: equal(q.get_sorder().size(), (size_t)0); + test :: equal(q.next(), (Track *)NULL); +} + int main(int argc, char **argv) { test_default(); + test_constructor(Q_ENABLED | Q_RANDOM); return 0; }