10 #include "cgds/types.h"
11 #include "cgds/safe_alloc.h"
14 * @brief Generic internal stack cell.
16 typedef struct StackCell
{
17 void* data
; ///< Generic data contained in the cell.
18 struct StackCell
* previous
; ///< Previous cell in the internal single-linked list.
22 * @brief Stack containing generic data.
24 typedef struct Stack
{
25 UInt size
; ///< Count elements in the stack.
26 size_t dataSize
; ///< Size in bytes of a stack element.
27 StackCell
* top
; ///< Last added element, on top of the stack.
31 * @brief Initialize an empty stack.
34 Stack
* stack
, ///< "this" pointer.
35 size_t dataSize
///< Size in bytes of a stack element.
39 * @brief Return an allocated and initialized stack.
42 size_t dataSize
///< Size in bytes of a stack element.
46 * @brief Return an allocated and initialized stack.
47 * @param type Type of a stack element (int, char*, ...).
49 * Usage: Stack* stack_new(<Type> type)
51 #define stack_new(type) \
52 _stack_new(sizeof(type))
55 * @brief Copy constructor (works well if items do not have allocated sub-pointers).
58 Stack
* stack
///< "this" pointer.
62 * @brief Check if the stack is empty.
65 Stack
* stack
///< "this" pointer.
69 * @brief Return size of the current stack.
72 Stack
* stack
///< "this" pointer.
76 * @brief Add something on top of the stack.
79 Stack
* stack
, ///< "this" pointer.
80 void* data
///< Data to be added.
84 * @brief Add something on top of the stack.
85 * @param stack "this" pointer.
86 * @param data Data to be added.
88 * Usage: void stack_push(Stack* stack, void data)
90 #define stack_push(stack, data) \
92 typeof((data)) tmp = data; \
93 _stack_push(stack,&tmp); \
97 * @brief Return what is on top of the stack.
100 Stack
* stack
///< "this" pointer.
104 * @brief Return what is on top of the stack.
105 * @param stack "this" pointer.
106 * @param data Data to be assigned.
108 * Usage: void stack_top(Stack* stack, void data)
110 #define stack_top(stack, data) \
112 void* pData = _stack_top(stack); \
113 data = *((typeof(&data))pData); \
117 * @brief Remove the top of the stack.
120 Stack
* stack
///< "this" pointer.
124 * @brief Clear the entire stack.
127 Stack
* stack
///< "this" pointer.
131 * @brief Destroy the stack: clear it, and free 'stack' pointer.
134 Stack
* stack
///< "this" pointer.