Lolly 1.4.28
Loading...
Searching...
No Matches
basic.hpp
Go to the documentation of this file.
1
2/** \file basic.hpp
3 * \copyright GPLv3
4 * \details defines basic types and macros used throughout the kernel.
5 * \author Joris van der Hoeven
6 * \date 1999
7 */
8
9#ifndef BASIC_H
10#define BASIC_H
11
12#include "classdef.hpp"
13#include "fast_alloc.hpp"
14#include "minmax.hpp"
15#include "tm_ostream.hpp"
16#include <math.h>
17
18/**
19 * @brief Macro used for template specialization of less than and greater than
20 * operators.
21 */
22#define LESSGTR <>
23
24typedef void* pointer;
25typedef unsigned int color;
26
27#define MAX_SI 0x7fffffff
28
29/**
30 * @brief Macro defining the minimum value of a signed integer.
31 */
32#define MIN_SI 0x80000000
33
34/******************************************************************************
35 * debugging
36 ******************************************************************************/
37
38#if (defined __SUNPRO_CC || defined __clang__ || defined(_WIN32) || \
39 defined(_WIN64))
40#define STACK_NEW_ARRAY(name, T, size) T* name= tm_new_array<T> (size)
41#define STACK_DELETE_ARRAY(name) tm_delete_array (name)
42#else
43#define STACK_NEW_ARRAY(name, T, size) T name[size]
44#define STACK_DELETE_ARRAY(name)
45#endif
46
47/**
48 * @brief Macro used to enable or disable the use of exceptions.
49 */
50#define USE_EXCEPTIONS
51
52/**
53 * @brief Function used to handle a TM failure by displaying an error message
54 * and exiting the program.
55 *
56 * @param msg The error message to display.
57 */
58void tm_failure (const char* msg);
59#ifdef USE_EXCEPTIONS
60
61/**
62 * @brief Global variable used to store the current exception message.
63 */
64extern string the_exception;
65
66/**
67 * @brief Function used to throw an exception with a specified error message.
68 *
69 * @param msg The error message to throw with the exception.
70 */
71void tm_throw (const char* msg);
72
73/**
74 * @brief Function used to handle exceptions by displaying and clearing the
75 * exception message.
76 * @note if catching an exception throwed by \ref tm_throw, this function must
77 * be appended after catch(){...} block.
78 */
79void handle_exceptions ();
80
81/**
82 * @brief Macro used to assert that a condition is true, and throw an exception
83 * with an error message if the condition is false.
84 */
85#define ASSERT(cond, msg) \
86 { \
87 if (!(cond)) tm_throw (msg); \
88 }
89
90/**
91 * @brief Macro used to throw an exception with a specified error message.
92 */
93#define TM_FAILED(msg) \
94 { tm_throw (msg); }
95#else
96#ifdef DEBUG_ASSERT
97#include <assert.h>
98
99/**
100 * @brief Macro used to assert that a condition is true, and print an error
101 * message and exit the program if the condition is false.
102 */
103#define ASSERT(cond, msg) \
104 { \
105 if (!(cond)) { \
106 tm_failure (msg); \
107 assert (cond); \
108 } \
109 }
110
111/**
112 * @brief Macro used to print an error message and exit the program.
113 */
114#define TM_FAILED(msg) \
115 { \
116 tm_failure (msg); \
117 assert (false); \
118 }
119#else
120
121/**
122 * @brief Macro used to assert that a condition is true, and print an error
123 * message and exit the program if the condition is false.
124 */
125#define ASSERT(cond, msg) \
126 { \
127 if (!(cond)) { \
128 tm_failure (msg); \
129 } \
130 }
131
132/**
133 * @brief Macro used to print an error message and exit the program.
134 */
135#define TM_FAILED(msg) \
136 { tm_failure (msg); }
137#endif
138#endif
139
140/******************************************************************************
141 * miscellaneous routines
142 ******************************************************************************/
143
144/**
145 * @brief Hashes an integer.
146 *
147 * @param i The integer to hash.
148 * @return The hashed value of the integer.
149 */
150inline int
151hash (int i) {
152 return i;
153}
154
155/**
156 * @brief Hashes a long integer.
157 *
158 * @param i The long integer to hash.
159 * @return The hashed value of the long integer.
160 */
161inline int
162hash (long int i) {
163 return (int) i;
164}
165
166/**
167 * @brief Hashes a double integer.
168 *
169 * @param i The double integer to hash.
170 * @return The hashed value of the double integer.
171 */
172inline int
173hash (DI i) {
174 return (int) i;
175}
176
177/**
178 * @brief Hashes an unsigned integer.
179 *
180 * @param i The unsigned integer to hash.
181 * @return The hashed value of the unsigned integer.
182 */
183inline int
184hash (unsigned int i) {
185 return i;
186}
187
188/**
189 * @brief Hashes an unsigned long integer.
190 *
191 * @param i The unsigned long integer to hash.
192 * @return The hashed value of the unsigned long integer.
193 */
194inline int
195hash (unsigned long int i) {
196 return (int) i;
197}
198
199/**
200 * @brief Hashes a double number.
201 *
202 * @param x The double number to hash.
203 * @return The hashed value of the double number.
204 */
205inline int
206hash (DN i) {
207 return (int) i;
208}
209
210/**
211 * @brief Hashes a float number.
212 *
213 * @param x The float numberto hash.
214 * @return The hashed value of the float number.
215 */
216inline int
217hash (float x) {
218 union {
219 int n;
220 float d;
221 } u;
222 u.d= x;
223 return u.n & 0xffffffff;
224}
225
226/**
227 * @brief Hashes a double number.
228 *
229 * @param x The double number to hash.
230 * @return The hashed value of the double number.
231 */
232inline int
233hash (double x) {
234 union {
235 DI n;
236 double d;
237 } u;
238 u.d= x;
239 return (int) (u.n ^ (u.n >> 32));
240}
241
242/**
243 * @brief Returns a copy of an integer.
244 *
245 * @param x The integer to copy.
246 * @return A copy of the integer.
247 */
248inline int
249copy (int x) {
250 return x;
251}
252
253/**
254 * @brief Converts a double to a signed integer, rounding to the nearest
255 * integer.
256 *
257 * @param x The double to convert.
258 * @return The converted signed integer.
259 */
260inline SI
261as_int (double x) {
262 return (SI) floor (x + 0.5);
263}
264
265/**
266 * @brief Rounds a double to the nearest integer.
267 *
268 * @param x The double to round.
269 * @return The rounded double.
270 */
271inline double
272tm_round (double x) {
273 return floor (x + 0.5);
274}
275
276/**
277 * @brief Computes a hash value for a pointer.
278 *
279 * @param ptr The pointer to hash.
280 * @return The hash value.
281 */
282int hash (pointer ptr);
283
284/**
285 * @brief Enumeration of display control options.
286 */
288
289/**
290 * @brief Output operator for display control options.
291 *
292 * @param out The output stream to write to.
293 * @param ctrl The display control option.
294 * @return The output stream.
295 */
297
298/**
299 * @brief Helper struct for type identification and initialization.
300 *
301 * @tparam T The type to help identify and initialize.
302 */
303template <typename T> struct type_helper {
304 static int id; ///< The type identifier.
305 static T init; ///< The initialized value of the type.
306 static inline T init_val () {
307 return T ();
308 } ///< Returns the initialized value of the type.
309};
310
311/**
312 * @brief Generates a new type identifier.
313 *
314 * @return The new type identifier.
315 */
317
318/**
319 * @brief Initializes the type identifier and initialized value for a
320 * giventemplate type T.
321 *
322 * @tparam T The type to initialize.
323 */
324template <typename T> int type_helper<T>::id= new_type_identifier ();
325///< The type identifier for type T.
326template <typename T> T type_helper<T>::init= T ();
327///< The initialized value for type T.
328
329#endif // defined BASIC_H
int copy(int x)
Returns a copy of an integer.
Definition basic.hpp:249
int new_type_identifier()
Generates a new type identifier.
Definition basic.cpp:53
void * pointer
Definition basic.hpp:24
void handle_exceptions()
Function used to handle exceptions by displaying and clearing the exception message.
Definition basic.cpp:38
int hash(int i)
Hashes an integer.
Definition basic.hpp:151
double tm_round(double x)
Rounds a double to the nearest integer.
Definition basic.hpp:272
tm_ostream & operator<<(tm_ostream &out, display_control ctrl)
Output operator for display control options.
Definition basic.cpp:62
SI as_int(double x)
Converts a double to a signed integer, rounding to the nearest integer.
Definition basic.hpp:261
void tm_failure(const char *msg)
Function used to handle a TM failure by displaying an error message and exiting the program.
void tm_throw(const char *msg)
Function used to throw an exception with a specified error message.
Definition basic.cpp:27
string the_exception
Global variable used to store the current exception message.
Definition basic.cpp:22
unsigned int color
Definition basic.hpp:25
display_control
Enumeration of display control options.
Definition basic.hpp:287
@ UNINDENT
Definition basic.hpp:287
@ LF
Definition basic.hpp:287
@ HRULE
Definition basic.hpp:287
@ INDENT
Definition basic.hpp:287
The list class represents a linked list.
Definition list.hpp:48
long long int DI
Definition minmax.hpp:16
unsigned long long int DN
Definition minmax.hpp:17
int SI
Definition minmax.hpp:10
Helper struct for type identification and initialization.
Definition basic.hpp:303
static T init_val()
Returns the initialized value of the type.
Definition basic.hpp:306
static T init
The initialized value of the type.
Definition basic.hpp:305
static int id
The type identifier.
Definition basic.hpp:304