diff --git a/core/deck.cpp b/core/deck.cpp index 720be476..0b2f86cb 100644 --- a/core/deck.cpp +++ b/core/deck.cpp @@ -12,13 +12,6 @@ static std::list queue_deck; static struct file deck_file; -TempQueue :: TempQueue(bool random, struct queue_ops *ops) -{ - unsigned int flags = Q_ENABLED | Q_SAVE_FLAGS | Q_SAVE_SORT; - queue_init(this, flags | (random ? Q_RANDOM : 0), ops); -} - - void TempQueue :: write(file &file) { file_writef(&file, "%u %zu", q_flags, queue_size(this)); @@ -68,13 +61,22 @@ void tempq_init(struct queue_ops *ops) file_readf(&deck_file, "%u", &num); for (i = 0; i < num; i++) { - queue_deck.push_back(TempQueue(false, NULL)); - queue_deck.back().read(deck_file); + ((TempQueue *)tempq_alloc(NULL, 0))->read(deck_file); queue_deck.back().q_ops = ops; } file_close(&deck_file); } +struct queue *tempq_alloc(struct queue_ops *ops, unsigned int flags) +{ + struct queue *queue; + + queue_deck.push_back(TempQueue()); + queue = &queue_deck.back(); + queue_init(queue, flags | Q_ENABLED | Q_SAVE_FLAGS | Q_SAVE_SORT, ops); + return queue; +} + void deck :: write() { std::list::iterator it; @@ -96,12 +98,6 @@ void deck :: save(struct queue *queue, enum queue_flags flag) deck :: write(); } -queue *deck :: create(bool random, struct queue_ops *ops) -{ - queue_deck.push_back(TempQueue(random, ops)); - return &queue_deck.back(); -} - static void _destroy(std::list::iterator &it) { queue_deck.erase(it); diff --git a/gui/tabs.cpp b/gui/tabs.cpp index 58dbced4..354ff5d1 100644 --- a/gui/tabs.cpp +++ b/gui/tabs.cpp @@ -202,10 +202,11 @@ void Tab :: tab_queue_add(queue *pq) bool Tab :: tab_queue_selected(bool random) { + unsigned int flags = random ? 0 : Q_RANDOM; if (deck :: get_queues().size() >= 10) return true; - queue *pq = deck :: create(random, &tempq_ops); + queue *pq = tempq_alloc(&tempq_ops, flags); on_pq_created(pq, deck :: get_queues().size() - 1); tab_queue_add(pq); return true; diff --git a/include/core/deck.h b/include/core/deck.h index 0e6087a0..b44128ae 100644 --- a/include/core/deck.h +++ b/include/core/deck.h @@ -17,8 +17,6 @@ extern "C" { class TempQueue : public queue { public: - TempQueue(bool, struct queue_ops *); - void read(file &); void write(file &); @@ -48,15 +46,6 @@ namespace deck void write(); void save(struct queue *, enum queue_flags); - /** - * Create a new queue at the end of the deck. - * - * @param random Set to true if the new queue should return a random - * track when queue->next() is called. - * @return The newly created queue. - */ - queue *create(bool, struct queue_ops *); - /** * Removes the queue from the deck. * @@ -105,4 +94,8 @@ namespace deck /* Called to initialize the temporary queue manager. */ void tempq_init(struct queue_ops *); + +/* Called to allocate a new temporary queue. */ +struct queue *tempq_alloc(struct queue_ops *, unsigned int); + #endif /* OCARINA_CORE_DECK_H */ diff --git a/tests/core/deck.cpp b/tests/core/deck.cpp index 3b3a397c..f8fb7693 100644 --- a/tests/core/deck.cpp +++ b/tests/core/deck.cpp @@ -28,7 +28,28 @@ static void test_init() test_equal(deck :: get(0), NULL); test_equal(deck :: index(NULL), 0); test_equal(deck :: get_queues().size(), (size_t)0); +} +static void test_tempq() +{ + struct queue *q0, *q1; + + q0 = tempq_alloc(NULL, 0); + test_not_equal(q0, NULL); + test_equal(queue_has_flag(q0, Q_ENABLED), true); + test_equal(queue_has_flag(q0, Q_RANDOM), false); + test_equal(queue_has_flag(q0, Q_SAVE_SORT), true); + test_equal(queue_has_flag(q0, Q_SAVE_FLAGS), true); + + q1 = tempq_alloc(NULL, Q_RANDOM); + test_not_equal(q1, NULL); + test_equal(queue_has_flag(q1, Q_ENABLED), true); + test_equal(queue_has_flag(q1, Q_RANDOM), true); + test_equal(queue_has_flag(q1, Q_SAVE_SORT), true); + test_equal(queue_has_flag(q1, Q_SAVE_FLAGS), true); + + deck :: destroy(q0); + deck :: destroy(q1); history_deinit(); collection_deinit(); playlist_deinit(); @@ -48,14 +69,14 @@ static void test_create_mv_destroy() history_init(NULL); tempq_init(NULL); - q1 = deck :: create(true, NULL); + q1 = tempq_alloc(NULL, Q_RANDOM); test_not_equal(q1, Q_NULL); test_equal(queue_has_flag(q1, Q_ENABLED), true); test_equal(queue_has_flag(q1, Q_RANDOM), true); test_equal(deck :: index(q1), (unsigned)2); test_equal(deck :: get(2), q1); - q2 = deck :: create(false, NULL); + q2 = tempq_alloc(NULL, 0); test_not_equal(q2, Q_NULL); test_equal(queue_has_flag(q2, Q_ENABLED), true); test_equal(queue_has_flag(q2, Q_RANDOM), false); @@ -118,6 +139,7 @@ static void test_next_prev() DECLARE_UNIT_TESTS( UNIT_TEST("Temporary Queue Initialization", test_init), + UNIT_TEST("Temporary Queue", test_tempq), UNIT_TEST("Deck Create, Move, and Destroy", test_create_mv_destroy), UNIT_TEST("Deck Next and Prev", test_next_prev), );