Solid
soliddefs_p.h
Go to the documentation of this file.
00001 /* 00002 Copyright 2006-2007 Kevin Ottens <ervin@kde.org> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Lesser General Public 00006 License as published by the Free Software Foundation; either 00007 version 2.1 of the License, or (at your option) version 3, or any 00008 later version accepted by the membership of KDE e.V. (or its 00009 successor approved by the membership of KDE e.V.), which shall 00010 act as a proxy defined in Section 6 of version 3 of the license. 00011 00012 This 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 GNU 00015 Lesser General Public License for more details. 00016 00017 You should have received a copy of the GNU Lesser General Public 00018 License along with this library. If not, see <http://www.gnu.org/licenses/>. 00019 */ 00020 00021 #ifndef SOLID_SOLIDDEFS_P_H 00022 #define SOLID_SOLIDDEFS_P_H 00023 00024 #include <QtCore/QObject> 00025 00026 #define return_SOLID_CALL(Type, Object, Default, Method) \ 00027 Type t = qobject_cast<Type>(Object); \ 00028 if (t!=0) \ 00029 { \ 00030 return t->Method; \ 00031 } \ 00032 else \ 00033 { \ 00034 return Default; \ 00035 } 00036 00037 00038 00039 #define SOLID_CALL(Type, Object, Method) \ 00040 Type t = qobject_cast<Type>(Object); \ 00041 if (t!=0) \ 00042 { \ 00043 t->Method; \ 00044 } 00045 00046 // 00047 // WARNING!! 00048 // This code uses undocumented Qt API 00049 // Do not copy it to your application! Use only the functions that are here! 00050 // Otherwise, it could break when a new version of Qt ships. 00051 // 00052 00053 /* 00054 * Lame copy of K_GLOBAL_STATIC below, but that's the price for 00055 * being completely kdecore independent. 00056 */ 00057 00058 namespace Solid 00059 { 00060 typedef void (*CleanUpFunction)(); 00061 00062 class CleanUpGlobalStatic 00063 { 00064 public: 00065 Solid::CleanUpFunction func; 00066 00067 inline ~CleanUpGlobalStatic() { func(); } 00068 }; 00069 } 00070 00071 #ifdef Q_CC_MSVC 00072 # define SOLID_GLOBAL_STATIC_STRUCT_NAME(NAME) _solid_##NAME##__LINE__ 00073 #else 00074 # define SOLID_GLOBAL_STATIC_STRUCT_NAME(NAME) 00075 #endif 00076 00077 #define SOLID_GLOBAL_STATIC(TYPE, NAME) SOLID_GLOBAL_STATIC_WITH_ARGS(TYPE, NAME, ()) 00078 00079 #define SOLID_GLOBAL_STATIC_WITH_ARGS(TYPE, NAME, ARGS) \ 00080 static QBasicAtomicPointer<TYPE > _solid_static_##NAME = Q_BASIC_ATOMIC_INITIALIZER(0);\ 00081 static bool _solid_static_##NAME##_destroyed; \ 00082 static struct SOLID_GLOBAL_STATIC_STRUCT_NAME(NAME) \ 00083 { \ 00084 bool isDestroyed() \ 00085 { \ 00086 return _solid_static_##NAME##_destroyed; \ 00087 } \ 00088 inline operator TYPE*() \ 00089 { \ 00090 return operator->(); \ 00091 } \ 00092 inline TYPE *operator->() \ 00093 { \ 00094 if (!_solid_static_##NAME) { \ 00095 if (isDestroyed()) { \ 00096 qFatal("Fatal Error: Accessed global static '%s *%s()' after destruction. " \ 00097 "Defined at %s:%d", #TYPE, #NAME, __FILE__, __LINE__); \ 00098 } \ 00099 TYPE *x = new TYPE ARGS; \ 00100 if (!_solid_static_##NAME.testAndSetOrdered(0, x) \ 00101 && _solid_static_##NAME != x ) { \ 00102 delete x; \ 00103 } else { \ 00104 static Solid::CleanUpGlobalStatic cleanUpObject = { destroy }; \ 00105 } \ 00106 } \ 00107 return _solid_static_##NAME; \ 00108 } \ 00109 inline TYPE &operator*() \ 00110 { \ 00111 return *operator->(); \ 00112 } \ 00113 static void destroy() \ 00114 { \ 00115 _solid_static_##NAME##_destroyed = true; \ 00116 TYPE *x = _solid_static_##NAME; \ 00117 _solid_static_##NAME = 0; \ 00118 delete x; \ 00119 } \ 00120 } NAME; 00121 00122 #endif
KDE 4.6 API Reference