PlaylistError: add exception class wrapping enum PlaylistResult

This commit is contained in:
Max Kellermann
2015-12-18 09:21:11 +01:00
parent 7562c5751c
commit 8bb5a565cd
8 changed files with 132 additions and 134 deletions

View File

@@ -214,15 +214,13 @@ protected:
unsigned song, const DetachedSong **queued_p);
public:
PlaylistResult DeletePosition(PlayerControl &pc,
unsigned position);
void DeletePosition(PlayerControl &pc, unsigned position);
PlaylistResult DeleteOrder(PlayerControl &pc,
unsigned order) {
return DeletePosition(pc, queue.OrderToPosition(order));
void DeleteOrder(PlayerControl &pc, unsigned order) {
DeletePosition(pc, queue.OrderToPosition(order));
}
PlaylistResult DeleteId(PlayerControl &pc, unsigned id);
void DeleteId(PlayerControl &pc, unsigned id);
/**
* Deletes a range of songs from the playlist.
@@ -230,31 +228,27 @@ public:
* @param start the position of the first song to delete
* @param end the position after the last song to delete
*/
PlaylistResult DeleteRange(PlayerControl &pc,
unsigned start, unsigned end);
void DeleteRange(PlayerControl &pc, unsigned start, unsigned end);
void DeleteSong(PlayerControl &pc, const char *uri);
void Shuffle(PlayerControl &pc, unsigned start, unsigned end);
PlaylistResult MoveRange(PlayerControl &pc,
unsigned start, unsigned end, int to);
void MoveRange(PlayerControl &pc, unsigned start,
unsigned end, int to);
PlaylistResult MoveId(PlayerControl &pc, unsigned id, int to);
void MoveId(PlayerControl &pc, unsigned id, int to);
PlaylistResult SwapPositions(PlayerControl &pc,
unsigned song1, unsigned song2);
void SwapPositions(PlayerControl &pc, unsigned song1, unsigned song2);
PlaylistResult SwapIds(PlayerControl &pc,
unsigned id1, unsigned id2);
void SwapIds(PlayerControl &pc, unsigned id1, unsigned id2);
PlaylistResult SetPriorityRange(PlayerControl &pc,
unsigned start_position,
unsigned end_position,
uint8_t priority);
void SetPriorityRange(PlayerControl &pc,
unsigned start_position, unsigned end_position,
uint8_t priority);
PlaylistResult SetPriorityId(PlayerControl &pc,
unsigned song_id, uint8_t priority);
void SetPriorityId(PlayerControl &pc,
unsigned song_id, uint8_t priority);
/**
* Sets the start_time and end_time attributes on the song
@@ -270,11 +264,11 @@ public:
void Stop(PlayerControl &pc);
PlaylistResult PlayPosition(PlayerControl &pc, int position);
void PlayPosition(PlayerControl &pc, int position);
void PlayOrder(PlayerControl &pc, int order);
PlaylistResult PlayId(PlayerControl &pc, int id);
void PlayId(PlayerControl &pc, int id);
void PlayNext(PlayerControl &pc);

View File

@@ -56,7 +56,7 @@ playlist::Stop(PlayerControl &pc)
}
}
PlaylistResult
void
playlist::PlayPosition(PlayerControl &pc, int song)
{
pc.LockClearError();
@@ -66,13 +66,13 @@ playlist::PlayPosition(PlayerControl &pc, int song)
/* play any song ("current" song, or the first song */
if (queue.IsEmpty())
return PlaylistResult::SUCCESS;
return;
if (playing) {
/* already playing: unpause playback, just in
case it was paused, and return */
pc.LockSetPause(false);
return PlaylistResult::SUCCESS;
return;
}
/* select a song: "current" song, or the first one */
@@ -80,7 +80,7 @@ playlist::PlayPosition(PlayerControl &pc, int song)
? current
: 0;
} else if (!queue.IsValidPosition(song))
return PlaylistResult::BAD_RANGE;
throw PlaylistError::BadRange();
if (queue.random) {
if (song >= 0)
@@ -103,18 +103,19 @@ playlist::PlayPosition(PlayerControl &pc, int song)
error_count = 0;
PlayOrder(pc, i);
return PlaylistResult::SUCCESS;
}
PlaylistResult
void
playlist::PlayId(PlayerControl &pc, int id)
{
if (id == -1)
return PlayPosition(pc, id);
if (id == -1) {
PlayPosition(pc, id);
return;
}
int song = queue.IdToPosition(id);
if (song < 0)
return PlaylistResult::NO_SUCH_SONG;
throw PlaylistError::NoSuchSong();
return PlayPosition(pc, song);
}

View File

