]> www.ginac.de Git - cln.git/blob - src/base/cl_macros.h
* All Files have been modified for inclusion of namespace cln;
[cln.git] / src / base / cl_macros.h
1 // CLN internal macros
2
3 #ifndef _CL_MACROS_H
4 #define _CL_MACROS_H
5
6 // Concatenation of macroexpanded tokens.
7 // Example:
8 //   #undef x
9 //   #define y 16
10 //   CONCAT(x,y)        ==>  'x16' (not 'xy' !)
11   #define CONCAT_(xxx,yyy)  xxx##yyy
12   #define CONCAT3_(aaa,bbb,ccc)  aaa##bbb##ccc
13   #define CONCAT4_(aaa,bbb,ccc,ddd)  aaa##bbb##ccc##ddd
14   #define CONCAT5_(aaa,bbb,ccc,ddd,eee)  aaa##bbb##ccc##ddd##eee
15   #define CONCAT6_(aaa,bbb,ccc,ddd,eee,fff)  aaa##bbb##ccc##ddd##eee##fff
16   #define CONCAT7_(aaa,bbb,ccc,ddd,eee,fff,ggg)  aaa##bbb##ccc##ddd##eee##fff##ggg
17   #define CONCAT(xxx,yyy)  CONCAT_(xxx,yyy)
18   #define CONCAT3(aaa,bbb,ccc)  CONCAT3_(aaa,bbb,ccc)
19   #define CONCAT4(aaa,bbb,ccc,ddd)  CONCAT4_(aaa,bbb,ccc,ddd)
20   #define CONCAT5(aaa,bbb,ccc,ddd,eee)  CONCAT5_(aaa,bbb,ccc,ddd,eee)
21   #define CONCAT6(aaa,bbb,ccc,ddd,eee,fff)  CONCAT6_(aaa,bbb,ccc,ddd,eee,fff)
22   #define CONCAT7(aaa,bbb,ccc,ddd,eee,fff,ggg)  CONCAT7_(aaa,bbb,ccc,ddd,eee,fff,ggg)
23
24 // Convert tokens to strings.
25 // STRING(token)  ==>  "token"
26   #define STRING(token) #token
27   #define STRINGIFY(token) STRING(token)
28
29 // Declare functions that don't return.
30 // nonreturning_function(extern,exit,(void)); == extern void exit (void);
31   #ifdef __GNUC__
32     #if (__GNUC__ >= 3) || ((__GNUC__ == 2) && (__GNUC_MINOR__ >= 90))
33       #define nonreturning_function(storclass,funname,arguments)  \
34         storclass void funname arguments __attribute__((__noreturn__))
35     #else
36       #define nonreturning_function(storclass,funname,arguments)  \
37         typedef void CONCAT3(funname,_function_,__LINE__) arguments; \
38         storclass __volatile__ CONCAT3(funname,_function_,__LINE__) funname
39     #endif
40   #else
41     #define nonreturning_function(storclass,funname,arguments)  \
42       storclass void funname arguments
43   #endif
44
45 // Declaration of variables.
46   #define var
47
48 // `if' with more than one clause:
49 // if (cond1) ... {elif (condi) ...} [else ...]
50   #define elif  else if
51
52 // Endless loop, leave with  break;  or return...;
53   #define loop  while (1)
54
55 // Reversed end condition.
56 // Allows   until (expression) statement
57 // and      do statement until (expression);
58   #define until(expression)  while(!(expression))
59
60 // Boolean values.
61   #define FALSE  0
62   #define TRUE   1
63
64 // Ignore a value (instead of assigning it to a variable).
65 // unused ...
66   #if defined(__GNUC__) || defined(__KCC) // avoid a gcc warning "statement with no effect"
67     #define unused  (void)
68   #else
69     #define unused
70   #endif
71
72 // Denotes a point where control flow can never arrive.
73 // NOTREACHED
74   #define NOTREACHED  cl_notreached_abort(__FILE__,__LINE__);
75 namespace cln {
76   nonreturning_function(extern,cl_notreached_abort, (const char* filename, int lineno));
77 }  // namespace cln
78
79 // Check an arithmetic expression.
80 // ASSERT(expr)
81   #define ASSERT(expr)  { if (!(expr)) { NOTREACHED } }
82
83 // alloca()
84   #if defined(__GNUC__) && !defined(__riscos) && !defined(__convex__)
85     #undef alloca
86     #define alloca  __builtin_alloca
87   #elif defined(_MSC_VER)
88     #include <malloc.h>
89     #define alloca  _alloca
90   #elif defined(HAVE_ALLOCA_H) || defined(__riscos)
91     #include <alloca.h>
92     #ifndef alloca // Sometimes `alloca' is defined as a macro...
93       #if defined(__osf__)
94         extern "C" char* alloca (int size);
95       #else
96         extern "C" void* alloca (int size);
97       #endif
98     #endif
99   #elif defined(_AIX)
100     #pragma alloca // AIX requires this to be the first thing in the file.
101   #elif defined(WATCOM)
102     #include <malloc.h> // defines `alloca' as a macro
103   #elif !defined(NO_ALLOCA)
104     extern "C" void* alloca (int size);
105   #endif
106
107 // NULL pointer.
108   #undef NULL
109   #define NULL  0
110
111 // Bit number n (0<=n<32)
112   #define bit(n)  (1L<<(n))
113 // Bit number n (0<n<=32) mod 2^32
114   #define bitm(n)  (2L<<((n)-1))
115 // Test bit n in x, n constant, x a cl_uint:
116   #if !(defined(__sparc__) || defined(__sparc64__))
117     #define bit_test(x,n)  ((x) & bit(n))
118   #else
119     // On Sparcs long constants are slower than shifts.
120     #if !defined(__GNUC__)
121       #define bit_test(x,n)  \
122         ((n)<12 ? ((x) & bit(n)) : ((sint32)((uint32)(x) << (31-(n))) < 0))
123     #else // gcc optimizes boolean expressions better this way:
124       #define bit_test(x,n)  \
125         (   ( ((n)<12) && ((x) & bit(n)) )                           \
126          || ( ((n)>=12) && ((sint32)((uint32)(x) << (31-(n))) < 0) ) \
127         )
128     #endif
129   #endif
130 // minus bit number n (0<=n<32)
131   #define minus_bit(n)  (-1L<<(n))
132 // minus bit number n (0<n<=32) mod 2^32
133   #define minus_bitm(n)  (-2L<<((n)-1))
134
135 // Return 2^n, n a constant expression.
136 // Same as bit(n), but undefined if n<0 or n>=long_bitsize.
137   #define bitc(n)  (1UL << (((n) >= 0 && (n) < long_bitsize) ? (n) : 0))
138
139 // floor(a,b) for a>=0, b>0 returns floor(a/b).
140 // b should be a constant expression.
141   #define floor(a_from_floor,b_from_floor)  ((a_from_floor) / (b_from_floor))
142 // Save the macro in case we need to include <cmath>.
143   #define cln_floor(a_from_floor,b_from_floor)  ((a_from_floor) / (b_from_floor))
144
145 // ceiling(a,b) for a>=0, b>0 returns ceiling(a/b) = floor((a+b-1)/b).
146 // b should be a constant expression.
147   #define ceiling(a_from_ceiling,b_from_ceiling)  \
148     (((a_from_ceiling) + (b_from_ceiling) - 1) / (b_from_ceiling))
149
150 // round_down(a,b) decreases a>=0 such that it becomes divisible by b>0.
151 // b should be a constant expression.
152   #define round_down(a_from_round,b_from_round)  \
153     (floor(a_from_round,b_from_round)*(b_from_round))
154
155 // round_up(a,b) increases a>=0 such that it becomes divisible by b>0.
156 // b should be a constant expression.
157   #define round_up(a_from_round,b_from_round)  \
158     (ceiling(a_from_round,b_from_round)*(b_from_round))
159
160 // We never call malloc(0), so no need to handle it.
161   #define __MALLOC_0_RETURNS_NULL
162
163 // Loop which executes a statement a given number of times.
164 // dotimesC(countvar,count,statement);
165 // countvar must be of type `uintC'. It is modified!
166   #define dotimesC(countvar_from_dotimesC,count_from_dotimesC,statement_from_dotimesC)  \
167     { countvar_from_dotimesC = (count_from_dotimesC);         \
168       until (countvar_from_dotimesC==0)                       \
169         {statement_from_dotimesC; countvar_from_dotimesC--; } \
170     }
171   #define dotimespC(countvar_from_dotimespC,count_from_dotimespC,statement_from_dotimespC)  \
172     { countvar_from_dotimespC = (count_from_dotimespC);                   \
173       do {statement_from_dotimespC} until (--countvar_from_dotimespC==0); \
174     }
175
176 // doconsttimes(count,statement);
177 // führt statement count mal aus (count mal der Code!),
178 // wobei count eine constant-expression >=0, <=8 ist.
179   #define doconsttimes(count_from_doconsttimes,statement_from_doconsttimes)  \
180     { if (0 < (count_from_doconsttimes)) { statement_from_doconsttimes; } \
181       if (1 < (count_from_doconsttimes)) { statement_from_doconsttimes; } \
182       if (2 < (count_from_doconsttimes)) { statement_from_doconsttimes; } \
183       if (3 < (count_from_doconsttimes)) { statement_from_doconsttimes; } \
184       if (4 < (count_from_doconsttimes)) { statement_from_doconsttimes; } \
185       if (5 < (count_from_doconsttimes)) { statement_from_doconsttimes; } \
186       if (6 < (count_from_doconsttimes)) { statement_from_doconsttimes; } \
187       if (7 < (count_from_doconsttimes)) { statement_from_doconsttimes; } \
188     }
189
190 // DOCONSTTIMES(count,macroname);
191 // ruft count mal den Macro macroname auf (count mal der Code!),
192 // wobei count eine constant-expression >=0, <=8 ist.
193 // Dabei bekommt macroname der Reihe nach die Werte 0,...,count-1 übergeben.
194   #define DOCONSTTIMES(count_from_DOCONSTTIMES,macroname_from_DOCONSTTIMES)  \
195     { if (0 < (count_from_DOCONSTTIMES)) { macroname_from_DOCONSTTIMES((0 < (count_from_DOCONSTTIMES) ? 0 : 0)); } \
196       if (1 < (count_from_DOCONSTTIMES)) { macroname_from_DOCONSTTIMES((1 < (count_from_DOCONSTTIMES) ? 1 : 0)); } \
197       if (2 < (count_from_DOCONSTTIMES)) { macroname_from_DOCONSTTIMES((2 < (count_from_DOCONSTTIMES) ? 2 : 0)); } \
198       if (3 < (count_from_DOCONSTTIMES)) { macroname_from_DOCONSTTIMES((3 < (count_from_DOCONSTTIMES) ? 3 : 0)); } \
199       if (4 < (count_from_DOCONSTTIMES)) { macroname_from_DOCONSTTIMES((4 < (count_from_DOCONSTTIMES) ? 4 : 0)); } \
200       if (5 < (count_from_DOCONSTTIMES)) { macroname_from_DOCONSTTIMES((5 < (count_from_DOCONSTTIMES) ? 5 : 0)); } \
201       if (6 < (count_from_DOCONSTTIMES)) { macroname_from_DOCONSTTIMES((6 < (count_from_DOCONSTTIMES) ? 6 : 0)); } \
202       if (7 < (count_from_DOCONSTTIMES)) { macroname_from_DOCONSTTIMES((7 < (count_from_DOCONSTTIMES) ? 7 : 0)); } \
203     }
204
205 // AT_INITIALIZATION(id) { ... }
206 // executes the given code at initialization time of the file.
207 // The id is something unique.
208   #define AT_INITIALIZATION(id)  \
209     class CONCAT3(INIT_CLASS_,id,__LINE__) {                            \
210       public: CONCAT3(INIT_CLASS_,id,__LINE__) (void);                  \
211     } CONCAT4(INIT_CLASS_,id,__LINE__,_DUMMY);                          \
212     inline CONCAT3(INIT_CLASS_,id,__LINE__)::CONCAT3(INIT_CLASS_,id,__LINE__) (void)
213
214 // AT_DESTRUCTION(id) { ... }
215 // executes the given code at destruction time of the file.
216 // The id is something unique.
217   #define AT_DESTRUCTION(id)  \
218     class CONCAT3(DESTR_CLASS_,id,__LINE__) {                           \
219       public: ~CONCAT3(DESTR_CLASS_,id,__LINE__) (void);                \
220     } CONCAT4(DESTR_CLASS_,id,__LINE__,_DUMMY);                         \
221     CONCAT3(DESTR_CLASS_,id,__LINE__)::~CONCAT3(DESTR_CLASS_,id,__LINE__) (void)
222
223 // Inside a class definition:
224 // Overload `new' so that a class object can be allocated anywhere.
225 #if !((defined(__rs6000__) || defined(__alpha__)) && !defined(__GNUC__))
226 #define ALLOCATE_ANYWHERE(classname)  \
227     /* Ability to place an object at a given address. */                \
228 public:                                                                 \
229     void* operator new (size_t size) { return malloc_hook(size); }      \
230     void* operator new (size_t size, classname* ptr) { unused size; return ptr; } \
231     void operator delete (void* ptr) { free_hook(ptr); }
232 #else
233 // For some compilers, work around template problem with "classname".
234 #define ALLOCATE_ANYWHERE(classname)  \
235     /* Ability to place an object at a given address. */                \
236 public:                                                                 \
237     void* operator new (size_t size) { return malloc_hook(size); }      \
238     void* operator new (size_t size, void* ptr) { unused size; return ptr; } \
239     void operator delete (void* ptr) { free_hook(ptr); }
240 #endif
241
242 // init1(type, object) (value);
243 // initializes `object' with `value', by calling `type''s constructor.
244 // (The identifiers `init' and `Init' are already in use by <streambuf.h>,
245 // it's a shame!)
246 #define init1(type,lvalue)  (void) new (&(lvalue)) type
247
248 // MAYBE_INLINE normally expands to nothing.
249 // Useful for including the implementation of some file inline into another.
250   #define MAYBE_INLINE
251   #define MAYBE_INLINE2
252
253 #endif /* _CL_MACROS_H */