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

And remove the deck :: write() and TempQueue :: write() functions while
we're at it.

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-12-14 11:08:17 -05:00
parent b919843717
commit 2102b1bfc9
4 changed files with 72 additions and 51 deletions

View File

@ -12,13 +12,6 @@ static std::list<TempQueue> queue_deck;
static struct file deck_file;
void TempQueue :: write(file &file)
{
file_writef(&file, "%u %zu", q_flags, queue_size(this));
for (unsigned int i = 0; i < queue_size(this); i++)
file_writef(&file, " %u", queue_at(this, i)->tr_dbe.dbe_index);
}
void TempQueue :: read(file &file)
{
unsigned int n, id;
@ -32,20 +25,20 @@ void TempQueue :: read(file &file)
unsigned int TempQueue :: add(struct track *track)
{
unsigned int res = queue_add(this, track);
deck :: write();
tempq_save(this, Q_ENABLED);
return res;
}
void TempQueue :: del(struct track *track)
{
queue_remove_all(this, track);
deck :: write();
tempq_save(this, Q_ENABLED);
}
void TempQueue :: del(unsigned int id)
{
queue_remove(this, id);
deck :: write();
tempq_save(this, Q_ENABLED);
}
@ -88,6 +81,27 @@ void tempq_deinit()
queue_deck.erase(queue_deck.begin());
}
void tempq_save(struct queue *queue, enum queue_flags flag)
{
std::list<TempQueue>::iterator it;
unsigned int i;
if (!file_open(&deck_file, OPEN_WRITE))
return;
file_writef(&deck_file, "%zu\n", queue_deck.size());
for (it = queue_deck.begin(); it != queue_deck.end(); it++) {
file_writef(&deck_file, "%u %zu", it->q_flags, queue_size(&(*it)));
for (i = 0; i < queue_size(&(*it)); i++) {
file_writef(&deck_file, " %u",
queue_at(&(*it), i)->tr_dbe.dbe_index);
}
file_writef(&deck_file, "\n");
}
file_close(&deck_file);
}
struct queue *tempq_alloc(struct queue_ops *ops, unsigned int flags)
{
struct queue *queue;
@ -95,6 +109,7 @@ struct queue *tempq_alloc(struct queue_ops *ops, unsigned int flags)
queue_deck.push_back(TempQueue());
queue = &queue_deck.back();
queue_init(queue, flags | Q_ENABLED | Q_SAVE_FLAGS | Q_SAVE_SORT, ops);
tempq_save(queue, Q_ENABLED);
return queue;
}
@ -106,7 +121,7 @@ void tempq_free(struct queue *queue)
if (&(*it) == queue) {
queue_deinit(&(*it));
queue_deck.erase(it);
deck :: write();
tempq_save(NULL, Q_ENABLED);
return;
}
}
@ -145,7 +160,7 @@ void tempq_move(struct queue *queue, unsigned int index)
it_new++;
queue_deck.splice(it_new, queue_deck, it_old);
deck :: write();
tempq_save(queue, Q_ENABLED);
}
struct track *tempq_next()
@ -166,27 +181,6 @@ struct track *tempq_next()
return track;
}
void deck :: write()
{
std::list<TempQueue>::iterator it;
if (!file_open(&deck_file, OPEN_WRITE))
return;
file_writef(&deck_file, "%zu\n", queue_deck.size());
for (it = queue_deck.begin(); it != queue_deck.end(); it++) {
it->write(deck_file);
file_writef(&deck_file, "\n");
}
file_close(&deck_file);
}
void deck :: save(struct queue *queue, enum queue_flags flag)
{
deck :: write();
}
unsigned int tempq_count()
{
return queue_deck.size();

View File

@ -24,19 +24,19 @@ static compare_t sort_fields[] = {
static void tempq_added(struct queue *queue, unsigned int pos)
{
find_tab(queue)->on_track_added(pos);
deck :: write();
tempq_save(queue, Q_ENABLED);
}
static void tempq_removed(struct queue *queue, unsigned int pos)
{
find_tab(queue)->on_track_removed(pos);
deck :: write();
tempq_save(queue, Q_ENABLED);
}
static void tempq_cleared(struct queue *queue, unsigned int n)
{
find_tab(queue)->on_tracks_cleared(n);
deck :: write();
tempq_save(queue, Q_ENABLED);
}
static void tempq_updated(struct queue *queue, unsigned int pos)
@ -48,7 +48,7 @@ struct queue_ops tempq_ops = {
tempq_added,
tempq_removed,
tempq_cleared,
deck :: save,
tempq_save,
tempq_updated,
};

View File

@ -18,7 +18,6 @@ class TempQueue : public queue
{
public:
void read(file &);
void write(file &);
unsigned int add(struct track *);
void del(struct track *);
@ -37,17 +36,6 @@ public:
* ...
* ... << deck[N] <<< endl;
*/
namespace deck
{
/**
* Save the current queues to a file on disk.
*/
void write();
void save(struct queue *, enum queue_flags);
};
/* Called to initialize the temporary queue manager. */
void tempq_init(struct queue_ops *);
@ -55,6 +43,9 @@ void tempq_init(struct queue_ops *);
/* Called to deinitialize the temporary queue manager. */
void tempq_deinit();
/* Called to save the temporary queue list. */
void tempq_save(struct queue *, enum queue_flags);
/* Called to allocate a new temporary queue. */
struct queue *tempq_alloc(struct queue_ops *, unsigned int);

View File

@ -29,7 +29,7 @@ static void test_init()
test_equal(tempq_count(), 0);
}
static void test_tempq()
static void test_alloc()
{
struct queue *q0, *q1;
@ -51,6 +51,21 @@ static void test_tempq()
test_equal(tempq_get(1), q1);
test_equal(tempq_count(), 2);
tempq_deinit();
test_equal(tempq_count(), 0);
tempq_init(NULL);
q0 = tempq_get(0);
q1 = tempq_get(1);
test_equal(queue_has_flag(q0, Q_RANDOM), false);
test_equal(queue_has_flag(q1, Q_RANDOM), true);
}
static void test_move()
{
struct queue *q0 = tempq_get(0);
struct queue *q1 = tempq_get(1);
tempq_move(q1, 0);
test_equal(tempq_get(0), q1);
test_equal(tempq_get(1), q0);
@ -61,6 +76,21 @@ static void test_tempq()
test_equal(tempq_get(1), q0);
test_equal(tempq_get(2), NULL);
tempq_deinit();
test_equal(tempq_count(), 0);
tempq_init(NULL);
q0 = tempq_get(1);
q1 = tempq_get(0);
test_equal(queue_has_flag(q0, Q_RANDOM), false);
test_equal(queue_has_flag(q1, Q_RANDOM), true);
}
static void test_free()
{
struct queue *q0 = tempq_get(1);
struct queue *q1 = tempq_get(0);
tempq_free(q0);
test_equal(tempq_get(0), q1);
test_equal(tempq_count(), 1);
@ -68,6 +98,10 @@ static void test_tempq()
tempq_free(q1);
test_equal(tempq_get(0), NULL);
test_equal(tempq_count(), 0);
tempq_deinit();
tempq_init(NULL);
test_equal(tempq_count(), 0);
}
static void test_next()
@ -111,6 +145,8 @@ static void test_next()
DECLARE_UNIT_TESTS(
UNIT_TEST("Temporary Queue Initialization", test_init),
UNIT_TEST("Temporary Queue", test_tempq),
UNIT_TEST("Temporary Queue Alloc", test_alloc),
UNIT_TEST("Temporary Queue Move", test_move),
UNIT_TEST("Temporary Queue Free", test_free),
UNIT_TEST("Temporary Queue Next Track", test_next),
);