10 #include "cgds/types.h"
11 #include "cgds/safe_alloc.h"
14 * @brief Generic internal queue cell.
16 typedef struct QueueCell
{
17 void* data
; ///< Generic data contained in the cell.
18 struct QueueCell
* next
; ///< Next cell in the internal single-linked list.
22 * @brief Queue containing generic data.
23 * @param dataSize Size in bytes of a queue element.
25 typedef struct Queue
{
26 UInt size
; ///< Count elements in the queue.
27 size_t dataSize
; ///< Size in bytes of a queue element.
28 QueueCell
* front
; ///< Pointer to the next dequeued element.
29 QueueCell
* back
; ///< Pointer to the last enqueued element.
33 * @brief Initialize an empty queue.
34 * @param queue "this" pointer.
35 * @param dataSize Size in bytes of a queue element.
38 Queue
* queue
, ///< "this" pointer.
39 size_t dataSize
///< Size in bytes of a queue element.
43 * @brief Return an allocated and initialized queue.
46 size_t dataSize
///< Size in bytes of a queue element.
50 * @brief Return an allocated and initialized queue.
51 * @param type Type of a queue element (int, char*, ...).
53 * Usage: Queue* queue_new(<Type> type)
55 #define queue_new(type) \
56 _queue_new(sizeof(type))
59 * @brief Copy constructor (works well if items do not have allocated sub-pointers).
62 Queue
* queue
///< "this" pointer.
66 * @brief Check if the queue is empty.
69 Queue
* queue
///< "this" pointer.
73 * @brief Return size of the current queue.
76 Queue
* queue
///< "this" pointer.
80 * @brief Add something at the end of the queue.
83 Queue
* queue
, ///< "this" pointer.
84 void* data
///< Data to be pushed.
88 * @brief Add something at the end of the queue.
89 * @param queue "this" pointer
90 * @param data Data to be pushed.
92 * Usage: void queue_push(Queue* queue, void data)
94 #define queue_push(queue, data) \
96 typeof((data)) tmp = data; \
97 _queue_push(queue, &tmp); \
101 * @brief Return what is at the beginning of the queue.
104 Queue
* queue
///< "this" pointer.
108 * @brief Return what is at the beginning of the queue.
109 * @param queue "this" pointer.
110 * @param data Data to be assigned.
112 * Usage: void queue_peek(Queue* queue, void data)
114 #define queue_peek(queue, data) \
116 void* pData = _queue_peek(queue); \
117 data = *((typeof(&data))pData); \
121 * @brief Remove the beginning of the queue.
124 Queue
* queue
///< "this" pointer.
128 * @brief Clear the entire queue.
131 Queue
* queue
///< "this" pointer.
135 * @brief Destroy the queue: clear it, and free 'queue' pointer.
138 Queue
* queue
///< "this" pointer.