FMI Library: part of JModelica.org
jm_stack.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012 Modelon AB
3 
4  This program is free software: you can redistribute it and/or modify
5  it under the terms of the BSD style license.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  FMILIB_License.txt file for more details.
11 
12  You should have received a copy of the FMILIB_License.txt file
13  along with this program. If not, contact Modelon AB <http://www.modelon.com>.
14 */
15 
16 #ifndef jm_stack_h_
17 #define jm_stack_h_
18 
19 #include "jm_vector.h"
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
41 #define jm_stack(T) jm_mangle(jm_stack, T)
42 
52 #define jm_stack_alloc(T) jm_mangle(jm_stack_alloc, T)
53 
60 #define jm_stack_free(T) jm_mangle(jm_stack_free, T)
61 
71 #define jm_stack_init(T) jm_mangle(jm_stack_init, T)
72 
83 #define jm_stack_free_data(T) jm_mangle(jm_stack_free_data, T)
84 
92 #define jm_stack_get_size(T) jm_mangle(jm_stack_get_size, T)
93 
101 #define jm_stack_reserve(T) jm_mangle(jm_stack_reserve, T)
102 
111 #define jm_stack_push(T) jm_mangle(jm_stack_push, T)
112 
117 #define jm_stack_is_empty(T) jm_mangle(jm_stack_is_empty, T)
118 
123 #define jm_stack_pop(T) jm_mangle(jm_stack_pop, T)
124 
129 #define jm_stack_top(T) jm_mangle(jm_stack_top, T)
130 
136 #define jm_stack_foreach(T) jm_mangle(jm_stack_foreach, T)
137 
138 
140 #define JM_STACK_MINIMAL_CAPACITY JM_VECTOR_MINIMAL_CAPACITY
141 
143 #define JM_STACK_MAX_MEMORY_CHUNK JM_VECTOR_MAX_MEMORY_CHUNK
144 
146 #define jm_stack_declare_template(T) \
147 typedef jm_vector(T) jm_stack(T); \
148  \
149 static jm_stack(T)* jm_stack_alloc(T)(size_t capacity,jm_callbacks* c) { return jm_vector_alloc(T)(0, capacity, c); } \
150  \
151 static void jm_stack_free(T)(jm_stack(T) * a) { jm_vector_free(T)(a); } \
152  \
153 static void jm_stack_init(T)(jm_stack(T)* a, jm_callbacks* c) { jm_vector_init(T)(a,0,c); } \
154 \
155 static void jm_stack_free_data(T)(jm_stack(T)* a) { jm_vector_free_data(T)(a); } \
156 \
157 static size_t jm_stack_get_size(T)(jm_stack(T)* a) { return jm_vector_get_size(T)(a); } \
158 \
159 static size_t jm_stack_reserve(T)(jm_stack(T)* a, size_t capacity) { return jm_vector_reserve(T)(a, capacity); } \
160  \
161 static T* jm_stack_push(T)(jm_stack(T)* a, T item) { return jm_vector_push_back(T)(a, item); }\
162  \
163 static int jm_stack_is_empty(T)(jm_stack(T)* a) { return ((jm_stack_get_size(T)(a) > 0)? 0:1); } \
164  \
165 static T jm_stack_top(T)(jm_stack(T)* a) { \
166  assert(!jm_stack_is_empty(T)(a)); \
167  return jm_vector_get_item(T)(a,jm_vector_get_size(T)(a)-1) ; \
168 } \
169  \
170 static T jm_stack_pop(T)(jm_stack(T)* a) { \
171  T ret; \
172  ret = jm_stack_top(T)(a); \
173  jm_vector_resize(T)(a, jm_vector_get_size(T)(a) - 1); \
174  return ret; \
175 } \
176 \
177 static void jm_stack_foreach(T)(jm_stack(T)* a, void (*f)(T, void*), void * data) { jm_vector_foreach_c(T)(a,f,data); }
178 
187 
188 #ifdef __cplusplus
189 }
190 #endif
191 
192 #endif
void * jm_voidp
A void pointer.
Definition: jm_types.h:35
#define jm_stack_declare_template(T)
Definition: jm_stack.h:146
const char * jm_string
A constant string.
Definition: jm_types.h:33