diff options
-rw-r--r-- | scripts/HeaderOrder.txt | 1 | ||||
-rw-r--r-- | win32/HanjaDic.cxx | 14 | ||||
-rw-r--r-- | win32/PlatWin.cxx | 1 | ||||
-rw-r--r-- | win32/PlatWin.h | 31 | ||||
-rw-r--r-- | win32/ScintillaWin.cxx | 1 | ||||
-rw-r--r-- | win32/WinTypes.h | 59 | ||||
-rw-r--r-- | win32/deps.mak | 4 | ||||
-rw-r--r-- | win32/nmdeps.mak | 4 |
8 files changed, 69 insertions, 46 deletions
diff --git a/scripts/HeaderOrder.txt b/scripts/HeaderOrder.txt index a0954fcce..6ad1a0a7f 100644 --- a/scripts/HeaderOrder.txt +++ b/scripts/HeaderOrder.txt @@ -151,6 +151,7 @@ // Platform-specific headers // win32 +#include "WinTypes.h" #include "PlatWin.h" #include "HanjaDic.h" #include "ScintillaWin.h" diff --git a/win32/HanjaDic.cxx b/win32/HanjaDic.cxx index 04df43845..90ba6f698 100644 --- a/win32/HanjaDic.cxx +++ b/win32/HanjaDic.cxx @@ -14,23 +14,11 @@ #include <windows.h> #include <ole2.h> +#include "WinTypes.h" #include "HanjaDic.h" namespace Scintilla::Internal::HanjaDict { -struct UnknownReleaser { - // Called by unique_ptr to destroy/free the resource - template <class T> - void operator()(T *pUnknown) noexcept { - // same as ReleaseUnknown() in PlatWin.h - try { - pUnknown->Release(); - } catch (...) { - // IUnknown::Release must not throw, ignore if it does. - } - } -}; - struct BSTRDeleter { void operator()(BSTR bstr) const noexcept { SysFreeString(bstr); diff --git a/win32/PlatWin.cxx b/win32/PlatWin.cxx index aaa6d57d6..71c23eee7 100644 --- a/win32/PlatWin.cxx +++ b/win32/PlatWin.cxx @@ -55,6 +55,7 @@ #include "UniConversion.h" #include "DBCS.h" +#include "WinTypes.h" #include "PlatWin.h" #ifndef SPI_GETFONTSMOOTHINGCONTRAST diff --git a/win32/PlatWin.h b/win32/PlatWin.h index 3ee0d6bb7..68b5dd9ef 100644 --- a/win32/PlatWin.h +++ b/win32/PlatWin.h @@ -43,37 +43,6 @@ inline HWND HwndFromWindow(const Window &w) noexcept { void *PointerFromWindow(HWND hWnd) noexcept; void SetWindowPointer(HWND hWnd, void *ptr) noexcept; -/// Find a function in a DLL and convert to a function pointer. -/// This avoids undefined and conditionally defined behaviour. -template<typename T> -T DLLFunction(HMODULE hModule, LPCSTR lpProcName) noexcept { - if (!hModule) { - return nullptr; - } - FARPROC function = ::GetProcAddress(hModule, lpProcName); - static_assert(sizeof(T) == sizeof(function)); - T fp {}; - memcpy(&fp, &function, sizeof(T)); - return fp; -} - -// Release an IUnknown* and set to nullptr. -// While IUnknown::Release must be noexcept, it isn't marked as such so produces -// warnings which are avoided by the catch. -template <class T> -void ReleaseUnknown(T *&ppUnknown) noexcept { - if (ppUnknown) { - try { - ppUnknown->Release(); - } - catch (...) { - // Never occurs - } - ppUnknown = nullptr; - } -} - - UINT DpiForWindow(WindowID wid) noexcept; int SystemMetricsForDpi(int nIndex, UINT dpi) noexcept; diff --git a/win32/ScintillaWin.cxx b/win32/ScintillaWin.cxx index e89e93977..c80be52e3 100644 --- a/win32/ScintillaWin.cxx +++ b/win32/ScintillaWin.cxx @@ -93,6 +93,7 @@ #include "AutoComplete.h" #include "ScintillaBase.h" +#include "WinTypes.h" #include "PlatWin.h" #include "HanjaDic.h" #include "ScintillaWin.h" diff --git a/win32/WinTypes.h b/win32/WinTypes.h new file mode 100644 index 000000000..44a12f38b --- /dev/null +++ b/win32/WinTypes.h @@ -0,0 +1,59 @@ +// Scintilla source code edit control +/** @file WinTypes.h + ** Implement safe release of COM objects and access to functions in DLLs. + ** Header contains all implementation - there is no .cxx file. + **/ +// Copyright 2020-2021 by Neil Hodgson <neilh@scintilla.org> +// The License.txt file describes the conditions under which this software may be distributed. + +#ifndef WINTYPES_H +#define WINTYPES_H + +namespace Scintilla::Internal { + +// Release an IUnknown* and set to nullptr. +// While IUnknown::Release must be noexcept, it isn't marked as such so produces +// warnings which are avoided by the catch. +template <class T> +inline void ReleaseUnknown(T *&ppUnknown) noexcept { + if (ppUnknown) { + try { + ppUnknown->Release(); + } catch (...) { + // Never occurs + } + ppUnknown = nullptr; + } +} + +struct UnknownReleaser { + // Called by unique_ptr to destroy/free the resource + template <class T> + void operator()(T *pUnknown) noexcept { + // same as ReleaseUnknown() in PlatWin.h + try { + pUnknown->Release(); + } catch (...) { + // IUnknown::Release must not throw, ignore if it does. + } + } +}; + + +/// Find a function in a DLL and convert to a function pointer. +/// This avoids undefined and conditionally defined behaviour. +template<typename T> +inline T DLLFunction(HMODULE hModule, LPCSTR lpProcName) noexcept { + if (!hModule) { + return nullptr; + } + FARPROC function = ::GetProcAddress(hModule, lpProcName); + static_assert(sizeof(T) == sizeof(function)); + T fp {}; + memcpy(&fp, &function, sizeof(T)); + return fp; +} + +} + +#endif diff --git a/win32/deps.mak b/win32/deps.mak index 1cd14a730..61798b358 100644 --- a/win32/deps.mak +++ b/win32/deps.mak @@ -1,7 +1,7 @@ # Created by DepGen.py. To recreate, run DepGen.py. HanjaDic.o: \ HanjaDic.cxx \ - ../src/UniConversion.h \ + WinTypes.h \ HanjaDic.h PlatWin.o: \ PlatWin.cxx \ @@ -12,6 +12,7 @@ PlatWin.o: \ ../src/XPM.h \ ../src/UniConversion.h \ ../src/DBCS.h \ + WinTypes.h \ PlatWin.h ScintillaDLL.o: \ ScintillaDLL.cxx \ @@ -57,6 +58,7 @@ ScintillaWin.o: \ ../src/ElapsedPeriod.h \ ../src/AutoComplete.h \ ../src/ScintillaBase.h \ + WinTypes.h \ PlatWin.h \ HanjaDic.h \ ScintillaWin.h diff --git a/win32/nmdeps.mak b/win32/nmdeps.mak index f36ca21e2..d456d6319 100644 --- a/win32/nmdeps.mak +++ b/win32/nmdeps.mak @@ -1,7 +1,7 @@ # Created by DepGen.py. To recreate, run DepGen.py. $(DIR_O)/HanjaDic.obj: \ HanjaDic.cxx \ - ../src/UniConversion.h \ + WinTypes.h \ HanjaDic.h $(DIR_O)/PlatWin.obj: \ PlatWin.cxx \ @@ -12,6 +12,7 @@ $(DIR_O)/PlatWin.obj: \ ../src/XPM.h \ ../src/UniConversion.h \ ../src/DBCS.h \ + WinTypes.h \ PlatWin.h $(DIR_O)/ScintillaDLL.obj: \ ScintillaDLL.cxx \ @@ -57,6 +58,7 @@ $(DIR_O)/ScintillaWin.obj: \ ../src/ElapsedPeriod.h \ ../src/AutoComplete.h \ ../src/ScintillaBase.h \ + WinTypes.h \ PlatWin.h \ HanjaDic.h \ ScintillaWin.h |