Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_stack.h
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 */
19
27#ifndef __N_STACK_HEADER
28#define __N_STACK_HEADER
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
39#include "nilorea/n_common.h"
40
42#define STACK_ITEM_BOOL 1
44#define STACK_ITEM_CHAR 2
46#define STACK_ITEM_UINT8 3
48#define STACK_ITEM_INT8 4
50#define STACK_ITEM_UINT32 5
52#define STACK_ITEM_INT32 6
54#define STACK_ITEM_UINT64 7
56#define STACK_ITEM_INT64 8
58#define STACK_ITEM_FLOAT 9
60#define STACK_ITEM_DOUBLE 10
62#define STACK_ITEM_PTR 11
63
65#define STACK_IS_FULL 0
67#define STACK_IS_EMPTY 1
69#define STACK_IS_UNDEFINED 2
71#define STACK_ITEM_WRONG_TYPE 3
73#define STACK_ITEM_OK 4
74
78 bool b;
80 char c;
82 uint8_t ui8;
84 int8_t i8;
86 uint32_t ui32;
88 int32_t i32;
89#ifdef ENV_64BITS
91 uint64_t ui64;
93 int64_t i64;
94#endif
96 float f;
98 double d;
100 void* p;
101};
102
104typedef struct STACK_ITEM {
106 bool is_set;
112 uint8_t v_type;
114 uint16_t p_type;
115} STACK_ITEM;
116
118typedef struct STACK {
122 size_t size;
124 size_t head;
126 size_t tail;
128 size_t nb_items;
129} STACK;
130
131/* stack_push_p_default is declared below with the other push functions.
132 * It wraps stack_push_p with a default p_type of 0 so the _Generic macro
133 * can dispatch void* with only 2 arguments.
134 * Use stack_push_p() directly when a custom p_type is needed. */
135
136#if defined(__sun) && defined(__SVR4)
137/* Solaris: avoid _Generic conflicts due to type compatibility */
138#ifdef ENV_64BITS
139#define stack_push(__STACK, __VAL) \
140 _Generic((__VAL), \
141 bool: stack_push_b, \
142 char: stack_push_c, \
143 signed char: stack_push_i8, /* replaces int8_t */ \
144 unsigned char: stack_push_ui8, /* replaces uint8_t */ \
145 uint32_t: stack_push_ui32, \
146 int32_t: stack_push_i32, \
147 uint64_t: stack_push_ui64, \
148 int64_t: stack_push_i64, \
149 float: stack_push_f, \
150 double: stack_push_d, \
151 void*: stack_push_p_default)(__STACK, __VAL)
152#else
153#define stack_push(__STACK, __VAL) \
154 _Generic((__VAL), \
155 bool: stack_push_b, \
156 char: stack_push_c, \
157 signed char: stack_push_i8, \
158 unsigned char: stack_push_ui8, \
159 uint32_t: stack_push_ui32, \
160 int32_t: stack_push_i32, \
161 float: stack_push_f, \
162 double: stack_push_d, \
163 void*: stack_push_p_default)(__STACK, __VAL)
164#endif
165#else
166/* Default version for Linux, Windows, etc. */
167#ifdef ENV_64BITS
168#define stack_push(__STACK, __VAL) \
169 _Generic((__VAL), \
170 bool: stack_push_b, \
171 char: stack_push_c, \
172 uint8_t: stack_push_ui8, \
173 int8_t: stack_push_i8, \
174 uint32_t: stack_push_ui32, \
175 int32_t: stack_push_i32, \
176 uint64_t: stack_push_ui64, \
177 int64_t: stack_push_i64, \
178 float: stack_push_f, \
179 double: stack_push_d, \
180 void*: stack_push_p_default)(__STACK, __VAL)
181#else
182#define stack_push(__STACK, __VAL) \
183 _Generic((__VAL), \
184 bool: stack_push_b, \
185 char: stack_push_c, \
186 uint8_t: stack_push_ui8, \
187 int8_t: stack_push_i8, \
188 uint32_t: stack_push_ui32, \
189 int32_t: stack_push_i32, \
190 float: stack_push_f, \
191 double: stack_push_d, \
192 void*: stack_push_p_default)(__STACK, __VAL)
193#endif
194#endif
195
197STACK* new_stack(size_t nb_items);
199bool delete_stack(STACK** stack);
201bool stack_is_full(const STACK* stack);
203bool stack_is_empty(const STACK* stack);
205STACK_ITEM* stack_peek(STACK* stack, size_t position);
206
208bool stack_push_b(STACK* stack, bool b);
210bool stack_push_c(STACK* stack, char c);
212bool stack_push_ui8(STACK* stack, uint8_t ui8);
214bool stack_push_i8(STACK* stack, int8_t i8);
216bool stack_push_ui32(STACK* stack, uint32_t ui32);
218bool stack_push_i32(STACK* stack, int32_t i32);
220bool stack_push_f(STACK* stack, float f);
222bool stack_push_d(STACK* stack, double d);
224bool stack_push_p(STACK* stack, void* p, uint16_t p_type);
226bool stack_push_p_default(STACK* stack, void* p);
227
229bool stack_pop_b(STACK* stack, uint8_t* status);
231char stack_pop_c(STACK* stack, uint8_t* status);
233uint8_t stack_pop_ui8(STACK* stack, uint8_t* status);
235int8_t stack_pop_i8(STACK* stack, uint8_t* status);
237uint32_t stack_pop_ui32(STACK* stack, uint8_t* status);
239int32_t stack_pop_i32(STACK* stack, uint8_t* status);
241float stack_pop_f(STACK* stack, uint8_t* status);
243double stack_pop_d(STACK* stack, uint8_t* status);
245void* stack_pop_p(STACK* stack, uint8_t* status);
246
247#ifdef ENV_64BITS
249bool stack_push_ui64(STACK* stack, uint64_t ui64_t);
251bool stack_push_i64(STACK* stack, int64_t i64);
253uint64_t stack_pop_ui64(STACK* stack, uint8_t* status);
255int64_t stack_pop_i64(STACK* stack, uint8_t* status);
256#endif
257
262#ifdef __cplusplus
263}
264#endif
265
266#endif
size_t tail
position of tail
Definition n_stack.h:126
bool is_empty
is item empty ?
Definition n_stack.h:108
size_t head
position of head
Definition n_stack.h:124
size_t nb_items
number of item inside stack
Definition n_stack.h:128
STACK_ITEM * stack_array
STACK_ITEM array.
Definition n_stack.h:120
int8_t i8
int 8
Definition n_stack.h:84
uint16_t p_type
if v_type is STACK_ITEM_PTR, user defined pointer type
Definition n_stack.h:114
union STACK_DATA data
union of different types
Definition n_stack.h:110
void * p
pointer
Definition n_stack.h:100
double d
double
Definition n_stack.h:98
size_t size
Size of array.
Definition n_stack.h:122
float f
float
Definition n_stack.h:96
char c
single character
Definition n_stack.h:80
int32_t i32
int 32
Definition n_stack.h:88
uint8_t v_type
type of the item
Definition n_stack.h:112
bool b
boolean
Definition n_stack.h:78
bool is_set
is item set ?
Definition n_stack.h:106
uint32_t ui32
unsigned int 32
Definition n_stack.h:86
uint8_t ui8
unsigned int 8
Definition n_stack.h:82
bool stack_push_f(STACK *stack, float f)
push a float onto the stack
Definition n_stack.c:478
double stack_pop_d(STACK *stack, uint8_t *status)
pop a double from the stack
Definition n_stack.c:547
bool stack_pop_b(STACK *stack, uint8_t *status)
pop a bool from the stack
Definition n_stack.c:204
bool stack_push_d(STACK *stack, double d)
push a double onto the stack
Definition n_stack.c:527
bool stack_push_ui8(STACK *stack, uint8_t ui8)
push a uint8_t onto the stack
Definition n_stack.c:282
STACK_ITEM * stack_peek(STACK *stack, size_t position)
peek at an item at given position without removing it
Definition n_stack.c:93
bool stack_push_ui32(STACK *stack, uint32_t ui32)
push a uint32_t onto the stack
Definition n_stack.c:380
int8_t stack_pop_i8(STACK *stack, uint8_t *status)
pop an int8_t from the stack
Definition n_stack.c:351
char stack_pop_c(STACK *stack, uint8_t *status)
pop a char from the stack
Definition n_stack.c:253
bool stack_is_empty(const STACK *stack)
check if the stack is empty
Definition n_stack.c:82
float stack_pop_f(STACK *stack, uint8_t *status)
pop a float from the stack
Definition n_stack.c:498
uint8_t stack_pop_ui8(STACK *stack, uint8_t *status)
pop a uint8_t from the stack
Definition n_stack.c:302
bool stack_push_c(STACK *stack, char c)
push a char onto the stack
Definition n_stack.c:233
bool stack_is_full(const STACK *stack)
check if the stack is full
Definition n_stack.c:72
uint32_t stack_pop_ui32(STACK *stack, uint8_t *status)
pop a uint32_t from the stack
Definition n_stack.c:400
bool stack_push_b(STACK *stack, bool b)
push a bool onto the stack
Definition n_stack.c:186
bool stack_push_i32(STACK *stack, int32_t i32)
push an int32_t onto the stack
Definition n_stack.c:429
int32_t stack_pop_i32(STACK *stack, uint8_t *status)
pop an int32_t from the stack
Definition n_stack.c:449
bool delete_stack(STACK **stack)
delete a stack and free its memory
Definition n_stack.c:59
bool stack_push_i8(STACK *stack, int8_t i8)
push an int8_t onto the stack
Definition n_stack.c:331
STACK * new_stack(size_t nb_items)
allocate a new stack
Definition n_stack.c:35
bool stack_push_p(STACK *stack, void *p, uint16_t p_type)
push a pointer onto the stack with a custom type
Definition n_stack.c:577
void * stack_pop_p(STACK *stack, uint8_t *status)
pop a pointer from the stack
Definition n_stack.c:598
bool stack_push_p_default(STACK *stack, void *p)
push a pointer onto the stack with default type
Definition n_stack.c:627
STACK structure.
Definition n_stack.h:118
structure of a STACK item
Definition n_stack.h:104
structure of a STACK_ITEM data
Definition n_stack.h:76
Common headers and low-level functions & define.