Queue: add "noexcept"

This commit is contained in:
Max Kellermann 2017-11-26 12:25:53 +01:00
parent 2f32df1f09
commit a6910313b4
2 changed files with 40 additions and 39 deletions

View File

@ -21,7 +21,7 @@
#include "Queue.hxx" #include "Queue.hxx"
#include "DetachedSong.hxx" #include "DetachedSong.hxx"
Queue::Queue(unsigned _max_length) Queue::Queue(unsigned _max_length) noexcept
:max_length(_max_length), :max_length(_max_length),
items(new Item[max_length]), items(new Item[max_length]),
order(new unsigned[max_length]), order(new unsigned[max_length]),
@ -29,7 +29,7 @@ Queue::Queue(unsigned _max_length)
{ {
} }
Queue::~Queue() Queue::~Queue() noexcept
{ {
Clear(); Clear();
@ -79,7 +79,7 @@ Queue::ModifyAtOrder(unsigned _order) noexcept
} }
unsigned unsigned
Queue::Append(DetachedSong &&song, uint8_t priority) Queue::Append(DetachedSong &&song, uint8_t priority) noexcept
{ {
assert(!IsFull()); assert(!IsFull());
@ -295,7 +295,7 @@ queue_sort_order_by_priority(Queue *queue,
} }
void void
Queue::ShuffleOrderRange(unsigned start, unsigned end) Queue::ShuffleOrderRange(unsigned start, unsigned end) noexcept
{ {
assert(random); assert(random);
assert(start <= end); assert(start <= end);
@ -310,7 +310,7 @@ Queue::ShuffleOrderRange(unsigned start, unsigned end)
* priority group. * priority group.
*/ */
void void
Queue::ShuffleOrderRangeWithPriority(unsigned start, unsigned end) Queue::ShuffleOrderRangeWithPriority(unsigned start, unsigned end) noexcept
{ {
assert(random); assert(random);
assert(start <= end); assert(start <= end);
@ -344,13 +344,13 @@ Queue::ShuffleOrderRangeWithPriority(unsigned start, unsigned end)
} }
void void
Queue::ShuffleOrder() Queue::ShuffleOrder() noexcept
{ {
ShuffleOrderRangeWithPriority(0, length); ShuffleOrderRangeWithPriority(0, length);
} }
void void
Queue::ShuffleOrderFirst(unsigned start, unsigned end) Queue::ShuffleOrderFirst(unsigned start, unsigned end) noexcept
{ {
rand.AutoCreate(); rand.AutoCreate();
@ -359,7 +359,7 @@ Queue::ShuffleOrderFirst(unsigned start, unsigned end)
} }
void void
Queue::ShuffleOrderLast(unsigned start, unsigned end) Queue::ShuffleOrderLast(unsigned start, unsigned end) noexcept
{ {
rand.AutoCreate(); rand.AutoCreate();
@ -368,7 +368,7 @@ Queue::ShuffleOrderLast(unsigned start, unsigned end)
} }
void void
Queue::ShuffleRange(unsigned start, unsigned end) Queue::ShuffleRange(unsigned start, unsigned end) noexcept
{ {
assert(start <= end); assert(start <= end);
assert(end <= length); assert(end <= length);
@ -418,7 +418,7 @@ Queue::CountSamePriority(unsigned start_order, uint8_t priority) const noexcept
bool bool
Queue::SetPriority(unsigned position, uint8_t priority, int after_order, Queue::SetPriority(unsigned position, uint8_t priority, int after_order,
bool reorder) bool reorder) noexcept
{ {
assert(position < length); assert(position < length);
@ -478,7 +478,7 @@ Queue::SetPriority(unsigned position, uint8_t priority, int after_order,
bool bool
Queue::SetPriorityRange(unsigned start_position, unsigned end_position, Queue::SetPriorityRange(unsigned start_position, unsigned end_position,
uint8_t priority, int after_order) uint8_t priority, int after_order) noexcept
{ {
assert(start_position <= end_position); assert(start_position <= end_position);
assert(end_position <= length); assert(end_position <= length);

View File

@ -103,18 +103,18 @@ struct Queue {
/** random number generator for shuffle and random mode */ /** random number generator for shuffle and random mode */
LazyRandomEngine rand; LazyRandomEngine rand;
explicit Queue(unsigned max_length); explicit Queue(unsigned max_length) noexcept;
/** /**
* Deinitializes a queue object. It does not free the queue * Deinitializes a queue object. It does not free the queue
* pointer itself. * pointer itself.
*/ */
~Queue(); ~Queue() noexcept;
Queue(const Queue &) = delete; Queue(const Queue &) = delete;
Queue &operator=(const Queue &) = delete; Queue &operator=(const Queue &) = delete;
unsigned GetLength() const { unsigned GetLength() const noexcept {
assert(length <= max_length); assert(length <= max_length);
return length; return length;
@ -123,14 +123,14 @@ struct Queue {
/** /**
* Determine if the queue is empty, i.e. there are no songs. * Determine if the queue is empty, i.e. there are no songs.
*/ */
bool IsEmpty() const { bool IsEmpty() const noexcept {
return length == 0; return length == 0;
} }
/** /**
* Determine if the maximum number of songs has been reached. * Determine if the maximum number of songs has been reached.
*/ */
bool IsFull() const { bool IsFull() const noexcept {
assert(length <= max_length); assert(length <= max_length);
return length >= max_length; return length >= max_length;
@ -139,23 +139,22 @@ struct Queue {
/** /**
* Is that a valid position number? * Is that a valid position number?
*/ */
bool IsValidPosition(unsigned position) const { bool IsValidPosition(unsigned position) const noexcept {
return position < length; return position < length;
} }
/** /**
* Is that a valid order number? * Is that a valid order number?
*/ */
bool IsValidOrder(unsigned _order) const { bool IsValidOrder(unsigned _order) const noexcept {
return _order < length; return _order < length;
} }
int IdToPosition(unsigned id) const { int IdToPosition(unsigned id) const noexcept {
return id_table.IdToPosition(id); return id_table.IdToPosition(id);
} }
int PositionToId(unsigned position) const int PositionToId(unsigned position) const noexcept {
{
assert(position < length); assert(position < length);
return items[position].id; return items[position].id;
@ -187,20 +186,20 @@ struct Queue {
return items[position].priority; return items[position].priority;
} }
const Item &GetOrderItem(unsigned i) const { const Item &GetOrderItem(unsigned i) const noexcept {
assert(IsValidOrder(i)); assert(IsValidOrder(i));
return items[OrderToPosition(i)]; return items[OrderToPosition(i)];
} }
uint8_t GetOrderPriority(unsigned i) const { uint8_t GetOrderPriority(unsigned i) const noexcept {
return GetOrderItem(i).priority; return GetOrderItem(i).priority;
} }
/** /**
* Returns the song at the specified position. * Returns the song at the specified position.
*/ */
DetachedSong &Get(unsigned position) const { DetachedSong &Get(unsigned position) const noexcept {
assert(position < length); assert(position < length);
return *items[position].song; return *items[position].song;
@ -209,7 +208,7 @@ struct Queue {
/** /**
* Returns the song at the specified order number. * Returns the song at the specified order number.
*/ */
DetachedSong &GetOrder(unsigned _order) const { DetachedSong &GetOrder(unsigned _order) const noexcept {
return Get(OrderToPosition(_order)); return Get(OrderToPosition(_order));
} }
@ -217,7 +216,8 @@ struct Queue {
* Is the song at the specified position newer than the specified * Is the song at the specified position newer than the specified
* version? * version?
*/ */
bool IsNewerAtPosition(unsigned position, uint32_t _version) const { bool IsNewerAtPosition(unsigned position,
uint32_t _version) const noexcept {
assert(position < length); assert(position < length);
return _version > version || return _version > version ||
@ -245,7 +245,7 @@ struct Queue {
* IncrementVersion() after all modifications have been made. * IncrementVersion() after all modifications have been made.
* number. * number.
*/ */
void ModifyAtPosition(unsigned position) { void ModifyAtPosition(unsigned position) noexcept {
assert(position < length); assert(position < length);
items[position].version = version; items[position].version = version;
@ -268,7 +268,7 @@ struct Queue {
* *
* @param priority the priority of this new queue item * @param priority the priority of this new queue item
*/ */
unsigned Append(DetachedSong &&song, uint8_t priority); unsigned Append(DetachedSong &&song, uint8_t priority) noexcept;
/** /**
* Swaps two songs, addressed by their position. * Swaps two songs, addressed by their position.
@ -278,7 +278,7 @@ struct Queue {
/** /**
* Swaps two songs, addressed by their order number. * Swaps two songs, addressed by their order number.
*/ */
void SwapOrders(unsigned order1, unsigned order2) { void SwapOrders(unsigned order1, unsigned order2) noexcept {
std::swap(order[order1], order[order2]); std::swap(order[order1], order[order2]);
} }
@ -330,7 +330,7 @@ struct Queue {
/** /**
* Initializes the "order" array, and restores "normal" order. * Initializes the "order" array, and restores "normal" order.
*/ */
void RestoreOrder() { void RestoreOrder() noexcept {
for (unsigned i = 0; i < length; ++i) for (unsigned i = 0; i < length; ++i)
order[i] = i; order[i] = i;
} }
@ -339,43 +339,44 @@ struct Queue {
* Shuffle the order of items in the specified range, ignoring * Shuffle the order of items in the specified range, ignoring
* their priorities. * their priorities.
*/ */
void ShuffleOrderRange(unsigned start, unsigned end); void ShuffleOrderRange(unsigned start, unsigned end) noexcept;
/** /**
* Shuffle the order of items in the specified range, taking their * Shuffle the order of items in the specified range, taking their
* priorities into account. * priorities into account.
*/ */
void ShuffleOrderRangeWithPriority(unsigned start, unsigned end); void ShuffleOrderRangeWithPriority(unsigned start,
unsigned end) noexcept;
/** /**
* Shuffles the virtual order of songs, but does not move them * Shuffles the virtual order of songs, but does not move them
* physically. This is used in random mode. * physically. This is used in random mode.
*/ */
void ShuffleOrder(); void ShuffleOrder() noexcept;
void ShuffleOrderFirst(unsigned start, unsigned end); void ShuffleOrderFirst(unsigned start, unsigned end) noexcept;
/** /**
* Shuffles the virtual order of the last song in the specified * Shuffles the virtual order of the last song in the specified
* (order) range. This is used in random mode after a song has been * (order) range. This is used in random mode after a song has been
* appended by queue_append(). * appended by queue_append().
*/ */
void ShuffleOrderLast(unsigned start, unsigned end); void ShuffleOrderLast(unsigned start, unsigned end) noexcept;
/** /**
* Shuffles a (position) range in the queue. The songs are physically * Shuffles a (position) range in the queue. The songs are physically
* shuffled, not by using the "order" mapping. * shuffled, not by using the "order" mapping.
*/ */
void ShuffleRange(unsigned start, unsigned end); void ShuffleRange(unsigned start, unsigned end) noexcept;
bool SetPriority(unsigned position, uint8_t priority, int after_order, bool SetPriority(unsigned position, uint8_t priority, int after_order,
bool reorder=true); bool reorder=true) noexcept;
bool SetPriorityRange(unsigned start_position, unsigned end_position, bool SetPriorityRange(unsigned start_position, unsigned end_position,
uint8_t priority, int after_order); uint8_t priority, int after_order) noexcept;
private: private:
void MoveItemTo(unsigned from, unsigned to) { void MoveItemTo(unsigned from, unsigned to) noexcept {
unsigned from_id = items[from].id; unsigned from_id = items[from].id;
items[to] = items[from]; items[to] = items[from];