Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_stack.h
Go to the documentation of this file.
1
8#ifndef __N_STACK_HEADER
9#define __N_STACK_HEADER
10
11#ifdef __cplusplus
12extern "C" {
13#endif
14
20#include "nilorea/n_common.h"
21
23#define STACK_ITEM_BOOL 1
25#define STACK_ITEM_CHAR 2
27#define STACK_ITEM_UINT8 3
29#define STACK_ITEM_INT8 4
31#define STACK_ITEM_UINT32 5
33#define STACK_ITEM_INT32 6
35#define STACK_ITEM_UINT64 7
37#define STACK_ITEM_INT64 8
39#define STACK_ITEM_FLOAT 9
41#define STACK_ITEM_DOUBLE 10
43#define STACK_ITEM_PTR 11
44
46#define STACK_IS_FULL 0
48#define STACK_IS_EMPTY 1
50#define STACK_IS_UNDEFINED 2
52#define STACK_ITEM_WRONG_TYPE 3
54#define STACK_ITEM_OK 4
55
59 bool b;
61 char c;
63 uint8_t ui8;
65 int8_t i8;
67 uint32_t ui32;
69 int32_t i32;
70#ifdef ENV_64BITS
72 uint64_t ui64;
74 int64_t i64;
75#endif
77 float f;
79 double d;
81 void* p;
82};
83
85typedef struct STACK_ITEM {
87 bool is_set;
93 uint8_t v_type;
95 uint16_t p_type;
97
99typedef struct STACK {
103 size_t size;
105 size_t head;
107 size_t tail;
109 size_t nb_items;
110} STACK;
111
112#if defined(__sun) && defined(__SVR4)
113/* Solaris: avoid _Generic conflicts due to type compatibility */
114#ifdef ENV_64BITS
115#define stack_push(__STACK, __VAL) \
116 _Generic((__VAL), \
117 bool: stack_push_b, \
118 signed char: stack_push_i8, /* replaces int8_t */ \
119 unsigned char: stack_push_ui8, /* replaces uint8_t */ \
120 uint32_t: stack_push_ui32, \
121 int32_t: stack_push_i32, \
122 uint64_t: stack_push_ui64, \
123 int64_t: stack_push_i64, \
124 float: stack_push_f, \
125 double: stack_push_d, \
126 void*: stack_push_p)(__STACK, __VAL)
127#else
128#define stack_push(__STACK, __VAL) \
129 _Generic((__VAL), \
130 bool: stack_push_b, \
131 signed char: stack_push_i8, \
132 unsigned char: stack_push_ui8, \
133 uint32_t: stack_push_ui32, \
134 int32_t: stack_push_i32, \
135 float: stack_push_f, \
136 double: stack_push_d, \
137 void*: stack_push_p)(__STACK, __VAL)
138#endif
139#else
140/* Default version for Linux, Windows, etc. */
141#ifdef ENV_64BITS
142#define stack_push(__STACK, __VAL) \
143 _Generic((__VAL), \
144 bool: stack_push_b, \
145 char: stack_push_c, \
146 uint8_t: stack_push_ui8, \
147 int8_t: stack_push_i8, \
148 uint32_t: stack_push_ui32, \
149 int32_t: stack_push_i32, \
150 uint64_t: stack_push_ui64, \
151 int64_t: stack_push_i64, \
152 float: stack_push_f, \
153 double: stack_push_d, \
154 void*: stack_push_p)(__STACK, __VAL)
155#else
156#define stack_push(__STACK, __VAL) \
157 _Generic((__VAL), \
158 bool: stack_push_b, \
159 char: stack_push_c, \
160 uint8_t: stack_push_ui8, \
161 int8_t: stack_push_i8, \
162 uint32_t: stack_push_ui32, \
163 int32_t: stack_push_i32, \
164 float: stack_push_f, \
165 double: stack_push_d, \
166 void*: stack_push_p)(__STACK, __VAL)
167#endif
168#endif
169
170STACK* new_stack(size_t nb_items);
171bool delete_stack(STACK** stack);
172bool stack_is_full(STACK* stack);
173bool stack_is_empty(STACK* stack);
174STACK_ITEM* stack_peek(STACK* stack, size_t position);
175
176bool stack_push_b(STACK* stack, bool b);
177bool stack_push_c(STACK* stack, char c);
178bool stack_push_ui8(STACK* stack, uint8_t ui8);
179bool stack_push_i8(STACK* stack, int8_t i8);
180bool stack_push_ui32(STACK* stack, uint32_t ui32);
181bool stack_push_i32(STACK* stack, int32_t i32);
182bool stack_push_f(STACK* stack, float f);
183bool stack_push_d(STACK* stack, double d);
184bool stack_push_p(STACK* stack, void* p, uint16_t p_type);
185
186bool stack_pop_b(STACK* stack, uint8_t* status);
187char stack_pop_c(STACK* stack, uint8_t* status);
188uint8_t stack_pop_ui8(STACK* stack, uint8_t* status);
189int8_t stack_pop_i8(STACK* stack, uint8_t* status);
190uint32_t stack_pop_ui32(STACK* stack, uint8_t* status);
191int32_t stack_pop_i32(STACK* stack, uint8_t* status);
192float stack_pop_f(STACK* stack, uint8_t* status);
193double stack_pop_d(STACK* stack, uint8_t* status);
194void* stack_pop_p(STACK* stack, uint8_t* status);
195
196#ifdef ENV_64BITS
197bool stack_push_ui64(STACK* stack, uint64_t ui64_t);
198bool stack_push_i64(STACK* stack, int64_t i64);
199uint64_t stack_pop_ui64(STACK* stack, uint8_t* status);
200int64_t stack_pop_i64(STACK* stack, uint8_t* status);
201#endif
202
207#ifdef __cplusplus
208}
209#endif
210
211#endif
size_t tail
position of tail
Definition n_stack.h:107
bool is_empty
is item set ?
Definition n_stack.h:89
size_t head
position of head
Definition n_stack.h:105
size_t nb_items
number of item inside stack
Definition n_stack.h:109
STACK_ITEM * stack_array
STACK_ITEM array.
Definition n_stack.h:101
int8_t i8
int 8
Definition n_stack.h:65
uint16_t p_type
if v_type is STACK_ITEM_PTR, user defined pointer type
Definition n_stack.h:95
union STACK_DATA data
union of different types
Definition n_stack.h:91
void * p
pointer
Definition n_stack.h:81
double d
double
Definition n_stack.h:79
size_t size
Size of array.
Definition n_stack.h:103
float f
float
Definition n_stack.h:77
char c
single character
Definition n_stack.h:61
int32_t i32
int 32
Definition n_stack.h:69
uint8_t v_type
type of the item
Definition n_stack.h:93
bool b
boolean
Definition n_stack.h:59
bool is_set
is item empty ?
Definition n_stack.h:87
uint32_t ui32
unsigned int 32
Definition n_stack.h:67
uint8_t ui8
unsigned int 8
Definition n_stack.h:63
bool stack_push_f(STACK *stack, float f)
helper to push a float
Definition n_stack.c:421
bool stack_is_empty(STACK *stack)
test if the stack is empty
Definition n_stack.c:56
double stack_pop_d(STACK *stack, uint8_t *status)
helper to pop a double
Definition n_stack.c:485
bool stack_pop_b(STACK *stack, uint8_t *status)
helper to pop a bool
Definition n_stack.c:177
bool stack_push_d(STACK *stack, double d)
helper to push a double
Definition n_stack.c:465
bool stack_push_ui8(STACK *stack, uint8_t ui8)
helper to push an uint8_t
Definition n_stack.c:245
STACK_ITEM * stack_peek(STACK *stack, size_t position)
peek in the stack with un stacking the stack item
Definition n_stack.c:66
bool stack_is_full(STACK *stack)
test if the stack is full
Definition n_stack.c:47
bool stack_push_ui32(STACK *stack, uint32_t ui32)
helper to push an uint32_t
Definition n_stack.c:333
int8_t stack_pop_i8(STACK *stack, uint8_t *status)
helper to pop a int8_t
Definition n_stack.c:309
char stack_pop_c(STACK *stack, uint8_t *status)
helper to pop a char
Definition n_stack.c:221
float stack_pop_f(STACK *stack, uint8_t *status)
helper to pop a float
Definition n_stack.c:441
uint8_t stack_pop_ui8(STACK *stack, uint8_t *status)
helper to pop a uint8_t
Definition n_stack.c:265
bool stack_push_c(STACK *stack, char c)
helper to push a char
Definition n_stack.c:201
uint32_t stack_pop_ui32(STACK *stack, uint8_t *status)
helper to pop a uint32_t
Definition n_stack.c:353
bool stack_push_b(STACK *stack, bool b)
helper to push a bool
Definition n_stack.c:159
bool stack_push_i32(STACK *stack, int32_t i32)
helper to push an int32_t
Definition n_stack.c:377
int32_t stack_pop_i32(STACK *stack, uint8_t *status)
helper to pop a int32_t
Definition n_stack.c:397
bool delete_stack(STACK **stack)
delete a STACK *stack
Definition n_stack.c:35
bool stack_push_i8(STACK *stack, int8_t i8)
helper to push an int8_t
Definition n_stack.c:289
STACK * new_stack(size_t nb_items)
allocate a new STACK
Definition n_stack.c:16
bool stack_push_p(STACK *stack, void *p, uint16_t p_type)
helper to push a pointer
Definition n_stack.c:510
void * stack_pop_p(STACK *stack, uint8_t *status)
helper to pop a pointer
Definition n_stack.c:531
STACK structure.
Definition n_stack.h:99
structure of a STACK item
Definition n_stack.h:85
structure of a STACK_ITEM data
Definition n_stack.h:57
Common headers and low-level functions & define.