57bb6158658c1fd4e4fc3d37c178ed7c1a270980
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) \
45 _stack_new(sizeof(type)); \
49 * @brief Copy constructor (shallow copy, ok for basic types).
52 Stack
* stack
///< "this" pointer.
56 * @brief Check if the stack is empty.
59 Stack
* stack
///< "this" pointer.
63 * @brief Return size of the current stack.
66 Stack
* stack
///< "this" pointer.
70 * @brief Add something on top of the stack.
73 Stack
* stack
, ///< "this" pointer.
74 void* data
///< Data to be added.
78 * @brief Add something on top of the stack.
79 * @param stack "this" pointer.
80 * @param data Data to be added.
82 * Usage: void stack_push(Stack* stack, void data)
84 #define stack_push(stack, data) \
86 typeof((data)) tmp = data; \
87 _stack_push(stack,&tmp); \
91 * @brief Return what is on top of the stack.
94 Stack
* stack
///< "this" pointer.
98 * @brief Return what is on top of the stack.
99 * @param stack "this" pointer.
100 * @param data Data to be assigned.
102 * Usage: void stack_top(Stack* stack, void data)
104 #define stack_top(stack, data) \
106 void* pData = _stack_top(stack); \
107 data = *((typeof(&data))pData); \
111 * @brief Remove the top of the stack.
114 Stack
* stack
///< "this" pointer.
118 * @brief Clear the entire stack.
121 Stack
* stack
///< "this" pointer.
125 * @brief Destroy the stack: clear it, and free 'stack' pointer.
128 Stack
* stack
///< "this" pointer.