1 /* This file is part of the KDE libraries
2 Copyright (c) 2002-2003 KDE Team
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.
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.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
23 * This header defines several compiler-independent macros which are used
24 * throughout KDE. Most of these macros make use of GCC extensions; on other
25 * compilers, they don't have any effect.
28 #ifndef _KDE_MACROS_H_
29 #define _KDE_MACROS_H_
31 #cmakedefine __KDE_HAVE_GCC_VISIBILITY
37 * The KDE_NO_EXPORT macro marks the symbol of the given variable
38 * to be hidden. A hidden symbol is stripped during the linking step,
39 * so it can't be used from outside the resulting library, which is similar
40 * to static. However, static limits the visibility to the current
41 * compilation unit. Hidden symbols can still be used in multiple compilation
45 * int KDE_NO_EXPORT foo;
56 * The KDE_EXPORT macro marks the symbol of the given variable
57 * to be visible, so it can be used from outside the resulting library.
60 * int KDE_NO_EXPORT foo;
72 #ifdef __KDE_HAVE_GCC_VISIBILITY
73 #define KDE_NO_EXPORT __attribute__ ((visibility("hidden")))
74 #define KDE_EXPORT __attribute__ ((visibility("default")))
75 #define KDE_IMPORT __attribute__ ((visibility("default")))
76 #elif defined(_WIN32) || defined(_WIN64)
78 #define KDE_EXPORT __declspec(dllexport)
79 #define KDE_IMPORT __declspec(dllimport)
90 * The KDE_PACKED macro can be used to hint the compiler that a particular
91 * structure or class should not contain unnecessary paddings.
95 #define KDE_PACKED __attribute__((__packed__))
101 * @def KDE_DEPRECATED
104 * The KDE_DEPRECATED macro can be used to trigger compile-time warnings
105 * with newer compilers when deprecated functions are used.
107 * For non-inline functions, the macro gets inserted at front of the
108 * function declaration, right before the return type:
111 * KDE_DEPRECATED void deprecatedFunctionA();
112 * KDE_DEPRECATED int deprecatedFunctionB() const;
115 * For functions which are implemented inline,
116 * the KDE_DEPRECATED macro is inserted at the front, right before the return
117 * type, but after "static", "inline" or "virtual":
120 * KDE_DEPRECATED void deprecatedInlineFunctionA() { .. }
121 * virtual KDE_DEPRECATED int deprecatedInlineFunctionB() { .. }
122 * static KDE_DEPRECATED bool deprecatedInlineFunctionC() { .. }
123 * inline KDE_DEPRECATED bool deprecatedInlineFunctionD() { .. }
126 * You can also mark whole structs or classes as deprecated, by inserting the
127 * KDE_DEPRECATED macro after the struct/class keyword, but before the
128 * name of the struct/class:
131 * class KDE_DEPRECATED DeprecatedClass { };
132 * struct KDE_DEPRECATED DeprecatedStruct { };
136 * It does not make much sense to use the KDE_DEPRECATED keyword for a Qt signal;
137 * this is because usually get called by the class which they belong to,
138 * and one would assume that a class author does not use deprecated methods of
139 * his own class. The only exception to this are signals which are connected to
140 * other signals; they get invoked from moc-generated code. In any case,
141 * printing a warning message in either case is not useful.
142 * For slots, it can make sense (since slots can be invoked directly) but be
143 * aware that if the slots get triggered by a signal, the will get called from
144 * moc code as well and thus the warnings are useless.
147 * Also note that it is not possible to use KDE_DEPRECATED for classes which
148 * use the k_dcop keyword (to indicate a DCOP interface declaration); this is
149 * because the dcopidl program would choke on the unexpected declaration
153 * KDE_DEPRECATED cannot be used at the end of the declaration anymore,
154 * unlike what is done for KDE3.
157 * KDE_DEPRECATED cannot be used for constructors,
158 * use KDE_CONSTRUCTOR_DEPRECATED instead.
162 # include <QtCore/qglobal.h>
163 # ifndef KDE_DEPRECATED
164 # ifdef KDE_DEPRECATED_WARNINGS
165 # define KDE_DEPRECATED Q_DECL_DEPRECATED
167 # define KDE_DEPRECATED
173 * @def KDE_CONSTRUCTOR_DEPRECATED
176 * The KDE_CONSTRUCTOR_DEPRECATED macro can be used to trigger compile-time
177 * warnings with newer compilers when deprecated constructors are used.
179 * For non-inline constructors, the macro gets inserted at front of the
180 * constructor declaration, right before the return type:
183 * KDE_CONSTRUCTOR_DEPRECATED classA();
186 * For constructors which are implemented inline,
187 * the KDE_CONSTRUCTOR_DEPRECATED macro is inserted at the front,
188 * but after the "inline" keyword:
191 * KDE_CONSTRUCTOR_DEPRECATED classA() { .. }
194 * \note Do not forget that inlined constructors are not allowed in public
198 #ifndef KDE_CONSTRUCTOR_DEPRECATED
200 # if __GNUC__ == 3 && __GNUC_MINOR__ <= 3
201 /* GCC 3.3.x cannot handle Qt 4.1.2's definition of Q_DECL_CONSTRUCTOR_DEPRECATED */
202 # define KDE_CONSTRUCTOR_DEPRECATED
204 # define KDE_CONSTRUCTOR_DEPRECATED Q_DECL_CONSTRUCTOR_DEPRECATED
207 # define KDE_CONSTRUCTOR_DEPRECATED Q_DECL_CONSTRUCTOR_DEPRECATED
215 * The KDE_ISLIKELY macro tags a boolean expression as likely to evaluate to
216 * @c true. When used in an <tt>if ( )</tt> statement, it gives a hint to the compiler
217 * that the following codeblock is likely to get executed. Providing this
218 * information helps the compiler to optimize the code for better performance.
219 * Using the macro has an insignificant code size or runtime memory footprint impact.
220 * The code semantics is not affected.
225 * if ( KDE_ISLIKELY( testsomething() ) )
226 * abort(); // assume its likely that the application aborts
230 * Providing wrong information ( like marking a condition that almost never
231 * passes as 'likely' ) will cause a significant runtime slowdown. Therefore only
232 * use it for cases where you can be sure about the odds of the expression to pass
233 * in all cases ( independent from e.g. user configuration ).
236 * Do NOT use ( !KDE_ISLIKELY(foo) ) as an replacement for KDE_ISUNLIKELY() !
242 * @def KDE_ISUNLIKELY
245 * The KDE_ISUNLIKELY macro tags a boolean expression as likely to evaluate to
246 * @c false. When used in an <tt>if ( )</tt> statement, it gives a hint to the compiler
247 * that the following codeblock is unlikely to get executed. Providing this
248 * information helps the compiler to optimize the code for better performance.
249 * Using the macro has an insignificant code size or runtime memory footprint impact.
250 * The code semantics is not affected.
255 * if ( KDE_ISUNLIKELY( testsomething() ) )
256 * abort(); // assume its unlikely that the application aborts
260 * Providing wrong information ( like marking a condition that almost never
261 * passes as 'unlikely' ) will cause a significant runtime slowdown. Therefore only
262 * use it for cases where you can be sure about the odds of the expression to pass
263 * in all cases ( independent from e.g. user configuration ).
266 * Do NOT use ( !KDE_ISUNLIKELY(foo) ) as an replacement for KDE_ISLIKELY() !
271 #if defined(__GNUC__) && __GNUC__ - 0 >= 3
272 # define KDE_ISLIKELY( x ) __builtin_expect(!!(x),1)
273 # define KDE_ISUNLIKELY( x ) __builtin_expect(!!(x),0)
275 # define KDE_ISLIKELY( x ) ( x )
276 # define KDE_ISUNLIKELY( x ) ( x )
282 * This macro, and it's friends going up to 10 reserve a fixed number of virtual
283 * functions in a class. Because adding virtual functions to a class changes the
284 * size of the vtable, adding virtual functions to a class breaks binary
285 * compatibility. However, by using this macro, and decrementing it as new
286 * virtual methods are added, binary compatibility can still be preserved.
288 * \note The added functions must be added to the header at the same location
289 * as the macro; changing the order of virtual functions in a header is also
290 * binary incompatible as it breaks the layout of the vtable.
292 #define RESERVE_VIRTUAL_1 \
293 virtual void reservedVirtual1() {}
297 #define RESERVE_VIRTUAL_2 \
298 virtual void reservedVirtual2() {} \
303 #define RESERVE_VIRTUAL_3 \
304 virtual void reservedVirtual3() {} \
309 #define RESERVE_VIRTUAL_4 \
310 virtual void reservedVirtual4() {} \
315 #define RESERVE_VIRTUAL_5 \
316 virtual void reservedVirtual5() {} \
321 #define RESERVE_VIRTUAL_6 \
322 virtual void reservedVirtual6() {} \
327 #define RESERVE_VIRTUAL_7 \
328 virtual void reservedVirtual7() {} \
333 #define RESERVE_VIRTUAL_8 \
334 virtual void reservedVirtual8() {} \
339 #define RESERVE_VIRTUAL_9 \
340 virtual void reservedVirtual9() {} \
342 #define RESERVE_VIRTUAL_10 \
343 virtual void reservedVirtual10() {} \
347 * @def KDE_FULL_TEMPLATE_EXPORT_INSTANTIATION
350 * From Qt's global.h:
351 * Compilers which follow outdated template instantiation rules
352 * require a class to have a comparison operator to exist when
353 * a QList of this type is instantiated. It's not actually
354 * used in the list, though. Hence the dummy implementation.
355 * Just in case other code relies on it we better trigger a warning
356 * mandating a real implementation.
358 * In KDE we need this for classes which are exported in a shared
359 * lib because some compilers need a full instantiated class then.
361 * @sa KDE_DUMMY_COMPARISON_OPERATOR
362 * @sa KDE_DUMMY_QHASH_FUNCTION
366 * @def KDE_DUMMY_COMPARISON_OPERATOR
369 * The KDE_DUMMY_COMPARISON_OPERATOR defines a simple
370 * compare operator for classes.
372 * @sa KDE_FULL_TEMPLATE_EXPORT_INSTANTIATION
373 * @sa KDE_DUMMY_QHASH_FUNCTION
377 * @def KDE_DUMMY_QHASH_FUNCTION
380 * The KDE_DUMMY_QHASH_FUNCTION defines a simple
381 * hash-function for classes.
383 * @sa KDE_FULL_TEMPLATE_EXPORT_INSTANTIATION
384 * @sa KDE_DUMMY_COMPARISON_OPERATOR
387 #ifdef KDE_FULL_TEMPLATE_EXPORT_INSTANTIATION
388 # define KDE_DUMMY_COMPARISON_OPERATOR(C) \
389 bool operator==(const C&) const { \
390 qWarning(#C"::operator==(const "#C"&) was called"); \
393 # define KDE_DUMMY_QHASH_FUNCTION(C) \
394 inline uint qHash(const C) { \
395 qWarning("inline uint qHash(const "#C") was called"); \
399 # define KDE_DUMMY_COMPARISON_OPERATOR(C)
400 # define KDE_DUMMY_QHASH_FUNCTION(C)
407 * The KDE_BF_ENUM is used when storing an enum
408 * in a bitfield, to ensure correct conversion
411 * @sa KDE_CAST_BF_ENUM
415 * @def KDE_CAST_BF_ENUM
418 * The KDE_CAST_BF_ENUM is used when retrieving an
419 * enumfrom a bitfield, to ensure correct conversion
426 # define KDE_BF_ENUM(a) unsigned int
427 # define KDE_CAST_BF_ENUM(a,b) static_cast<a>(b)
429 # define KDE_BF_ENUM(a) a
430 # define KDE_CAST_BF_ENUM(a,b) b
434 * @def KDE_WEAK_SYMBOL
437 * The KDE_WEAK_SYMBOL macro can be used to tell the compiler that
438 * a particular function should be a weak symbol (that e.g. may be overriden
439 * in another library, -Bdirect will not bind this symbol directly)
443 #define KDE_WEAK_SYMBOL __attribute__((__weak__))
445 #define KDE_WEAK_SYMBOL
450 * @def KDE_MUST_USE_RESULT
453 * The KDE_MUST_USE_RESULT macro can be used to tell the compiler that
454 * a particular functions return value must be checked.
458 #define KDE_MUST_USE_RESULT __attribute__((__warn_unused_result__))
460 #define KDE_MUST_USE_RESULT
465 #endif /* _KDE_MACROS_H_ */