Queue: add constructor and destructor
This commit is contained in:
		| @@ -70,12 +70,10 @@ struct playlist { | |||||||
| 	int queued; | 	int queued; | ||||||
|  |  | ||||||
| 	playlist(unsigned max_length) | 	playlist(unsigned max_length) | ||||||
| 		:current(-1), queued(-1) { | 		:queue(max_length), current(-1), queued(-1) { | ||||||
| 		queue_init(&queue, max_length); |  | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	~playlist() { | 	~playlist() { | ||||||
| 		queue_finish(&queue); |  | ||||||
| 	} | 	} | ||||||
| }; | }; | ||||||
|  |  | ||||||
|   | |||||||
| @@ -300,40 +300,32 @@ queue_clear(struct queue *queue) | |||||||
| 	queue->length = 0; | 	queue->length = 0; | ||||||
| } | } | ||||||
|  |  | ||||||
| void | queue::queue(unsigned _max_length) | ||||||
| queue_init(struct queue *queue, unsigned max_length) | 	:max_length(_max_length), length(0), | ||||||
|  | 	 version(1), | ||||||
|  | 	 items(g_new(struct queue_item, max_length)), | ||||||
|  | 	 order((unsigned *)g_malloc(sizeof(order[0]) * max_length)), | ||||||
|  | 	 id_to_position((int *)g_malloc(sizeof(id_to_position[0]) * | ||||||
|  | 					max_length * QUEUE_HASH_MULT)), | ||||||
|  | 	 repeat(false), | ||||||
|  | 	 single(false), | ||||||
|  | 	 consume(false), | ||||||
|  | 	 random(false), | ||||||
|  | 	 rand(g_rand_new()) | ||||||
| { | { | ||||||
| 	queue->max_length = max_length; |  | ||||||
| 	queue->length = 0; |  | ||||||
| 	queue->version = 1; |  | ||||||
| 	queue->repeat = false; |  | ||||||
| 	queue->random = false; |  | ||||||
| 	queue->single = false; |  | ||||||
| 	queue->consume = false; |  | ||||||
|  |  | ||||||
| 	queue->items = g_new(struct queue_item, max_length); |  | ||||||
| 	queue->order = (unsigned *) |  | ||||||
| 		g_malloc(sizeof(queue->order[0]) * max_length); |  | ||||||
| 	queue->id_to_position = (int *) |  | ||||||
| 		g_malloc(sizeof(queue->id_to_position[0]) * |  | ||||||
| 			 max_length * QUEUE_HASH_MULT); |  | ||||||
|  |  | ||||||
| 	for (unsigned i = 0; i < max_length * QUEUE_HASH_MULT; ++i) | 	for (unsigned i = 0; i < max_length * QUEUE_HASH_MULT; ++i) | ||||||
| 		queue->id_to_position[i] = -1; | 		id_to_position[i] = -1; | ||||||
|  |  | ||||||
| 	queue->rand = g_rand_new(); |  | ||||||
| } | } | ||||||
|  |  | ||||||
| void | queue::~queue() | ||||||
| queue_finish(struct queue *queue) |  | ||||||
| { | { | ||||||
| 	queue_clear(queue); | 	queue_clear(this); | ||||||
|  |  | ||||||
| 	g_free(queue->items); | 	g_free(items); | ||||||
| 	g_free(queue->order); | 	g_free(order); | ||||||
| 	g_free(queue->id_to_position); | 	g_free(id_to_position); | ||||||
|  |  | ||||||
| 	g_rand_free(queue->rand); | 	g_rand_free(rand); | ||||||
| } | } | ||||||
|  |  | ||||||
| static const struct queue_item * | static const struct queue_item * | ||||||
|   | |||||||
| @@ -99,6 +99,17 @@ struct queue { | |||||||
|  |  | ||||||
| 	/** random number generator for shuffle and random mode */ | 	/** random number generator for shuffle and random mode */ | ||||||
| 	GRand *rand; | 	GRand *rand; | ||||||
|  |  | ||||||
|  | 	queue(unsigned max_length); | ||||||
|  |  | ||||||
|  | 	/** | ||||||
|  | 	 * Deinitializes a queue object.  It does not free the queue | ||||||
|  | 	 * pointer itself. | ||||||
|  | 	 */ | ||||||
|  | 	~queue(); | ||||||
|  |  | ||||||
|  | 	queue(const queue &other) = delete; | ||||||
|  | 	queue &operator=(const queue &other) = delete; | ||||||
| }; | }; | ||||||
|  |  | ||||||
| static inline unsigned | static inline unsigned | ||||||
| @@ -232,19 +243,6 @@ queue_song_newer(const struct queue *queue, unsigned position, | |||||||
| 		queue->items[position].version == 0; | 		queue->items[position].version == 0; | ||||||
| } | } | ||||||
|  |  | ||||||
| /** |  | ||||||
|  * Initialize a queue object. |  | ||||||
|  */ |  | ||||||
| void |  | ||||||
| queue_init(struct queue *queue, unsigned max_length); |  | ||||||
|  |  | ||||||
| /** |  | ||||||
|  * Deinitializes a queue object.  It does not free the queue pointer |  | ||||||
|  * itself. |  | ||||||
|  */ |  | ||||||
| void |  | ||||||
| queue_finish(struct queue *queue); |  | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * Returns the order number following the specified one.  This takes |  * Returns the order number following the specified one.  This takes | ||||||
|  * end of queue and "repeat" mode into account. |  * end of queue and "repeat" mode into account. | ||||||
|   | |||||||
| @@ -50,8 +50,7 @@ main(G_GNUC_UNUSED int argc, G_GNUC_UNUSED char **argv) | |||||||
| { | { | ||||||
| 	struct song songs[16]; | 	struct song songs[16]; | ||||||
|  |  | ||||||
| 	struct queue queue; | 	struct queue queue(32); | ||||||
| 	queue_init(&queue, 32); |  | ||||||
|  |  | ||||||
| 	for (unsigned i = 0; i < G_N_ELEMENTS(songs); ++i) | 	for (unsigned i = 0; i < G_N_ELEMENTS(songs); ++i) | ||||||
| 		queue_append(&queue, &songs[i], 0); | 		queue_append(&queue, &songs[i], 0); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Max Kellermann
					Max Kellermann