]> git.mxchange.org Git - simgear.git/blob - simgear/compiler.h
733e54cf704b4fd09f18c6b3ff2434bfd5add4db
[simgear.git] / simgear / compiler.h
1 /**************************************************************************
2  * compiler.h -- C++ Compiler Portability Macros
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  *
18  * $Id$
19  *
20  **************************************************************************/
21
22 /** \file compiler.h
23  * A set of defines to encapsulate compiler and platform differences.
24  * Please refer to the source code for full documentation on this file.
25  *
26  * Here is a summary of what this file does.
27  *
28  *  (1)  Defines macros for some STL includes which may be affected
29  *       by file name length limitations.
30  *
31  *  (2)  Defines macros for some features not supported by all C++ compilers.
32  *
33  *  (3)  Defines 'explicit' as a null macro if the compiler doesn't support
34  *       the explicit keyword.
35  *
36  *  (4)  Defines 'typename' as a null macro if the compiler doesn't support
37  *       the typename keyword.
38  *
39  *  (5)  Defines bool, true and false if the compiler doesn't do so.
40  *
41  *  (6)  Defines SG_EXPLICIT_FUNCTION_TMPL_ARGS if the compiler
42  *       supports calling a function template by providing its template
43  *       arguments explicitly.
44  *
45  *  (7)  Defines SG_NEED_AUTO_PTR if STL doesn't provide auto_ptr<>.
46  *
47  *  (8)  Defines SG_NO_ARROW_OPERATOR if the compiler is unable
48  *       to support operator->() for iterators.
49  *
50  *  (9)  Defines SG_USE_EXCEPTIONS if the compiler supports exceptions.
51  *       Note: no FlightGear code uses exceptions.
52  *
53  *  (10) Define SG_NAMESPACES if the compiler supports namespaces.
54  *
55  *  (11) SG_MATH_FN_IN_NAMESPACE_STD -- not used??
56  *
57  *  (12) Define SG_HAVE_STD if std namespace is supported.
58  *
59  *  (13) Defines SG_CLASS_PARTIAL_SPECIALIZATION if the compiler 
60  *       supports partial specialization of class templates.
61  *
62  *  (14) Defines SG_HAVE_STD_INCLUDES to use ISO C++ Standard headers.
63  *
64  *  (15) Defines SG_HAVE_STREAMBUF if <streambuf> of <streambuf.h> are present.
65  *
66  *  (16) Define SG_MATH_EXCEPTION_CLASH if math.h defines an exception class
67  *       that clashes with the one defined in <stdexcept>.
68  */
69
70 #ifndef _SG_COMPILER_H
71 #define _SG_COMPILER_H
72
73 /*
74  * Helper macro SG_STRINGIZE:
75  * Converts the parameter X to a string after macro replacement
76  * on X has been performed.
77  */
78 #define SG_STRINGIZE(X) SG_DO_STRINGIZE(X)
79 #define SG_DO_STRINGIZE(X) #X
80
81 #ifdef __GNUC__
82 #  if __GNUC__ == 2 
83 #    if __GNUC_MINOR__ < 8
84
85        // g++-2.7.x
86 #      define STL_ALGORITHM  <algorithm>
87 #      define STL_FUNCTIONAL <functional>
88 #      define STL_IOMANIP    <iomanip.h>
89 #      define STL_IOSTREAM   <iostream.h>
90 #      define STL_ITERATOR   <iterator.h>
91 #      define STL_FSTREAM    <fstream.h>
92 #      define STL_STDEXCEPT  <stdexcept>
93 #      define STL_STRING     <string>
94 #      define STL_STRSTREAM  <strstream.h>
95
96 #      define SG_NEED_AUTO_PTR
97 #      define SG_NO_DEFAULT_TEMPLATE_ARGS
98 #      define SG_INCOMPLETE_FUNCTIONAL
99 #      define SG_NO_ARROW_OPERATOR
100
101 #    elif __GNUC_MINOR__ >= 8
102
103        // g++-2.8.x and egcs-1.x
104 #      define SG_EXPLICIT_FUNCTION_TMPL_ARGS
105 #      define SG_NEED_AUTO_PTR
106 #      define SG_MEMBER_TEMPLATES
107 #      define SG_NAMESPACES
108 #      define SG_HAVE_STD
109 #      define SG_HAVE_STREAMBUF
110 #      define SG_CLASS_PARTIAL_SPECIALIZATION
111
112 #      define STL_ALGORITHM  <algorithm>
113 #      define STL_FUNCTIONAL <functional>
114 #      define STL_IOMANIP    <iomanip>
115 #      define STL_IOSTREAM   <iostream>
116 #      define STL_ITERATOR   <iterator>
117 #      define STL_FSTREAM    <fstream>
118 #      define STL_STDEXCEPT  <stdexcept>
119 #      define STL_STRING     <string>
120 #      define STL_STRSTREAM  <strstream>
121
122 #    endif
123 #  elif __GNUC__ >= 3
124        // g++-3.0.x
125 #      define SG_EXPLICIT_FUNCTION_TMPL_ARGS
126 #      define SG_NEED_AUTO_PTR
127 #      define SG_MEMBER_TEMPLATES
128 #      define SG_NAMESPACES
129 #      define SG_HAVE_STD
130 #      define SG_HAVE_STREAMBUF
131 #      define SG_CLASS_PARTIAL_SPECIALIZATION
132 #      define SG_HAVE_STD_INCLUDES
133
134 #      define STL_ALGORITHM  <algorithm>
135 #      define STL_FUNCTIONAL <functional>
136 #      define STL_IOMANIP    <iomanip>
137 #      define STL_IOSTREAM   <iostream>
138 #      define STL_ITERATOR   <iterator>
139 #      define STL_FSTREAM    <fstream>
140 #      define STL_STDEXCEPT  <stdexcept>
141 #      define STL_STRING     <string>
142 #      define STL_STRSTREAM  <strstream>
143 #  else
144 #    error Time to upgrade. GNU compilers < 2.7 not supported
145 #  endif
146
147 #  define SG_COMPILER_STR "GNU C++ version " SG_STRINGIZE(__GNUC__) "." SG_STRINGIZE(__GNUC_MINOR__)
148
149 #endif // __GNUC__
150
151 /* KAI C++ */
152 #if defined(__KCC)
153
154 #  define SG_NAMESPACES
155 #  define SG_HAVE_STD
156 #  define SG_HAVE_STREAMBUF
157 #  define SG_HAVE_TRAITS
158 #  define SG_HAVE_STD_INCLUDES
159
160 #  define STL_ALGORITHM  <algorithm>
161 #  define STL_FUNCTIONAL <functional>
162 #  define STL_IOMANIP    <iomanip>
163 #  define STL_IOSTREAM   <iostream>
164 #  define STL_ITERATOR   <iterator>
165 #  define STL_FSTREAM    <fstream>
166 #  define STL_STDEXCEPT  <stdexcept>
167 #  define STL_STRING     <string>
168 #  define STL_STRSTREAM  <strstream>
169
170 #  define SG_COMPILER_STR "Kai C++ version " SG_STRINGIZE(__KCC_VERSION)
171
172 #endif // __KCC
173
174 //
175 // Microsoft compilers.
176 //
177 #ifdef _MSC_VER
178 #  define bcopy(from, to, n) memcpy(to, from, n)
179 #  define FG_MEM_COPY(to,from,n) memcpy(to, from, n)
180
181 #  if _MSC_VER >= 1200  // msvc++ 6.0 or greater
182 #    define SG_NAMESPACES
183 #    define SG_HAVE_STD
184 #    define SG_HAVE_STD_INCLUDES
185 #    define SG_HAVE_STREAMBUF
186
187 #    define STL_ALGORITHM  <algorithm>
188 #    define STL_FUNCTIONAL <functional>
189 #    define STL_IOMANIP    <iomanip>
190 #    define STL_IOSTREAM   <iostream>
191 #    define STL_ITERATOR   <iterator>
192 #    define STL_FSTREAM    <fstream>
193 #    define STL_STDEXCEPT  <stdexcept>
194 #    define STL_STRING     <string>
195 #    define STL_STRSTREAM  <strstream>
196
197 #    define isnan _isnan
198 #    define snprintf _snprintf
199 #    define copysign _copysign
200
201 #    pragma warning(disable: 4786) // identifier was truncated to '255' characters
202 #    pragma warning(disable: 4244) // conversion from double to float
203 #    pragma warning(disable: 4305) //
204
205 #  else
206 #    error What version of MSVC++ is this?
207 #  endif
208
209 #    define SG_COMPILER_STR "Microsoft Visual C++ version " SG_STRINGIZE(_MSC_VER)
210
211 #endif // _MSC_VER
212
213 #ifdef __BORLANDC__
214 # if defined(HAVE_SGI_STL_PORT)
215
216 // Use quotes around long file names to get around Borland's include hackery
217
218 #  define STL_ALGORITHM  "algorithm"
219 #  define STL_FUNCTIONAL "functional"
220
221 #  define SG_MATH_EXCEPTION_CLASH
222
223 # else
224
225 #  define STL_ALGORITHM  <algorithm>
226 #  define STL_FUNCTIONAL <functional>
227 #  define STL_IOMANIP    <iomanip>
228 #  define STL_STDEXCEPT  <stdexcept>
229 #  define STL_STRSTREAM  <strstream>
230
231 #  define SG_INCOMPLETE_FUNCTIONAL
232
233 # endif // HAVE_SGI_STL_PORT
234
235 #  define STL_IOSTREAM   <iostream>
236 #  define STL_ITERATOR   <iterator>
237 #  define STL_FSTREAM    <fstream>
238 #  define STL_STRING     <string>
239 #  define SG_NO_DEFAULT_TEMPLATE_ARGS
240 #  define SG_NAMESPACES
241 // #  define SG_HAVE_STD
242
243 #  define SG_COMPILER_STR "Borland C++ version " SG_STRINGIZE(__BORLANDC__)
244
245 #endif // __BORLANDC__
246
247 //
248 // Native SGI compilers
249 //
250
251 #if defined ( sgi ) && !defined( __GNUC__ )
252 #  define SG_HAVE_NATIVE_SGI_COMPILERS
253
254 #  define SG_EXPLICIT_FUNCTION_TMPL_ARGS
255 #  define SG_CLASS_PARTIAL_SPECIALIZATION
256 #  define SG_NEED_AUTO_PTR
257 #  define SG_MEMBER_TEMPLATES
258 #  define SG_NAMESPACES
259 #  define SG_HAVE_STD
260 #  define SG_HAVE_STREAMBUF
261 #  define SG_HAVE_TRAITS
262 #  define SG_HAVE_STD_INCLUDES
263
264 #  define STL_ALGORITHM  <algorithm>
265 #  define STL_FUNCTIONAL <functional>
266 #  define STL_IOMANIP    <iomanip>
267 #  define STL_IOSTREAM   <iostream>
268 #  define STL_ITERATOR   <iterator>
269 #  define STL_FSTREAM    <fstream>
270 #  define STL_STDEXCEPT  <stdexcept>
271 #if (_COMPILER_VERSION < 740)
272 #  define STL_STRING     <irix_string>
273 #else
274 #  define STL_STRING     <string>
275 #endif
276 #  define STL_STRSTREAM  <strstream>
277
278 #pragma set woff 1001,1012,1014,1116,1155,1172,1174
279 #pragma set woff 1401,1460,1551,1552,1681
280
281 #ifdef __cplusplus
282 #pragma set woff 1682,3303
283 #if (_COMPILER_VERSION >= 740)
284 #  pragma set woff 3624
285 #endif
286 #endif
287
288 #  define SG_COMPILER_STR "SGI MipsPro compiler version " SG_STRINGIZE(_COMPILER_VERSION)
289
290 #endif // Native SGI compilers
291
292
293 #if defined (__sun)
294 #  include <strings.h>
295 #  include <memory.h>
296 #  if defined ( __cplusplus )
297      // typedef unsigned int size_t;
298      extern "C" {
299        extern void *memmove(void *, const void *, size_t);
300      }
301 #  else
302      extern void *memmove(void *, const void *, size_t);
303 #  endif // __cplusplus
304
305 #  if  !defined( __GNUC__ )
306 #   define SG_COMPILER_STR "Sun compiler version " SG_STRINGIZE(__SUNPRO_CC)
307 #  endif
308
309 #endif // sun
310
311 //
312 // Intel C++ Compiler
313 //
314 #if defined(__ICC) || defined (__ECC)
315 #  define SG_NAMESPACES
316 #  define SG_HAVE_STD
317 #  define SG_HAVE_STREAMBUF
318 #  define SG_HAVE_TRAITS
319 #  define SG_HAVE_STD_INCLUDES
320
321 #  define STL_ALGORITHM  <algorithm>
322 #  define STL_FUNCTIONAL <functional>
323 #  define STL_IOMANIP    <iomanip>
324 #  define STL_IOSTREAM   <iostream>
325 #  define STL_ITERATOR   <iterator>
326 #  define STL_FSTREAM    <fstream>
327 #  define STL_STDEXCEPT  <stdexcept>
328 #  define STL_STRING     <string>
329 #  define STL_STRSTREAM  <strstream>
330
331 #  define SG_COMPILER_STR "Intel C++ version " SG_STRINGIZE(__ICC)
332
333 #endif // __ICC
334
335 //
336 // Platform dependent gl.h and glut.h definitions
337 //
338
339 #ifdef __APPLE__
340 #  define SG_GL_H <OpenGL/gl.h>
341 #  define SG_GLX_H <AGL/agl.h>
342 #  define SG_GLU_H <OpenGL/glu.h>
343 #  define SG_GLEXT_H <OpenGL/glext.h>
344 #  define SG_GLUT_H <GLUT/glut.h>
345
346 inline int (isnan)(double r) { return !(r <= 0 || r >= 0); }
347 #else
348 #  define SG_GL_H <GL/gl.h>
349 #  define SG_GLX_H <GL/glx.h>
350 #  define SG_GLU_H <GL/glu.h>
351 #  define SG_GLEXT_H <GL/glext.h>
352 #  define SG_GLUT_H <GL/glut.h>
353 #endif
354
355
356 //
357 // No user modifiable definitions beyond here.
358 //
359
360 #ifdef SG_NEED_EXPLICIT
361 #  define explicit
362 #endif
363
364 #ifdef SG_NEED_TYPENAME
365 #  define typename
366 #endif
367
368 #ifdef SG_NEED_MUTABLE
369 #  define mutable
370 #endif
371
372 #ifdef SG_NEED_BOOL
373    typedef int bool;
374 #  define true 1
375 #  define false 0
376 #endif
377
378 #ifdef SG_EXPLICIT_FUNCTION_TMPL_ARGS
379 #  define SG_NULL_TMPL_ARGS <>
380 #else
381 #  define SG_NULL_TMPL_ARGS
382 #endif
383
384 #ifdef SG_CLASS_PARTIAL_SPECIALIZATION
385 # define SG_TEMPLATE_NULL template<>
386 #else
387 # define SG_TEMPLATE_NULL
388 #endif
389
390 // SG_NO_NAMESPACES is a hook so that users can disable namespaces
391 // without having to edit library headers.
392 #if defined(SG_NAMESPACES) && !defined(SG_NO_NAMESPACES)
393 #   define SG_NAMESPACE(X) namespace X {
394 #   define SG_NAMESPACE_END }
395 #   define SG_USING_NAMESPACE(X) using namespace X
396 # else
397 #   define SG_NAMESPACE(X)
398 #   define SG_NAMESPACE_END
399 #   define SG_USING_NAMESPACE(X)
400 #endif
401
402 /** \def SG_USING_STD(x)
403  *  Expands to using std::x if SG_HAVE_STD is defined 
404  */
405 # ifdef SG_HAVE_STD
406 #  define SG_USING_STD(X) using std::X
407 #  define STD std
408 # else
409 #  define SG_USING_STD(X) 
410 #  define STD
411 # endif
412
413 // Additional <functional> implementation from SGI STL 3.11
414 // Adapter function objects: pointers to member functions
415 #ifdef SG_INCOMPLETE_FUNCTIONAL
416
417 template <class _Ret, class _Tp>
418 class const_mem_fun_ref_t
419 #ifndef __BORLANDC__
420     : public unary_function<_Tp,_Ret>
421 #endif // __BORLANDC__
422 {
423 public:
424   explicit const_mem_fun_ref_t(_Ret (_Tp::*__pf)() const) : _M_f(__pf) {}
425   _Ret operator()(const _Tp& __r) const { return (__r.*_M_f)(); }
426 private:
427   _Ret (_Tp::*_M_f)() const;
428 };
429
430 template <class _Ret, class _Tp>
431 inline const_mem_fun_ref_t<_Ret,_Tp> mem_fun_ref(_Ret (_Tp::*__f)() const)
432   { return const_mem_fun_ref_t<_Ret,_Tp>(__f); }
433
434 #endif // SG_INCOMPLETE_FUNCTIONAL
435
436 #endif // _SG_COMPILER_H
437