• R/O
  • HTTP
  • SSH
  • HTTPS

MUtilities: 提交

MUtilities development repository


Commit MetaInfo

修订版b7d04c37691ee33d3241699e4b11b3c566d70eba (tree)
时间2017-01-08 02:08:24
作者LoRd_MuldeR <mulder2@gmx....>
CommiterLoRd_MuldeR

Log Message

Some refactoring and clean-up in create_qt() method.

更改概述

差异

--- a/include/MUtils/Startup.h
+++ b/include/MUtils/Startup.h
@@ -40,7 +40,7 @@ namespace MUtils
4040 MUTILS_API int startup(int &argc, char **argv, main_function_t *const entry_point, const char* const appName, const bool &debugConsole);
4141
4242 //Initialize Qt
43- MUTILS_API QApplication *create_qt(int &argc, char **argv, const QString &appName);
43+ MUTILS_API QApplication *create_qt(int &argc, char **argv, const QString &appName, const QString &appAuthor = QLatin1String("LoRd_MuldeR"), const QString &appDomain = QLatin1String("muldersoft.com"));
4444 }
4545 }
4646
--- a/src/Startup.cpp
+++ b/src/Startup.cpp
@@ -39,11 +39,21 @@
3939 #include <QtPlugin>
4040 #if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
4141 #include <QAbstractNativeEventFilter>
42+#else
43+#define QAbstractNativeEventFilter QObject
44+#define Q_DECL_OVERRIDE
4245 #endif
4346
4447 //CRT
4548 #include <string.h>
4649
50+//MSVC
51+#if defined(_MSC_VER)
52+#define FORCE_INLINE __forceinline
53+#else
54+#define FORCE_INLINE inline
55+#endif
56+
4757 ///////////////////////////////////////////////////////////////////////////////
4858 // Qt Static Initialization
4959 ///////////////////////////////////////////////////////////////////////////////
@@ -126,12 +136,10 @@ static void qt_message_handler(QtMsgType type, const QMessageLogContext&, const
126136 }
127137 #endif
128138
129-#if QT_VERSION < QT_VERSION_CHECK(5,0,0)
130-static bool qt_event_filter(void *message, long *result)
131-{
132- return MUtils::OS::handle_os_message(message, result);
133-}
134-#else
139+///////////////////////////////////////////////////////////////////////////////
140+// EVENT FILTER
141+///////////////////////////////////////////////////////////////////////////////
142+
135143 namespace MUtils
136144 {
137145 namespace Startup
@@ -143,27 +151,43 @@ namespace MUtils
143151 public:
144152 bool nativeEventFilter(const QByteArray&, void *message, long *result) Q_DECL_OVERRIDE
145153 {
146- return MUtils::OS::handle_os_message(message, result);
154+ return filterEvent(message, result);
147155 };
156+
157+ static FORCE_INLINE bool filterEvent(void *message, long *result)
158+ {
159+ return MUtils::OS::handle_os_message(message, result);
160+ }
161+
162+ static NativeEventFilter *instance(void)
163+ {
164+ while (m_instance.isNull())
165+ {
166+ m_instance.reset(new NativeEventFilter());
167+ }
168+ return m_instance.data();
169+ }
170+
171+ private:
172+ NativeEventFilter(void) {}
173+ static QScopedPointer<MUtils::Startup::Internal::NativeEventFilter> m_instance;
148174 };
149175 }
150176 }
151177 }
152-static QScopedPointer<MUtils::Startup::Internal::NativeEventFilter> qt_event_filter;
153-#endif
154178
155179 ///////////////////////////////////////////////////////////////////////////////
156180 // STARTUP FUNCTION
157181 ///////////////////////////////////////////////////////////////////////////////
158182
159-static int startup_main(int &argc, char **argv, MUtils::Startup::main_function_t *const entry_point, const char* const appName, const bool &debugConsole)
183+static FORCE_INLINE int startup_main(int &argc, char **argv, MUtils::Startup::main_function_t *const entry_point, const char* const appName, const bool &debugConsole)
160184 {
161185 qInstallMsgHandler(qt_message_handler);
162186 MUtils::Terminal::setup(argc, argv, appName, MUTILS_DEBUG || debugConsole);
163187 return entry_point(argc, argv);
164188 }
165189
166-static int startup_helper(int &argc, char **argv, MUtils::Startup::main_function_t *const entry_point, const char* const appName, const bool &debugConsole)
190+static FORCE_INLINE int startup_helper(int &argc, char **argv, MUtils::Startup::main_function_t *const entry_point, const char* const appName, const bool &debugConsole)
167191 {
168192 int iResult = -1;
169193 try
@@ -220,7 +244,7 @@ int MUtils::Startup::startup(int &argc, char **argv, main_function_t *const entr
220244 static QMutex g_init_lock;
221245 static const char *const g_imageformats[] = {"bmp", "png", "jpg", "gif", "ico", "xpm", "svg", NULL};
222246
223-static QString getExecutableName(int &argc, char **argv)
247+static FORCE_INLINE QString getExecutableName(int &argc, char **argv)
224248 {
225249 if(argc >= 1)
226250 {
@@ -241,22 +265,23 @@ static QString getExecutableName(int &argc, char **argv)
241265 return QLatin1String("Program.exe");
242266 }
243267
244-static void qt_registry_cleanup(void)
268+static FORCE_INLINE void qt_registry_cleanup(void)
245269 {
246270 static const wchar_t *const QT_JUNK_KEY = L"Software\\Trolltech\\OrganizationDefaults";
247271 MUtils::Registry::reg_key_delete(MUtils::Registry::root_user, MUTILS_QSTR(QT_JUNK_KEY), true, true);
248272 }
249273
250-QApplication *MUtils::Startup::create_qt(int &argc, char **argv, const QString &appName)
274+QApplication *MUtils::Startup::create_qt(int &argc, char **argv, const QString &appName, const QString &appAuthor, const QString &appDomain)
251275 {
252276 QMutexLocker lock(&g_init_lock);
253277 const OS::ArgumentMap &arguments = MUtils::OS::arguments();
254278
255279 //Don't initialized again, if done already
256- if(QApplication::instance() != NULL)
280+ QScopedPointer<QApplication> application(dynamic_cast<QApplication*>(QApplication::instance()));
281+ if(!application.isNull())
257282 {
258283 qWarning("Qt is already initialized!");
259- return NULL;
284+ return application.take();
260285 }
261286
262287 //Extract executable name from argv[] array
@@ -321,7 +346,7 @@ QApplication *MUtils::Startup::create_qt(int &argc, char **argv, const QString &
321346 QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
322347
323348 //Create Qt application instance
324- QApplication *application = new QApplication(argc, argv);
349+ application.reset(new QApplication(argc, argv));
325350
326351 //Register the Qt clean-up function
327352 atexit(qt_registry_cleanup);
@@ -332,13 +357,12 @@ QApplication *MUtils::Startup::create_qt(int &argc, char **argv, const QString &
332357
333358 //Set application properties
334359 application->setApplicationName(appName);
335- application->setOrganizationName("LoRd_MuldeR");
336- application->setOrganizationDomain("mulder.at.gg");
360+ application->setOrganizationDomain(appDomain);
361+ application->setOrganizationName(appAuthor);
337362 #if QT_VERSION < QT_VERSION_CHECK(5,0,0)
338- application->setEventFilter(qt_event_filter);
363+ application->setEventFilter(&Internal::NativeEventFilter::filterEvent);
339364 #else
340- qt_event_filter.reset(new Internal::NativeEventFilter);
341- application->installNativeEventFilter(qt_event_filter.data());
365+ application->installNativeEventFilter(Internal::NativeEventFilter::instance());
342366 #endif
343367
344368 //Check for supported image formats
@@ -348,7 +372,6 @@ QApplication *MUtils::Startup::create_qt(int &argc, char **argv, const QString &
348372 if(!supportedFormats.contains(g_imageformats[i]))
349373 {
350374 qFatal("Qt initialization error: QImageIOHandler for '%s' missing!", g_imageformats[i]);
351- MUTILS_DELETE(application);
352375 return NULL;
353376 }
354377 }
@@ -378,13 +401,12 @@ QApplication *MUtils::Startup::create_qt(int &argc, char **argv, const QString &
378401 messageBox.addButton("Ignore", QMessageBox::NoRole);
379402 if(messageBox.exec() == 0)
380403 {
381- MUTILS_DELETE(application);
382404 return NULL;
383405 }
384406 }
385407
386- //Qt created successfully
387- return application;
408+ //QApplication created successfully
409+ return application.take();
388410 }
389411
390412 ///////////////////////////////////////////////////////////////////////////////
Show on old repository browser