OSDN Git Service

Some more improvements of MUtils::CPUFetaures code.
[mutilities/MUtilities.git] / src / Utils_Win32.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2016 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18 //
19 // http://www.gnu.org/licenses/lgpl-2.1.txt
20 //////////////////////////////////////////////////////////////////////////////////
21
22 #include "Utils_Win32.h"
23
24 //Win32 API
25 #ifndef _INC_WINDOWS
26 #define WIN32_LEAN_AND_MEAN 1
27 #include <Windows.h>
28 #endif //_INC_WINDOWS
29
30 //Qt
31 #include <QIcon>
32 #include <QPair>
33 #include <QReadWriteLock>
34 #include <QLibrary>
35 #include <QHash>
36
37 ///////////////////////////////////////////////////////////////////////////////
38 // QICON TO HICON
39 ///////////////////////////////////////////////////////////////////////////////
40
41 uintptr_t MUtils::Win32Utils::qicon_to_hicon(const QIcon &icon, const int w, const int h)
42 {
43         if(!icon.isNull())
44         {
45                 QPixmap pixmap = icon.pixmap(w, h);
46                 if(!pixmap.isNull())
47                 {
48                         return (uintptr_t) pixmap.toWinHICON();
49                 }
50         }
51         return NULL;
52 }
53
54 ///////////////////////////////////////////////////////////////////////////////
55 // RESOLVE FUNCTION
56 ///////////////////////////////////////////////////////////////////////////////
57
58 typedef QHash<QString, uintptr_t> FunctionMap;
59 typedef QPair<QSharedPointer<QLibrary>, FunctionMap> LibraryItem;
60
61 static QReadWriteLock              g_resolve_lock;
62 static QHash<QString, LibraryItem> g_resolve_libs;
63
64 const uintptr_t &MUtils::Win32Utils::resolve_helper(const QString &libraryName, const QString &functionName)
65 {
66         const QString libraryNameFolded = libraryName.toCaseFolded().trimmed();
67         const QString functionIdTrimmed = functionName.trimmed();
68
69         //Fuction already loaded?
70         QReadLocker rdLock(&g_resolve_lock);
71         if (g_resolve_libs.contains(libraryNameFolded))
72         {
73                 LibraryItem &lib = g_resolve_libs[libraryNameFolded];
74                 if (lib.second.contains(functionIdTrimmed))
75                 {
76                         return lib.second[functionIdTrimmed];
77                 }
78         }
79
80         //Accquire write access!
81         rdLock.unlock();
82         QWriteLocker wrLock(&g_resolve_lock);
83
84         //Load library
85         if (!g_resolve_libs.contains(libraryNameFolded))
86         {
87                 QSharedPointer<QLibrary> lib(new QLibrary(libraryNameFolded));
88                 if (!(lib->isLoaded() || lib->load()))
89                 {
90                         qWarning("Failed to load dynamic library: %s", MUTILS_UTF8(libraryNameFolded));
91                         lib.clear();
92                 }
93                 g_resolve_libs.insert(libraryNameFolded, qMakePair(lib, FunctionMap()));
94         }
95
96         //Is library available?
97         LibraryItem &lib = g_resolve_libs[libraryNameFolded];
98         if (lib.first.isNull() || (!lib.first->isLoaded()))
99         {
100                 static const uintptr_t null = NULL;
101                 return null; /*library unavailable*/
102         }
103
104         //Lookup the function
105         if (!lib.second.contains(functionIdTrimmed))
106         {
107                 void *const ptr = lib.first->resolve(functionIdTrimmed.toLatin1().constData());
108                 if (!ptr)
109                 {
110                         qWarning("Failed to resolve function: %s::%s", MUTILS_UTF8(libraryNameFolded), MUTILS_UTF8(functionIdTrimmed));
111                 }
112                 lib.second.insert(functionIdTrimmed, reinterpret_cast<uintptr_t>(ptr));
113         }
114
115         //Return function pointer
116         return lib.second[functionIdTrimmed];
117 }