b6a3bf474fb5e0ae270e4ef216f6bacef47c779c
10 #include "cgds/types.h"
11 #include "cgds/safe_alloc.h"
12 #include "cgds/Vector.h"
15 * @brief Stack containing generic data.
17 typedef struct Stack
{
18 size_t dataSize
; ///< Size in bytes of a stack element.
19 Vector
* array
; ///< Internal data structure: resizeable array.
23 * @brief Initialize an empty stack.
26 Stack
* stack
, ///< "this" pointer.
27 size_t dataSize
///< Size in bytes of a stack element.
31 * @brief Return an allocated and initialized stack.
34 size_t dataSize
///< Size in bytes of a stack element.
38 * @brief Return an allocated and initialized stack.
39 * @param type Type of a stack element (int, char*, ...).
41 * Usage: Stack* stack_new(<Type> type)
43 #define stack_new(type) \
44 _stack_new(sizeof(type))
47 * @brief Copy constructor (shallow copy, ok for basic types).
50 Stack
* stack
///< "this" pointer.
54 * @brief Check if the stack is empty.
57 Stack
* stack
///< "this" pointer.
61 * @brief Return size of the current stack.
64 Stack
* stack
///< "this" pointer.
68 * @brief Add something on top of the stack.
71 Stack
* stack
, ///< "this" pointer.
72 void* data
///< Data to be added.
76 * @brief Add something on top of the stack.
77 * @param stack "this" pointer.
78 * @param data Data to be added.
80 * Usage: void stack_push(Stack* stack, void data)
82 #define stack_push(stack, data) \
84 typeof(data) tmp = data; \
85 _stack_push(stack, &tmp); \
89 * @brief Return what is on top of the stack.
92 Stack
* stack
///< "this" pointer.
96 * @brief Return what is on top of the stack.
97 * @param stack "this" pointer.
98 * @param data Data to be assigned.
100 * Usage: void stack_top(Stack* stack, void data)
102 #define stack_top(stack, data) \
104 void* pData = _stack_top(stack); \
105 data = *((typeof(&data))pData); \
109 * @brief Remove the top of the stack.
112 Stack
* stack
///< "this" pointer.
116 * @brief Clear the entire stack.
119 Stack
* stack
///< "this" pointer.
123 * @brief Destroy the stack: clear it, and free 'stack' pointer.
126 Stack
* stack
///< "this" pointer.