update credits
[LibreOffice.git] / include / sal / types.h
blob55f2e729ac8b803e8da7afff3ab8dee42900dfdf
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef _SAL_TYPES_H_
21 #define _SAL_TYPES_H_
23 #include <sal/config.h>
24 #include <sal/macros.h>
26 #include <sal/typesizes.h>
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
32 /********************************************************************************/
33 /* Data types
36 /* Boolean */
37 typedef unsigned char sal_Bool;
38 # define sal_False ((sal_Bool)0)
39 # define sal_True ((sal_Bool)1)
41 /* char is assumed to always be 1 byte long */
42 typedef signed char sal_Int8;
43 typedef unsigned char sal_uInt8;
45 #if SAL_TYPES_SIZEOFSHORT == 2
46 typedef signed short sal_Int16;
47 typedef unsigned short sal_uInt16;
48 #else
49 #error "Could not find 16-bit type, add support for your architecture"
50 #endif
52 #if SAL_TYPES_SIZEOFLONG == 4
53 typedef signed long sal_Int32;
54 typedef unsigned long sal_uInt32;
55 #define SAL_PRIdINT32 "ld"
56 #define SAL_PRIuUINT32 "lu"
57 #define SAL_PRIxUINT32 "lx"
58 #define SAL_PRIXUINT32 "lX"
59 #elif SAL_TYPES_SIZEOFINT == 4
60 typedef signed int sal_Int32;
61 typedef unsigned int sal_uInt32;
62 #define SAL_PRIdINT32 "d"
63 #define SAL_PRIuUINT32 "u"
64 #define SAL_PRIxUINT32 "x"
65 #define SAL_PRIXUINT32 "X"
66 #else
67 #error "Could not find 32-bit type, add support for your architecture"
68 #endif
70 #ifdef _MSC_VER
71 typedef __int64 sal_Int64;
72 typedef unsigned __int64 sal_uInt64;
74 /* The following are macros that will add the 64 bit constant suffix. */
75 #define SAL_CONST_INT64(x) x##i64
76 #define SAL_CONST_UINT64(x) x##ui64
78 #define SAL_PRIdINT64 "I64d"
79 #define SAL_PRIuUINT64 "I64u"
80 #define SAL_PRIxUINT64 "I64x"
81 #define SAL_PRIXUINT64 "I64X"
82 #elif defined(__SUNPRO_CC) || defined(__SUNPRO_C) || defined (__GNUC__) || defined (sgi)
83 #if SAL_TYPES_SIZEOFLONG == 8
84 typedef signed long int sal_Int64;
85 typedef unsigned long int sal_uInt64;
88 /* The following are macros that will add the 64 bit constant suffix. */
89 #define SAL_CONST_INT64(x) x##l
90 #define SAL_CONST_UINT64(x) x##ul
92 #define SAL_PRIdINT64 "ld"
93 #define SAL_PRIuUINT64 "lu"
94 #define SAL_PRIxUINT64 "lx"
95 #define SAL_PRIXUINT64 "lX"
96 #elif SAL_TYPES_SIZEOFLONGLONG == 8
97 typedef signed long long sal_Int64;
98 typedef unsigned long long sal_uInt64;
100 /* The following are macros that will add the 64 bit constant suffix. */
101 #define SAL_CONST_INT64(x) x##ll
102 #define SAL_CONST_UINT64(x) x##ull
104 #ifdef __MINGW32__
105 #define SAL_PRIdINT64 "I64d"
106 #define SAL_PRIuUINT64 "I64u"
107 #define SAL_PRIxUINT64 "I64x"
108 #define SAL_PRIXUINT64 "I64X"
109 #else
110 #define SAL_PRIdINT64 "lld"
111 #define SAL_PRIuUINT64 "llu"
112 #define SAL_PRIxUINT64 "llx"
113 #define SAL_PRIXUINT64 "llX"
114 #endif
115 #else
116 #error "Could not find 64-bit type, add support for your architecture"
117 #endif
118 #else
119 #error "Please define the 64-bit types for your architecture/compiler in sal/inc/sal/types.h"
120 #endif
122 typedef char sal_Char;
123 typedef signed char sal_sChar;
124 typedef unsigned char sal_uChar;
126 #if ( defined(SAL_W32) && !defined(__MINGW32__) )
127 // http://msdn.microsoft.com/en-us/library/s3f49ktz%28v=vs.80%29.aspx
128 // "By default wchar_t is a typedef for unsigned short."
129 // But MinGW has a native wchar_t, and on many places, we cannot deal with
130 // that, so sal_Unicode has to be explicitly typedef'd as sal_uInt16 there.
131 typedef wchar_t sal_Unicode;
132 #else
133 #define SAL_UNICODE_NOTEQUAL_WCHAR_T
134 typedef sal_uInt16 sal_Unicode;
135 #endif
137 typedef void * sal_Handle;
139 /* sal_Size should currently be the native width of the platform */
140 #if SAL_TYPES_SIZEOFPOINTER == 4
141 typedef sal_uInt32 sal_Size;
142 typedef sal_Int32 sal_sSize;
143 #elif SAL_TYPES_SIZEOFPOINTER == 8
144 typedef sal_uInt64 sal_Size;
145 typedef sal_Int64 sal_sSize;
146 #else
147 #error "Please make sure SAL_TYPES_SIZEOFPOINTER is defined for your architecture/compiler"
148 #endif
150 /* sal_PtrDiff holds the result of a pointer subtraction */
151 #if SAL_TYPES_SIZEOFPOINTER == 4
152 typedef sal_Int32 sal_PtrDiff;
153 #elif SAL_TYPES_SIZEOFPOINTER == 8
154 typedef sal_Int64 sal_PtrDiff;
155 #else
156 #error "Please make sure SAL_TYPES_SIZEOFPOINTER is defined for your architecture/compiler"
157 #endif
159 /* printf-style conversion specification length modifiers for size_t and
160 ptrdiff_t (most platforms support C99, MSC has its own extension) */
161 #if defined(_MSC_VER) || defined(__MINGW32__)
162 #define SAL_PRI_SIZET "I"
163 #define SAL_PRI_PTRDIFFT "I"
164 #else
165 #define SAL_PRI_SIZET "z"
166 #define SAL_PRI_PTRDIFFT "t"
167 #endif
169 /* sal_IntPtr, sal_uIntPtr are integer types designed to hold pointers so that any valid
170 * pointer to void can be converted to this type and back to a pointer to void and the
171 * result will compare to the original pointer */
172 #if SAL_TYPES_SIZEOFPOINTER == 4
173 typedef sal_Int32 sal_IntPtr;
174 typedef sal_uInt32 sal_uIntPtr;
175 #define SAL_PRIdINTPTR SAL_PRIdINT32
176 #define SAL_PRIuUINTPTR SAL_PRIuUINT32
177 #define SAL_PRIxUINTPTR SAL_PRIxUINT32
178 #define SAL_PRIXUINTPTR SAL_PRIXUINT32
179 #elif SAL_TYPES_SIZEOFPOINTER == 8
180 typedef sal_Int64 sal_IntPtr;
181 typedef sal_uInt64 sal_uIntPtr;
182 #define SAL_PRIdINTPTR SAL_PRIdINT64
183 #define SAL_PRIuUINTPTR SAL_PRIuUINT64
184 #define SAL_PRIxUINTPTR SAL_PRIxUINT64
185 #define SAL_PRIXUINTPTR SAL_PRIXUINT64
186 #else
187 #error "Please make sure SAL_TYPES_SIZEOFPOINTER is defined for your architecture/compiler"
188 #endif
190 /********************************************************************************/
191 /* Useful defines
194 /* The following SAL_MIN_INTn defines codify the assumption that the signed
195 * sal_Int types use two's complement representation. Defining them as
196 * "-0x7F... - 1" instead of as "-0x80..." prevents warnings about applying the
197 * unary minus operator to unsigned quantities.
199 #define SAL_MIN_INT8 ((sal_Int8) (-0x7F - 1))
200 #define SAL_MAX_INT8 ((sal_Int8) 0x7F)
201 #define SAL_MAX_UINT8 ((sal_uInt8) 0xFF)
202 #define SAL_MIN_INT16 ((sal_Int16) (-0x7FFF - 1))
203 #define SAL_MAX_INT16 ((sal_Int16) 0x7FFF)
204 #define SAL_MAX_UINT16 ((sal_uInt16) 0xFFFF)
205 #define SAL_MIN_INT32 ((sal_Int32) (-0x7FFFFFFF - 1))
206 #define SAL_MAX_INT32 ((sal_Int32) 0x7FFFFFFF)
207 #define SAL_MAX_UINT32 ((sal_uInt32) 0xFFFFFFFF)
208 #define SAL_MIN_INT64 ((sal_Int64) (SAL_CONST_INT64(-0x7FFFFFFFFFFFFFFF) - 1))
209 #define SAL_MAX_INT64 ((sal_Int64) SAL_CONST_INT64(0x7FFFFFFFFFFFFFFF))
210 #define SAL_MAX_UINT64 ((sal_uInt64) SAL_CONST_UINT64(0xFFFFFFFFFFFFFFFF))
212 #if SAL_TYPES_SIZEOFLONG == 4
213 #define SAL_MAX_SSIZE SAL_MAX_INT32
214 #define SAL_MAX_SIZE SAL_MAX_UINT32
215 #elif SAL_TYPES_SIZEOFLONG == 8
216 #define SAL_MAX_SSIZE SAL_MAX_INT64
217 #define SAL_MAX_SIZE SAL_MAX_UINT64
218 #endif
220 #if defined(SAL_W32) || defined(SAL_UNX)
221 # define SAL_MAX_ENUM 0x7fffffff
222 #endif
224 #if defined(_MSC_VER) || defined(__MINGW32__)
225 # define SAL_DLLPUBLIC_EXPORT __declspec(dllexport)
226 # define SAL_JNI_EXPORT __declspec(dllexport)
227 #if defined(_MSC_VER)
228 # define SAL_DLLPUBLIC_IMPORT __declspec(dllimport)
229 #else
230 # define SAL_DLLPUBLIC_IMPORT
231 #endif // defined(_MSC_VER)
232 # define SAL_DLLPRIVATE
233 # define SAL_DLLPUBLIC_TEMPLATE
234 # define SAL_CALL __cdecl
235 # define SAL_CALL_ELLIPSE __cdecl
236 #elif defined SAL_UNX
237 # if defined(__SUNPRO_CC) && (__SUNPRO_CC >= 0x550)
238 # define SAL_DLLPUBLIC_EXPORT __global
239 # define SAL_JNI_EXPORT __global
240 # define SAL_DLLPUBLIC_IMPORT
241 # define SAL_DLLPRIVATE __hidden
242 # define SAL_DLLPUBLIC_TEMPLATE
243 # elif defined(__SUNPRO_C ) && (__SUNPRO_C >= 0x550)
244 # define SAL_DLLPUBLIC_EXPORT __global
245 # define SAL_JNI_EXPORT __global
246 # define SAL_DLLPUBLIC_IMPORT
247 # define SAL_DLLPRIVATE __hidden
248 # define SAL_DLLPUBLIC_TEMPLATE
249 # elif defined(__GNUC__) && defined(HAVE_GCC_VISIBILITY_FEATURE)
250 # if defined(DISABLE_DYNLOADING)
251 # define SAL_DLLPUBLIC_EXPORT __attribute__ ((visibility("hidden")))
252 # define SAL_JNI_EXPORT __attribute__ ((visibility("default")))
253 # define SAL_DLLPUBLIC_IMPORT __attribute__ ((visibility("hidden")))
254 # define SAL_DLLPRIVATE __attribute__ ((visibility("hidden")))
255 # define SAL_DLLPUBLIC_TEMPLATE __attribute__ ((visibility("hidden")))
256 # else
257 # define SAL_DLLPUBLIC_EXPORT __attribute__ ((visibility("default")))
258 # define SAL_JNI_EXPORT __attribute__ ((visibility("default")))
259 # define SAL_DLLPUBLIC_IMPORT __attribute__ ((visibility("default")))
260 # define SAL_DLLPRIVATE __attribute__ ((visibility("hidden")))
261 # define SAL_DLLPUBLIC_TEMPLATE __attribute__ ((visibility("default")))
262 # endif
263 # else
264 # define SAL_DLLPUBLIC_EXPORT
265 # define SAL_JNI_EXPORT
266 # define SAL_DLLPUBLIC_IMPORT
267 # define SAL_DLLPRIVATE
268 # define SAL_DLLPUBLIC_TEMPLATE
269 # endif
270 # define SAL_CALL
271 # define SAL_CALL_ELLIPSE
272 #else
273 # error("unknown platform")
274 #endif
277 Exporting the symbols necessary for exception handling on GCC.
279 These macros are used for inline declarations of exception classes, as in
280 rtl/malformeduriexception.hxx.
282 #if defined(__GNUC__) && ! defined(__MINGW32__)
283 # if defined(DISABLE_DYNLOADING)
284 # define SAL_EXCEPTION_DLLPUBLIC_EXPORT __attribute__((visibility("default")))
285 # else
286 # define SAL_EXCEPTION_DLLPUBLIC_EXPORT SAL_DLLPUBLIC_EXPORT
287 # endif
288 # define SAL_EXCEPTION_DLLPRIVATE SAL_DLLPRIVATE
289 #else
290 # define SAL_EXCEPTION_DLLPUBLIC_EXPORT
291 # define SAL_EXCEPTION_DLLPRIVATE
292 #endif
294 /** Use this as markup for functions and methods whose return value must be
295 checked.
297 Compilers that support a construct of this nature will emit a compile
298 time warning on unchecked return value.
300 #if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1))
301 # define SAL_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
302 #else
303 # define SAL_WARN_UNUSED_RESULT
304 #endif
306 /** Use this for pure virtual classes, e.g. class SAL_NO_VTABLE Foo { ...
307 This hinders the compiler from setting a generic vtable stating that
308 a pure virtual function was called and thus slightly reduces code size.
310 #ifdef _MSC_VER
311 # define SAL_NO_VTABLE __declspec(novtable)
312 #else
313 # define SAL_NO_VTABLE
314 #endif
316 #ifdef SAL_W32
317 # pragma pack(push, 8)
318 #endif
320 /** This is the binary specification of a SAL sequence.
321 <br>
323 typedef struct _sal_Sequence
325 /** reference count of sequence<br>
327 sal_Int32 nRefCount;
328 /** element count<br>
330 sal_Int32 nElements;
331 /** elements array<br>
333 char elements[1];
334 } sal_Sequence;
336 #define SAL_SEQUENCE_HEADER_SIZE ((sal_Size)&((sal_Sequence *)0)->elements)
338 #if defined( SAL_W32)
339 #pragma pack(pop)
340 #endif
342 /** Definition of function throw clause macros. These have been introduced
343 to reduce code size by balancing out compiler bugs.
345 These macros are ONLY for function declarations,
346 use common C++ throw statement for throwing exceptions, e.g.
347 throw RuntimeException();
349 SAL_THROW() should be used for all C++ functions, e.g. SAL_THROW(())
350 SAL_THROW_EXTERN_C() should be used for all C functions
352 #ifdef __cplusplus
353 #if defined(__GNUC__) || defined(__SUNPRO_CC) || defined(__sgi)
354 #define SAL_THROW( exc )
355 #else /* MSVC, all other */
356 #define SAL_THROW( exc ) throw exc
357 #endif /* __GNUC__, __SUNPRO_CC */
358 #define SAL_THROW_EXTERN_C() throw ()
359 #else /* ! __cplusplus */
360 /* SAL_THROW() must not be used in C headers, only SAL_THROW_EXTERN_C() is defined */
361 #define SAL_THROW_EXTERN_C()
362 #endif
364 #ifdef __cplusplus
366 #endif /* __cplusplus */
368 #ifdef __cplusplus
370 enum __sal_NoAcquire
372 /** definition of a no acquire enum for ctors
374 SAL_NO_ACQUIRE
377 namespace com { namespace sun { namespace star { } } }
379 /** short-circuit extra-verbose API namespaces
381 @since LibreOffice 4.0
383 namespace css = ::com::sun::star;
385 /** C++11 "= delete" feature.
387 With HAVE_CXX11_DELETE, calling a deleted function will cause a compile-time
388 error, while otherwise it will only cause a link-time error as the declared
389 function is not defined.
391 @since LibreOffice 4.1
393 #if HAVE_CXX11_DELETE
394 #define SAL_DELETED_FUNCTION = delete
395 #else
396 #define SAL_DELETED_FUNCTION
397 #endif
399 /** C++11 "override" feature.
401 With HAVE_CXX11_OVERRIDE, force the method to override a existing method in
402 parent, error out if the method with the correct signature does not exist.
404 @since LibreOffice 4.1
406 #if HAVE_CXX11_OVERRIDE
407 #define SAL_OVERRIDE override
408 #else
409 #define SAL_OVERRIDE
410 #endif
412 /** C++11 "final" feature.
414 With HAVE_CXX11_FINAL, mark a class as non-derivable or a method as non-overridable.
416 @since LibreOffice 4.1
418 #if HAVE_CXX11_FINAL
419 #define SAL_FINAL final
420 #else
421 #define SAL_FINAL
422 #endif
424 #endif /* __cplusplus */
426 #ifdef __cplusplus
428 namespace sal {
431 A static_cast between integral types, to avoid C++ compiler warnings.
433 In C++ source code, use sal::static_int_cast<T>(n) instead of
434 static_cast<T>(n) whenever a compiler warning about integral type problems
435 shall be silenced. That way, source code that needs to be modified when the
436 type of any of the expressions involved in the compiler warning is changed
437 can be found more easily.
439 Both template arguments T1 and T2 must be integral types.
441 template< typename T1, typename T2 > inline T1 static_int_cast(T2 n) {
442 return static_cast< T1 >(n);
447 #else /* __cplusplus */
450 A cast between integer types, to avoid C compiler warnings.
452 In C source code, use SAL_INT_CAST(type, expr) instead of ((type) (expr))
453 whenever a compiler warning about integer type problems shall be silenced.
454 That way, source code that needs to be modified when the type of any of the
455 expressions involved in the compiler warning is changed can be found more
456 easily.
458 The argument 'type' must be an integer type and the argument 'expr' must be
459 an integer expression. Both arguments are evaluated exactly once.
461 #define SAL_INT_CAST(type, expr) ((type) (expr))
463 #endif /* __cplusplus */
466 Use as follows:
467 SAL_DEPRECATED("Dont use, its evil.") void doit(int nPara);
470 #if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5))
471 # define SAL_DEPRECATED(message) __attribute__((deprecated(message)))
472 #elif (__GNUC__)
473 # define SAL_DEPRECATED(message) __attribute__((deprecated))
474 #elif defined(_MSC_VER)
475 # define SAL_DEPRECATED(message) __declspec(deprecated(message))
476 #else
477 # define SAL_DEPRECATED(message)
478 #endif
481 This macro is used to tag interfaces that are deprecated for both
482 internal and external API users, but where we are still writing
483 out the internal usage. Ultimately these should be replaced by
484 SAL_DEPRECATED, and then removed.
486 Use as follows:
487 SAL_DEPRECATED_INTERNAL("Dont use, its evil.") void doit(int nPara);
489 #ifdef LIBO_INTERNAL_ONLY
490 # define SAL_DEPRECATED_INTERNAL(message)
491 #else
492 # define SAL_DEPRECATED_INTERNAL(message) SAL_DEPRECATED(message)
493 #endif
496 Use as follows:
497 SAL_WNODEPRECATED_DECLARATIONS_PUSH
498 \::std::auto_ptr<X> ...
499 SAL_WNODEPRECATED_DECLARATIONS_POP
502 #if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || defined(__clang__)
503 #define SAL_WNODEPRECATED_DECLARATIONS_PUSH \
504 _Pragma(SAL_STRINGIFY_ARG(GCC diagnostic push)) \
505 _Pragma(SAL_STRINGIFY_ARG(GCC diagnostic ignored "-Wdeprecated-declarations"))
506 #define SAL_WNODEPRECATED_DECLARATIONS_POP \
507 _Pragma(SAL_STRINGIFY_ARG(GCC diagnostic pop))
508 #else
509 # define SAL_WNODEPRECATED_DECLARATIONS_PUSH
510 # define SAL_WNODEPRECATED_DECLARATIONS_POP
511 #endif
513 /** Annotate unused but required C++ function parameters.
515 An unused parameter is required if the function needs to adhere to a given
516 type (e.g., if its address is assigned to a function pointer of a specific
517 type, or if it is called from template code). This annotation helps static
518 analysis tools suppress false warnings. In the case of virtual functions
519 (where unused required parameters are common, too), the annotation is not
520 required (as static analysis tools can themselves derive the information
521 whether a function is virtual).
523 Use the annotation in function definitions like
525 void f(SAL_UNUSED_PARAMETER int) {}
527 C does not allow unnamed parameters, anyway, so a function definition like
528 the above needs to be written there as
530 void f(int dummy) { (void) dummy; / * avoid warnings * / }
532 without a SAL_UNUSED_PARAMETER annotation.
534 @since LibreOffice 3.6
536 #if defined __cplusplus
537 #if defined __GNUC__
538 #define SAL_UNUSED_PARAMETER __attribute__ ((unused))
539 #else
540 #define SAL_UNUSED_PARAMETER
541 #endif
542 #endif
546 Annotate classes where a compiler should warn if an instance is unused.
548 The compiler cannot warn about unused instances if they have non-trivial
549 or external constructors or destructors. Classes marked with SAL_WARN_UNUSED
550 will be warned about.
552 Currently implemented by a Clang compiler plugin.
554 @since LibreOffice 4.0
558 #if defined __clang__
559 #define SAL_WARN_UNUSED __attribute__((annotate("lo_warn_unused")))
560 #else
561 #define SAL_WARN_UNUSED
562 #endif
564 #endif /*_SAL_TYPES_H_ */
566 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */