00001 /* 00002 * Copyright (C) 2003-2009 Olivier Boudeville 00003 * 00004 * This file is part of the Ceylan library. 00005 * 00006 * The Ceylan library is free software: you can redistribute it and/or modify 00007 * it under the terms of either the GNU Lesser General Public License or 00008 * the GNU General Public License, as they are published by the Free Software 00009 * Foundation, either version 3 of these Licenses, or (at your option) 00010 * any later version. 00011 * 00012 * The Ceylan library is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU Lesser General Public License and the GNU General Public License 00016 * for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License and the GNU General Public License along with the Ceylan library. 00020 * If not, see <http://www.gnu.org/licenses/>. 00021 * 00022 * Author: Olivier Boudeville (olivier.boudeville@esperide.com) 00023 * 00024 */ 00025 00026 00027 #ifndef CEYLAN_TYPES_H_ 00028 #define CEYLAN_TYPES_H_ 00029 00030 00031 #include <list> // for ListSize 00032 00033 00034 // Could be used to define bound: #include <climits> 00035 00036 00052 /* 00053 * inttypes.h is to be preferred to stdint.h, since the former is more 00054 * portable and includes the latter when appropriate. 00055 * 00056 * Even inttypes.h is not included here since it is not present on all 00057 * platforms. 00058 * 00059 * We did not want to use CEYLAN_USES_INTTYPES_H and other defines, as it 00060 * forces us to include beforehand CeylanConfig.h (our "config.h"). 00061 * It was deemed not satisfactory in installed headers, because of risks of 00062 * name clashes. 00063 * However no really portable solution was available when needing to detect 00064 * 64bit types, thus we finally rely on CeylanConfig.h, which defines 00065 * _ceylan_*int*_t symbols appropriately. 00066 * 00067 * cstdint, defined in Boost (http://www.boost.org), is sadly not existing 00068 * yet in the C++ standard. 00069 * 00070 * On the Nintendo DS, we could use also datatypes defined in libnds. 00071 * 00072 */ 00073 00074 00075 /* 00076 * Supposedly available on all targeted platforms (thus removes the need 00077 * to include "CeylanConfig.h". 00078 * However on GNU/Linux with gcc 4.4 we have: 00079 * "This file requires compiler and library support for the upcoming ISO 00080 * C++ standard, C++0x. This support is currently experimental, and must 00081 * be enabled with the -std=c++0x or -std=gnu++0x compiler options." 00082 * #include <cstdint> 00083 * 00084 * #include <stdint.h> does not trigger it. 00085 * 00086 * 00087 */ 00088 00089 00090 00091 #ifdef CEYLAN_USES_CONFIG_H 00092 00093 00094 // If wanting to use our CeylanConfig.h, do so (our tests will not): 00095 #include "CeylanConfig.h" // for configure-time settings 00096 00097 #ifdef CEYLAN_USES_INTTYPES_H 00098 #include <inttypes.h> // for int8_t and others 00099 #endif // CEYLAN_USES_INTTYPES_H 00100 00101 00102 #else // CEYLAN_USES_CONFIG_H 00103 00104 00105 // Otherwise suppose this one is available and use it: 00106 #include <inttypes.h> // for int8_t and others 00107 00108 #define _ceylan_int8_t int8_t 00109 #define _ceylan_uint8_t uint8_t 00110 00111 #define _ceylan_int16_t int16_t 00112 #define _ceylan_uint16_t uint16_t 00113 00114 #define _ceylan_int32_t int32_t 00115 #define _ceylan_uint32_t uint32_t 00116 00117 #define _ceylan_int64_t int64_t 00118 #define _ceylan_uint64_t uint64_t 00119 00120 00121 #endif // CEYLAN_USES_CONFIG_H 00122 00123 00124 00125 namespace Ceylan 00126 { 00127 00128 00129 /* 00130 * Links can be made between the Ceylan basic data types (ex: Sint16) and 00131 * the ones defined by the C language (ex: signed short) and by OpenGL 00132 * (ex: GLshort). 00133 * 00134 * C synonyms to data types are specified, then OpenGL ones (they all 00135 * start with'GL'). 00136 * 00137 * For each Ceylan basic numerical datatype, its lower and higher accepted 00138 * values are specified. For example, if x is a Uint8, then: 00139 * Uint8Min <= x <= Uint8Max (hence Min and Max bounds are included). 00140 * 00141 * @note Depending on the platform and the compiler, size of C types may 00142 * differ. All sizes are therefore checked at compile time (see 00143 * CEYLAN_COMPILE_TIME_ASSERT) and at run-time (see 00144 * testCeylanBasicDatatypes.cc). 00145 * 00146 * @note For example, int (signed or not) is 4 bytes (32 bits) on 32-bit 00147 * processors, and 2 bytes (16 bits) on 16-bit processors. With 4-bit 00148 * processors, it will be probably a mess again. 00149 * 00150 */ 00151 00152 00153 // First, integer types. 00154 00155 00156 00166 typedef _ceylan_int8_t Sint8 ; 00167 00168 extern CEYLAN_DLL Ceylan::Sint8 Sint8Min /* = -128 */ ; 00169 extern CEYLAN_DLL Ceylan::Sint8 Sint8Max /* = 127 */ ; 00170 00171 00172 00182 typedef _ceylan_uint8_t Uint8 ; 00183 00184 extern CEYLAN_DLL Ceylan::Uint8 Uint8Min /* = 0 */ ; 00185 extern CEYLAN_DLL Ceylan::Uint8 Uint8Max /* = 255 */ ; 00186 00187 00188 00215 typedef char Byte ; 00216 00217 extern CEYLAN_DLL Ceylan::Byte ByteMin /* should be -128 */ ; 00218 extern CEYLAN_DLL Ceylan::Byte ByteMax /* should be 127 */ ; 00219 00220 00221 00231 typedef _ceylan_int16_t Sint16 ; 00232 00233 extern CEYLAN_DLL Ceylan::Sint16 Sint16Min /* = -32768 */ ; 00234 extern CEYLAN_DLL Ceylan::Sint16 Sint16Max /* = 32767 */ ; 00235 00236 00237 00247 typedef _ceylan_uint16_t Uint16 ; 00248 00249 extern CEYLAN_DLL Ceylan::Uint16 Uint16Min /* = 0 */ ; 00250 extern CEYLAN_DLL Ceylan::Uint16 Uint16Max /* = 65535 */ ; 00251 00252 00253 00254 00264 typedef _ceylan_int32_t Sint32 ; 00265 00266 00267 /* 00268 * Actually is -2147483648 but is incremented since 00269 * 'this decimal constant is unsigned only in ISO C90'. 00270 * 00271 */ 00272 extern CEYLAN_DLL Ceylan::Sint32 Sint32Min /* = -2147483648 */ ; 00273 extern CEYLAN_DLL Ceylan::Sint32 Sint32Max /* = 2147483647 */ ; 00274 00275 00276 00286 typedef _ceylan_uint32_t Uint32 ; 00287 00288 extern CEYLAN_DLL Ceylan::Uint32 Uint32Min /* = 0 */ ; 00289 extern CEYLAN_DLL Ceylan::Uint32 Uint32Max /* = 4294967295 */ ; 00290 00291 00292 00293 00304 typedef signed long SignedLongInteger ; 00305 00306 extern CEYLAN_DLL Ceylan::SignedLongInteger SignedLongIntegerMin ; 00307 extern CEYLAN_DLL Ceylan::SignedLongInteger SignedLongIntegerMax ; 00308 00309 00310 00322 typedef unsigned long UnsignedLongInteger ; 00323 00324 extern CEYLAN_DLL Ceylan::UnsignedLongInteger UnsignedLongIntegerMin ; 00325 extern CEYLAN_DLL Ceylan::UnsignedLongInteger UnsignedLongIntegerMax ; 00326 00327 00328 00356 #ifdef CEYLAN_HAS_64_BIT_TYPE 00357 00367 typedef _ceylan_uint64_t Uint64 ; 00368 00369 00378 typedef _ceylan_int64_t Sint64 ; 00379 00380 00381 #else // CEYLAN_HAS_64_BIT_TYPE 00382 00383 #define CEYLAN_FAKES_64_BIT_TYPE 00384 00386 typedef struct 00387 { 00388 00389 Ceylan::Uint32 hi ; 00390 Ceylan::Uint32 lo ; 00391 00392 } Uint64, Sint64 ; 00393 00394 #endif // CEYLAN_HAS_64_BIT_TYPE 00395 00396 00397 00398 /* 00399 * From here normally Ceylan::Uint64 and Ceylan::Sint64 can be used in 00400 * all cases. 00401 * 00402 * 00403 */ 00404 00405 00426 typedef float Float32 ; 00427 00428 extern CEYLAN_DLL Ceylan::Float32 Float32Min /* = -3.4E-38 */ ; 00429 extern CEYLAN_DLL Ceylan::Float32 Float32Max /* = 3.4E38 */ ; 00430 00431 00442 typedef double Float64 ; 00443 00444 extern CEYLAN_DLL Ceylan::Float64 Float64Min /* = -1.7E-308 */ ; 00445 extern CEYLAN_DLL Ceylan::Float64 Float64Max /* = 1.7E308 */ ; 00446 00447 00448 00449 /* 00450 * Very large floating point values are not encapsulated with regard to 00451 * their bit widths since they depend too much on the underlying platform. 00452 * 00453 * For example, GNU/Linux IA32 thinks `long double' is 96-bits (while the 00454 * real number of used bits is only 80, both in processor and in memory), 00455 * whereas HP-UX thinks it is 128-bits, other 80, etc. 00456 * 00457 * So the largest fixed-size floating point value used by Ceylan is 00458 * Ceylan::Float64 (64-bits). 00459 * 00460 * One can use nevertheless (Un)SignedLongFloat, provided she does not 00461 * rely on any specific bit width. 00462 * 00463 */ 00464 00465 00476 typedef long double LongFloat ; 00477 00478 extern CEYLAN_DLL Ceylan::LongFloat LongFloatMin ; 00479 extern CEYLAN_DLL Ceylan::LongFloat LongFloatMax ; 00480 00481 00482 00493 //typedef long double Float80 ; 00494 00495 //extern CEYLAN_DLL Ceylan::Float80 Float80Min /* = -3.4E-4932 */ ; 00496 00497 /* 00498 * Actually is 3.4E4932 but is set to a lower value (the highest 00499 * possible one) since 'floating constant exceeds range of double'. 00500 * 00501 */ 00502 //extern CEYLAN_DLL Ceylan::Float80 Float80Max 00503 // /*= 3.4E4932 in theory, 1.7E308 actually */ ; 00504 00505 00506 00511 //typedef long double Float96 ; 00512 00513 00514 //extern CEYLAN_DLL Ceylan::Float96 Float96Min /* = -3.4E-4932 */ ; 00515 00516 /* 00517 * Actually is 3.4E4932 but is set to a lower value (the highest 00518 * possible one) since 'floating constant exceeds range of double'. 00519 * 00520 */ 00521 //extern CEYLAN_DLL Ceylan::Float96 Float96Max 00522 // /*= 3.4E4932 in theory, 1.7E308 actually */ ; 00523 00524 00525 00526 00532 typedef Ceylan::Uint16 Count ; 00533 00534 00535 00545 typedef std::list<Ceylan::Uint16>::size_type ListSize ; 00546 00547 00548 00550 typedef Ceylan::Uint32 Flags ; 00551 00552 00553 /* 00554 * Makes sure the data types really have the right sizes, thanks to a 00555 * trick coming from SDL: 00556 * if the size of tested type does not match desired value, expression 00557 * 'sizeof(t) == n' is false (0) and the macro attempts to define an 00558 * array of '0*2 -1' = -1 element, which results in a compilation error, 00559 * such as 'error: size of array 00560 * `CEYLAN_stop_wrong_size_for_uint64' is negative'. 00561 * 00562 * If the sizes are the expected ones, all arrays have exactly one element 00563 * and the compiler does not complain. 00564 * 00565 */ 00566 #define CEYLAN_COMPILE_TIME_ASSERT(name, x) \ 00567 typedef int CEYLAN_stop_wrong_size_for_ ## name[(x) * 2 - 1] 00568 00569 CEYLAN_COMPILE_TIME_ASSERT( uint8, sizeof(Uint8) == 1 ) ; 00570 CEYLAN_COMPILE_TIME_ASSERT( sint8, sizeof(Sint8) == 1 ) ; 00571 00572 CEYLAN_COMPILE_TIME_ASSERT( uint16, sizeof(Uint16) == 2 ) ; 00573 CEYLAN_COMPILE_TIME_ASSERT( sint16, sizeof(Sint16) == 2 ) ; 00574 00575 CEYLAN_COMPILE_TIME_ASSERT( uint32, sizeof(Uint32) == 4 ) ; 00576 CEYLAN_COMPILE_TIME_ASSERT( sint32, sizeof(Sint32) == 4 ) ; 00577 CEYLAN_COMPILE_TIME_ASSERT( float32, sizeof(Float32) == 4 ) ; 00578 00579 CEYLAN_COMPILE_TIME_ASSERT( uint64, sizeof(Uint64) == 8 ) ; 00580 CEYLAN_COMPILE_TIME_ASSERT( sint64, sizeof(Sint64) == 8 ) ; 00581 CEYLAN_COMPILE_TIME_ASSERT( float64, sizeof(Float64) == 8 ) ; 00582 00583 //CEYLAN_COMPILE_TIME_ASSERT( Float96, sizeof(Float96) == 12 ) ; 00584 00585 00586 } 00587 00588 00589 00590 #endif // CEYLAN_TYPES_H_ 00591