@@ -138,11 +138,11 @@ playlist::AppendURI(PlayerControl &pc, const SongLoader &loader,
return result;
}
PlaylistResult
void
playlist::SwapPositions(PlayerControl &pc, unsigned song1, unsigned song2)
{
if (!queue.IsValidPosition(song1) || !queue.IsValidPosition(song2))
return PlaylistResult::BAD_RANGE;
throw PlaylistError::BadRange();
const DetachedSong *const queued_song = GetQueuedSong();
@@ -165,35 +165,33 @@ playlist::SwapPositions(PlayerControl &pc, unsigned song1, unsigned song2)
UpdateQueuedSong(pc, queued_song);
OnModified();
return PlaylistResult::SUCCESS;
}
PlaylistResult
void
playlist::SwapIds(PlayerControl &pc, unsigned id1, unsigned id2)
{
int song1 = queue.IdToPosition(id1);
int song2 = queue.IdToPosition(id2);
if (song1 < 0 || song2 < 0)
return PlaylistResult::NO_SUCH_SONG;
throw PlaylistError::NoSuchSong();
return SwapPositions(pc, song1, song2);
SwapPositions(pc, song1, song2);
}
PlaylistResult
void
playlist::SetPriorityRange(PlayerControl &pc,
unsigned start, unsigned end,
uint8_t priority)
{
if (start >= GetLength())
return PlaylistResult::BAD_RANGE;
throw PlaylistError::BadRange();
if (end > GetLength())
end = GetLength();
if (start >= end)
return PlaylistResult::SUCCESS;
return;
/* remember "current" and "queued" */
@@ -211,21 +209,17 @@ playlist::SetPriorityRange(PlayerControl &pc,
UpdateQueuedSong(pc, queued_song);
OnModified();
return PlaylistResult::SUCCESS;
}
PlaylistResult
void
playlist::SetPriorityId(PlayerControl &pc,
unsigned song_id, uint8_t priority)
{
int song_position = queue.IdToPosition(song_id);
if (song_position < 0)
return PlaylistResult::NO_SUCH_SONG;
return SetPriorityRange(pc, song_position, song_position + 1,
priority);
throw PlaylistError::NoSuchSong();
SetPriorityRange(pc, song_position, song_position + 1, priority);
}
void
@@ -272,11 +266,11 @@ playlist::DeleteInternal(PlayerControl &pc,
current--;
}
PlaylistResult
void
playlist::DeletePosition(PlayerControl &pc, unsigned song)
{
if (song >= queue.GetLength())
return PlaylistResult::BAD_RANGE;
throw PlaylistError::BadRange();
const DetachedSong *queued_song = GetQueuedSong();
@@ -284,21 +278,19 @@ playlist::DeletePosition(PlayerControl &pc, unsigned song)
UpdateQueuedSong(pc, queued_song);
OnModified();
return PlaylistResult::SUCCESS;
}
PlaylistResult
void
playlist::DeleteRange(PlayerControl &pc, unsigned start, unsigned end)
{
if (start >= queue.GetLength())
return PlaylistResult::BAD_RANGE;
throw PlaylistError::BadRange();
if (end > queue.GetLength())
end = queue.GetLength();
if (start >= end)
return PlaylistResult::SUCCESS;
return;
const DetachedSong *queued_song = GetQueuedSong();
@@ -308,18 +300,16 @@ playlist::DeleteRange(PlayerControl &pc, unsigned start, unsigned end)
UpdateQueuedSong(pc, queued_song);
OnModified();
return PlaylistResult::SUCCESS;
}
PlaylistResult
void
playlist::DeleteId(PlayerControl &pc, unsigned id)
{
int song = queue.IdToPosition(id);
if (song < 0)
return PlaylistResult::NO_SUCH_SONG;
throw PlaylistError::NoSuchSong();
return DeletePosition(pc, song);
DeletePosition(pc, song);
}
void
@@ -330,19 +320,19 @@ playlist::DeleteSong(PlayerControl &pc, const char *uri)
DeletePosition(pc, i);
}
PlaylistResult
void
playlist::MoveRange(PlayerControl &pc, unsigned start, unsigned end, int to)
{
if (!queue.IsValidPosition(start) || !queue.IsValidPosition(end - 1))
return PlaylistResult::BAD_RANGE;
throw PlaylistError::BadRange();
if ((to >= 0 && to + end - start - 1 >= GetLength()) ||
(to < 0 && unsigned(abs(to)) > GetLength()))
return PlaylistResult::BAD_RANGE;
throw PlaylistError::BadRange();
if ((int)start == to)
/* nothing happens */
return PlaylistResult::SUCCESS;
return;
const DetachedSong *const queued_song = GetQueuedSong();
@@ -355,11 +345,11 @@ playlist::MoveRange(PlayerControl &pc, unsigned start, unsigned end, int to)
if (currentSong < 0)
/* can't move relative to current song,
because there is no current song */
return PlaylistResult::BAD_RANGE;
throw PlaylistError::BadRange();
if (start <= (unsigned)currentSong && (unsigned)currentSong < end)
/* no-op, can't be moved to offset of itself */
return PlaylistResult::SUCCESS;
return;
to = (currentSong + abs(to)) % GetLength();
if (start < (unsigned)to)
to--;
@@ -379,18 +369,16 @@ playlist::MoveRange(PlayerControl &pc, unsigned start, unsigned end, int to)
UpdateQueuedSong(pc, queued_song);
OnModified();
return PlaylistResult::SUCCESS;
}
PlaylistResult
void
playlist::MoveId(PlayerControl &pc, unsigned id1, int to)
{
int song = queue.IdToPosition(id1);
if (song < 0)
return PlaylistResult::NO_SUCH_SONG;
throw PlaylistError::NoSuchSong();
return MoveRange(pc, song, song + 1, to);
MoveRange(pc, song, song + 1, to);
}
void