xref: /aoo4110/main/sal/inc/sal/types.h (revision b1cdbd2c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _SAL_TYPES_H_
25 #define _SAL_TYPES_H_
26 
27 #include <sal/config.h>
28 
29 /* Grab __SIZEOFxxx constants from typesconfig tool on Unix */
30 #if defined UNX
31   #include <sal/typesizes.h>
32 #elif defined(WNT) || defined(OS2)
33   /* FIXME: autogeneration of type sizes on Win32/Win64? */
34   #define SAL_TYPES_ALIGNMENT2		1
35   #define SAL_TYPES_ALIGNMENT4		1
36   #define SAL_TYPES_ALIGNMENT8		1
37   #define SAL_TYPES_SIZEOFSHORT		2
38   #define SAL_TYPES_SIZEOFINT 		4
39   #define SAL_TYPES_SIZEOFLONG		4
40   #define SAL_TYPES_SIZEOFLONGLONG		8
41   #define SAL_TYPES_SIZEOFPOINTER		4
42 #endif
43 
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47 
48 /********************************************************************************/
49 /* Data types
50 */
51 
52 /* Boolean */
53 typedef unsigned char sal_Bool;
54 #   define sal_False ((sal_Bool)0)
55 #   define sal_True  ((sal_Bool)1)
56 
57 /* char is assumed to always be 1 byte long */
58 typedef signed char         sal_Int8;
59 typedef unsigned char       sal_uInt8;
60 
61 #if SAL_TYPES_SIZEOFSHORT == 2
62 	typedef signed short      sal_Int16;
63 	typedef unsigned short    sal_uInt16;
64 #else
65      #error "Could not find 16-bit type, add support for your architecture"
66 #endif
67 
68 #if SAL_TYPES_SIZEOFLONG == 4
69 	typedef signed long       sal_Int32;
70 	typedef unsigned long     sal_uInt32;
71     #define SAL_PRIdINT32 "ld"
72     #define SAL_PRIuUINT32 "lu"
73     #define SAL_PRIxUINT32 "lx"
74     #define SAL_PRIXUINT32 "lX"
75 #elif SAL_TYPES_SIZEOFINT == 4
76 	typedef signed int        sal_Int32;
77 	typedef unsigned int      sal_uInt32;
78     #define SAL_PRIdINT32 "d"
79     #define SAL_PRIuUINT32 "u"
80     #define SAL_PRIxUINT32 "x"
81     #define SAL_PRIXUINT32 "X"
82 #else
83      #error "Could not find 32-bit type, add support for your architecture"
84 #endif
85 
86 #if (_MSC_VER >= 1000)
87 	typedef __int64                  sal_Int64;
88 	typedef unsigned __int64         sal_uInt64;
89 
90 	/*  The following are macros that will add the 64 bit constant suffix. */
91 	#define SAL_CONST_INT64(x)       x##i64
92 	#define SAL_CONST_UINT64(x)      x##ui64
93 
94     #define SAL_PRIdINT64 "I64d"
95     #define SAL_PRIuUINT64 "I64u"
96     #define SAL_PRIxUINT64 "I64x"
97     #define SAL_PRIXUINT64 "I64X"
98 #elif defined(__SUNPRO_CC) || defined(__SUNPRO_C) || defined (__GNUC__) || defined(__hpux) || defined (sgi)
99 	#if SAL_TYPES_SIZEOFLONG == 8
100 		typedef signed long int         sal_Int64;
101 		typedef unsigned long int       sal_uInt64;
102 
103 
104 		/*  The following are macros that will add the 64 bit constant suffix. */
105 		#define SAL_CONST_INT64(x)       x##l
106 		#define SAL_CONST_UINT64(x)      x##ul
107 
108         #define SAL_PRIdINT64 "ld"
109         #define SAL_PRIuUINT64 "lu"
110         #define SAL_PRIxUINT64 "lx"
111         #define SAL_PRIXUINT64 "lX"
112 	#elif SAL_TYPES_SIZEOFLONGLONG == 8
113 		typedef signed long long    sal_Int64;
114 		typedef unsigned long long  sal_uInt64;
115 
116 		/*  The following are macros that will add the 64 bit constant suffix. */
117 		#define SAL_CONST_INT64(x)       x##ll
118 		#define SAL_CONST_UINT64(x)      x##ull
119 
120 	#ifdef __MINGW32__
121     #define SAL_PRIdINT64 "I64d"
122     #define SAL_PRIuUINT64 "I64u"
123     #define SAL_PRIxUINT64 "I64x"
124     #define SAL_PRIXUINT64 "I64X"
125 	#else
126         #define SAL_PRIdINT64 "lld"
127         #define SAL_PRIuUINT64 "llu"
128         #define SAL_PRIxUINT64 "llx"
129         #define SAL_PRIXUINT64 "llX"
130 	#endif
131 	#else
132 		#error "Could not find 64-bit type, add support for your architecture"
133 	#endif
134 #else
135 	#error "Please define the 64-bit types for your architecture/compiler in sal/inc/sal/types.h"
136 #endif
137 
138 typedef char                     sal_Char;
139 typedef signed char              sal_sChar;
140 typedef unsigned char            sal_uChar;
141 
142 #if ( defined(SAL_W32) && !defined(__MINGW32__) )
143 	typedef wchar_t             sal_Unicode;
144 #else
145 	#define SAL_UNICODE_NOTEQUAL_WCHAR_T
146 	typedef sal_uInt16          sal_Unicode;
147 #endif
148 
149 typedef void *                   sal_Handle;
150 
151 /* sal_Size should currently be the native width of the platform */
152 #if SAL_TYPES_SIZEOFPOINTER == 4
153 	typedef sal_uInt32          sal_Size;
154 	typedef sal_Int32           sal_sSize;
155 #elif SAL_TYPES_SIZEOFPOINTER == 8
156 	typedef sal_uInt64          sal_Size;
157 	typedef sal_Int64           sal_sSize;
158 #else
159 	#error "Please make sure SAL_TYPES_SIZEOFPOINTER is defined for your architecture/compiler"
160 #endif
161 
162 /* sal_PtrDiff holds the result of a pointer subtraction */
163 #if SAL_TYPES_SIZEOFPOINTER == 4
164 	typedef sal_Int32           sal_PtrDiff;
165 #elif SAL_TYPES_SIZEOFPOINTER == 8
166 	typedef sal_Int64           sal_PtrDiff;
167 #else
168 	#error "Please make sure SAL_TYPES_SIZEOFPOINTER is defined for your architecture/compiler"
169 #endif
170 
171 /* printf-style conversion specification length modifiers for size_t and
172    ptrdiff_t (most platforms support C99, MSC has its own extension) */
173 #if defined(_MSC_VER) || defined(__MINGW32__)
174     #define SAL_PRI_SIZET "I"
175     #define SAL_PRI_PTRDIFFT "I"
176 #else
177     #define SAL_PRI_SIZET "z"
178     #define SAL_PRI_PTRDIFFT "t"
179 #endif
180 
181 /* sal_IntPtr, sal_uIntPtr are integer types designed to hold pointers so that any valid
182  * pointer to void can be converted to this type and back to a pointer to void and the
183  * result will compare to the original pointer */
184 #if SAL_TYPES_SIZEOFPOINTER == 4
185 	typedef sal_Int32           sal_IntPtr;
186 	typedef sal_uInt32          sal_uIntPtr;
187     #define SAL_PRIdINTPTR SAL_PRIdINT32
188     #define SAL_PRIuUINTPTR SAL_PRIuUINT32
189     #define SAL_PRIxUINTPTR SAL_PRIxUINT32
190     #define SAL_PRIXUINTPTR SAL_PRIXUINT32
191 #elif SAL_TYPES_SIZEOFPOINTER == 8
192 	typedef sal_Int64           sal_IntPtr;
193 	typedef sal_uInt64          sal_uIntPtr;
194     #define SAL_PRIdINTPTR SAL_PRIdINT64
195     #define SAL_PRIuUINTPTR SAL_PRIuUINT64
196     #define SAL_PRIxUINTPTR SAL_PRIxUINT64
197     #define SAL_PRIXUINTPTR SAL_PRIXUINT64
198 #else
199 	#error "Please make sure SAL_TYPES_SIZEOFPOINTER is defined for your architecture/compiler"
200 #endif
201 
202 /********************************************************************************/
203 /* Useful defines
204  */
205 
206 /* The following SAL_MIN_INTn defines codify the assumption that the signed
207  * sal_Int types use two's complement representation.  Defining them as
208  * "-0x7F... - 1" instead of as "-0x80..." prevents warnings about applying the
209  * unary minus operator to unsigned quantities.
210  */
211 #define SAL_MIN_INT8          ((sal_Int8)   (-0x7F - 1))
212 #define SAL_MAX_INT8          ((sal_Int8)   0x7F)
213 #define SAL_MAX_UINT8         ((sal_uInt8)  0xFF)
214 #define SAL_MIN_INT16         ((sal_Int16)  (-0x7FFF - 1))
215 #define SAL_MAX_INT16         ((sal_Int16)  0x7FFF)
216 #define SAL_MAX_UINT16        ((sal_uInt16) 0xFFFF)
217 #define SAL_MIN_INT32         ((sal_Int32)  (-0x7FFFFFFF - 1))
218 #define SAL_MAX_INT32         ((sal_Int32)  0x7FFFFFFF)
219 #define SAL_MAX_UINT32        ((sal_uInt32) 0xFFFFFFFF)
220 #define SAL_MIN_INT64         ((sal_Int64)  (SAL_CONST_INT64(-0x7FFFFFFFFFFFFFFF) - 1))
221 #define SAL_MAX_INT64         ((sal_Int64)  SAL_CONST_INT64(0x7FFFFFFFFFFFFFFF))
222 #define SAL_MAX_UINT64        ((sal_uInt64) SAL_CONST_UINT64(0xFFFFFFFFFFFFFFFF))
223 
224 #if SAL_TYPES_SIZEOFLONG == 4
225 #define SAL_MAX_SSIZE		SAL_MAX_INT32
226 #define SAL_MAX_SIZE		SAL_MAX_UINT32
227 #elif SAL_TYPES_SIZEOFLONG == 8
228 #define SAL_MAX_SSIZE		SAL_MAX_INT64
229 #define SAL_MAX_SIZE		SAL_MAX_UINT64
230 #endif
231 
232 #if defined(SAL_W32) || defined(SAL_OS2) || defined(SAL_UNX)
233 #   define SAL_MAX_ENUM 0x7fffffff
234 #elif defined(SAL_W16)
235 #   define SAL_MAX_ENUM 0x7fff
236 #endif
237 
238 #if defined(_MSC_VER) || defined(__MINGW32__)
239 #   define SAL_DLLPUBLIC_EXPORT    __declspec(dllexport)
240 #if defined(_MSC_VER)
241 #   define SAL_DLLPUBLIC_IMPORT    __declspec(dllimport)
242 #else
243 #   define SAL_DLLPUBLIC_IMPORT
244 #endif // defined(_MSC_VER)
245 #   define SAL_DLLPRIVATE
246 #if defined(_MSC_VER)
247 #   define SAL_CALL         __cdecl
248 #   define SAL_CALL_ELLIPSE __cdecl
249 #else
250 #   define SAL_CALL
251 #   define SAL_CALL_ELLIPSE
252 #endif
253 #elif defined SAL_OS2 // YD
254 /* YD 25/09/2007 gcc doesn't like imports inside class members */
255 //#   define SAL_DLLPUBLIC_EXPORT
256 /* YD 01/11/2011 */
257 #   define SAL_DLLPUBLIC_EXPORT    __declspec(dllexport)
258 #   define SAL_DLLPUBLIC_IMPORT
259 #   define SAL_DLLPRIVATE
260 #   define SAL_CALL
261 #   define SAL_CALL_ELLIPSE
262 #elif defined SAL_UNX
263 #   if   defined(__SUNPRO_CC) && (__SUNPRO_CC >= 0x550)
264 #     define SAL_DLLPUBLIC_EXPORT  __global
265 #     define SAL_DLLPUBLIC_IMPORT
266 #     define SAL_DLLPRIVATE        __hidden
267 #   elif defined(__SUNPRO_C ) && (__SUNPRO_C  >= 0x550)
268 #     define SAL_DLLPUBLIC_EXPORT  __global
269 #     define SAL_DLLPUBLIC_IMPORT
270 #     define SAL_DLLPRIVATE        __hidden
271 #   elif defined(HAVE_GCC_VISIBILITY_FEATURE)
272 #     define SAL_DLLPUBLIC_EXPORT  __attribute__ ((visibility("default")))
273 #     define SAL_DLLPUBLIC_IMPORT
274 #     define SAL_DLLPRIVATE        __attribute__ ((visibility("hidden")))
275 #   else
276 #     define SAL_DLLPUBLIC_EXPORT
277 #     define SAL_DLLPUBLIC_IMPORT
278 #     define SAL_DLLPRIVATE
279 #   endif
280 #   define SAL_CALL
281 #   define SAL_CALL_ELLIPSE
282 #else
283 #   error("unknown platform")
284 #endif
285 
286 /**
287    Exporting the symbols necessary for exception handling on GCC.
288 
289    These macros are used for inline declarations of exception classes, as in
290    rtl/malformeduriexception.hxx.
291 */
292 #if defined(__GNUC__) && ! defined(__MINGW32__)
293 #define SAL_EXCEPTION_DLLPUBLIC_EXPORT SAL_DLLPUBLIC_EXPORT
294 #define SAL_EXCEPTION_DLLPRIVATE SAL_DLLPRIVATE
295 #else
296 #define SAL_EXCEPTION_DLLPUBLIC_EXPORT
297 #define SAL_EXCEPTION_DLLPRIVATE
298 #endif
299 
300 /** Use this for pure virtual classes, e.g. class SAL_NO_VTABLE Foo { ...
301     This hinders the compiler from setting a generic vtable stating that
302     a pure virtual function was called and thus slightly reduces code size.
303 */
304 #ifdef _MSC_VER
305 #   define SAL_NO_VTABLE __declspec(novtable)
306 #else
307 #   define SAL_NO_VTABLE
308 #endif
309 
310 #ifdef SAL_W32
311 #   pragma pack(push, 8)
312 #elif defined(SAL_OS2)
313 #	pragma pack(push, 4)
314 #endif
315 
316 /** This is the binary specification of a SAL sequence.
317 	<br>
318 */
319 typedef struct _sal_Sequence
320 {
321 	/** reference count of sequence<br>
322 	*/
323 	sal_Int32			nRefCount;
324 	/** element count<br>
325 	*/
326 	sal_Int32			nElements;
327 	/** elements array<br>
328 	*/
329 	char				elements[1];
330 } sal_Sequence;
331 
332 #define SAL_SEQUENCE_HEADER_SIZE ((sal_Size)&((sal_Sequence *)0)->elements)
333 
334 #if defined( SAL_W32) ||  defined(SAL_OS2)
335 #pragma pack(pop)
336 #endif
337 
338 
339 /** Wrap C++ const_cast, reinterpret_cast and static_cast expressions in
340     macros to keep code portable to old compilers (since most compilers still
341     lack RTTI support, dynamic_cast is not included here).
342  */
343 #ifdef __cplusplus
344 #if defined SAL_W32 || defined SOLARIS || defined LINUX || defined MACOSX || defined FREEBSD || defined NETBSD || defined AIX || defined OS2
345 #define SAL_CONST_CAST(type, expr) (const_cast< type >(expr))
346 #define SAL_REINTERPRET_CAST(type, expr) (reinterpret_cast< type >(expr))
347 #define SAL_STATIC_CAST(type, expr) (static_cast< type >(expr))
348 #else /* SAL_W32, SOLARIS, LINUX */
349 #define SAL_CONST_CAST(type, expr) ((type) (expr))
350 #define SAL_REINTERPRET_CAST(type, expr) ((type) (expr))
351 #define SAL_STATIC_CAST(type, expr) ((type) (expr))
352 #endif /* SAL_W32, SOLARIS, LINUX */
353 #endif /* __cplusplus */
354 
355 /** Definition of function throw clause macros.  These have been introduced
356 	to reduce code size by balancing out compiler bugs.
357 
358 	These macros are ONLY for function declarations,
359 	use common C++ throw statement for throwing exceptions, e.g.
360 	throw RuntimeException();
361 
362 	SAL_THROW()			 should be used for all C++ functions, e.g. SAL_THROW( () )
363 	SAL_THROW_EXTERN_C() should be used for all C functions
364 */
365 #ifdef __cplusplus
366 #if defined(__GNUC__) || defined(__SUNPRO_CC) || defined(__sgi)
367 #define SAL_THROW( exc )
368 #else /* MSVC, all other */
369 #define SAL_THROW( exc ) throw exc
370 #endif /* __GNUC__, __SUNPRO_CC */
371 #define SAL_THROW_EXTERN_C() throw ()
372 #else /* ! __cplusplus */
373 /* SAL_THROW() must not be used in C headers, only SAL_THROW_EXTERN_C() is defined */
374 #define SAL_THROW_EXTERN_C()
375 #endif
376 
377 
378 
379 #ifdef __cplusplus
380 enum __sal_NoAcquire
381 {
382 	/** definition of a no acquire enum for ctors
383 	*/
384 	SAL_NO_ACQUIRE
385 };
386 #endif /* __cplusplus */
387 
388 
389 #ifdef __cplusplus
390 }
391 #endif /* __cplusplus */
392 
393 #ifdef __cplusplus
394 
395 namespace sal {
396 
397 /**
398    A static_cast between integral types, to avoid C++ compiler warnings.
399 
400    In C++ source code, use sal::static_int_cast<T>(n) instead of
401    static_cast<T>(n) whenever a compiler warning about integral type problems
402    shall be silenced.  That way, source code that needs to be modified when the
403    type of any of the expressions involved in the compiler warning is changed
404    can be found more easily.
405 
406    Both template arguments T1 and T2 must be integral types.
407 */
static_int_cast(T2 n)408 template< typename T1, typename T2 > inline T1 static_int_cast(T2 n) {
409     return static_cast< T1 >(n);
410 }
411 
412 }
413 
414 #else /* __cplusplus */
415 
416 /**
417    A cast between integer types, to avoid C compiler warnings.
418 
419    In C source code, use SAL_INT_CAST(type, expr) instead of ((type) (expr))
420    whenever a compiler warning about integer type problems shall be silenced.
421    That way, source code that needs to be modified when the type of any of the
422    expressions involved in the compiler warning is changed can be found more
423    easily.
424 
425    The argument 'type' must be an integer type and the argument 'expr' must be
426    an integer expression.  Both arguments are evaluated exactly once.
427 */
428 #define SAL_INT_CAST(type, expr) ((type) (expr))
429 
430 #endif /* __cplusplus */
431 
432 #endif /*_SAL_TYPES_H_ */
433 
434