A
Andrea Crotti
In our project my groupmate wanted to have a generic queue (which we
don't really need since it's used once) and so he ended up with a
enormous ugly macro.
He said it's the only way to accomplish having different data types
inside a queue in C, is that true?
Would not work just creating a module which takes the size and uses some
pointers to void to obtain the same result?
Something as below (modified of course) could not work as well?
--8<---------------cut here---------------start------------->8---
#include <stdlib.h>
#include <stdio.h>
#define MAX_LEN 10
size_t queue_size;
typedef struct queue
{
void *data[MAX_LEN];
} queue;
void init_queue(size_t size) {
queue_size = size;
}
void add_to_queue(write_queue *queue, void *data) {
assert(!queue_full(queue));
LOG_DEBUG("write-queue: adding %p to the queue",data.stream);
queue->data[queue->last] = data;
// going forward of one position
queue->last = NEXT(queue->last);
}
int queue_full(write_queue *queue) {
return (NEXT(queue->last) == (queue->first));
}
int queue_empty(write_queue *queue) {
return (queue->first == queue->last);
}
void *fetch_from_queue(write_queue *queue) {
return queue->data[queue->first];
}
void delete_last(write_queue *queue) {
queue->first = NEXT(queue->first);
}
int main(int argc, char *argv[])
{
return 0;
}
--8<---------------cut here---------------end--------------->8---
don't really need since it's used once) and so he ended up with a
enormous ugly macro.
He said it's the only way to accomplish having different data types
inside a queue in C, is that true?
Would not work just creating a module which takes the size and uses some
pointers to void to obtain the same result?
Something as below (modified of course) could not work as well?
--8<---------------cut here---------------start------------->8---
#include <stdlib.h>
#include <stdio.h>
#define MAX_LEN 10
size_t queue_size;
typedef struct queue
{
void *data[MAX_LEN];
} queue;
void init_queue(size_t size) {
queue_size = size;
}
void add_to_queue(write_queue *queue, void *data) {
assert(!queue_full(queue));
LOG_DEBUG("write-queue: adding %p to the queue",data.stream);
queue->data[queue->last] = data;
// going forward of one position
queue->last = NEXT(queue->last);
}
int queue_full(write_queue *queue) {
return (NEXT(queue->last) == (queue->first));
}
int queue_empty(write_queue *queue) {
return (queue->first == queue->last);
}
void *fetch_from_queue(write_queue *queue) {
return queue->data[queue->first];
}
void delete_last(write_queue *queue) {
queue->first = NEXT(queue->first);
}
int main(int argc, char *argv[])
{
return 0;
}
--8<---------------cut here---------------end--------------->8---