core/tempq: Move tempq_alloc() out of the deck namespace

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-12-14 09:05:54 -05:00
parent 8c7550d5c3
commit bce2ba33f7
4 changed files with 41 additions and 29 deletions

View File

@ -12,13 +12,6 @@ static std::list<TempQueue> 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<TempQueue>::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<TempQueue>::iterator &it)
{
queue_deck.erase(it);

View File

@ -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;

View File

@ -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 */

View File

@ -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),
);