annotate DPF-Prymula-audioplugins/dpf/distrho/extra/LibraryUtils.hpp @ 3:84e66ea83026

DPF-Prymula-audioplugins-0.231015-2
author prymula <prymula76@outlook.com>
date Mon, 16 Oct 2023 21:53:34 +0200
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
1 /*
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
2 * DISTRHO Plugin Framework (DPF)
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
3 * Copyright (C) 2012-2021 Filipe Coelho <falktx@falktx.com>
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
4 *
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
5 * Permission to use, copy, modify, and/or distribute this software for any purpose with
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
6 * or without fee is hereby granted, provided that the above copyright notice and this
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
7 * permission notice appear in all copies.
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
8 *
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
10 * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
11 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
12 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
13 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
15 */
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
16
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
17 #ifndef DISTRHO_LIBRARY_UTILS_HPP_INCLUDED
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
18 #define DISTRHO_LIBRARY_UTILS_HPP_INCLUDED
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
19
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
20 #include "../DistrhoUtils.hpp"
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
21
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
22 #ifdef DISTRHO_OS_WINDOWS
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
23 # ifndef NOMINMAX
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
24 # define NOMINMAX
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
25 # endif
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
26 # include <winsock2.h>
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
27 # include <windows.h>
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
28 typedef HMODULE lib_t;
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
29 #else
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
30 # include <dlfcn.h>
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
31 typedef void* lib_t;
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
32 #endif
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
33
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
34 START_NAMESPACE_DISTRHO
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
35
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
36 // -----------------------------------------------------------------------
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
37 // library related calls
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
38
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
39 /*
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
40 * Open 'filename' library (must not be null).
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
41 * May return null, in which case "lib_error" has the error.
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
42 */
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
43 static inline
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
44 lib_t lib_open(const char* const filename) noexcept
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
45 {
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
46 DISTRHO_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', nullptr);
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
47
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
48 try {
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
49 #ifdef DISTRHO_OS_WINDOWS
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
50 return ::LoadLibraryA(filename);
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
51 #else
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
52 return ::dlopen(filename, RTLD_NOW|RTLD_LOCAL);
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
53 #endif
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
54 } DISTRHO_SAFE_EXCEPTION_RETURN("lib_open", nullptr);
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
55 }
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
56
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
57 /*
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
58 * Close a previously opened library (must not be null).
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
59 * If false is returned, "lib_error" has the error.
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
60 */
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
61 static inline
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
62 bool lib_close(const lib_t lib) noexcept
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
63 {
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
64 DISTRHO_SAFE_ASSERT_RETURN(lib != nullptr, false);
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
65
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
66 try {
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
67 #ifdef DISTRHO_OS_WINDOWS
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
68 return ::FreeLibrary(lib);
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
69 #else
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
70 return (::dlclose(lib) == 0);
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
71 #endif
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
72 } DISTRHO_SAFE_EXCEPTION_RETURN("lib_close", false);
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
73 }
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
74
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
75 /*
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
76 * Get a library symbol (must not be null).
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
77 * Returns null if the symbol is not found.
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
78 */
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
79 template<typename Func>
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
80 static inline
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
81 Func lib_symbol(const lib_t lib, const char* const symbol) noexcept
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
82 {
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
83 DISTRHO_SAFE_ASSERT_RETURN(lib != nullptr, nullptr);
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
84 DISTRHO_SAFE_ASSERT_RETURN(symbol != nullptr && symbol[0] != '\0', nullptr);
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
85
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
86 try {
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
87 #ifdef DISTRHO_OS_WINDOWS
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
88 # if defined(__GNUC__) && (__GNUC__ >= 9)
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
89 # pragma GCC diagnostic push
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
90 # pragma GCC diagnostic ignored "-Wcast-function-type"
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
91 # endif
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
92 return (Func)::GetProcAddress(lib, symbol);
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
93 # if defined(__GNUC__) && (__GNUC__ >= 9)
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
94 # pragma GCC diagnostic pop
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
95 # endif
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
96 #else
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
97 return (Func)(uintptr_t)::dlsym(lib, symbol);
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
98 #endif
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
99 } DISTRHO_SAFE_EXCEPTION_RETURN("lib_symbol", nullptr);
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
100 }
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
101
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
102 /*
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
103 * Return the last operation error ('filename' must not be null).
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
104 * May return null.
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
105 */
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
106 static inline
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
107 const char* lib_error(const char* const filename) noexcept
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
108 {
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
109 DISTRHO_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', nullptr);
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
110
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
111 #ifdef DISTRHO_OS_WINDOWS
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
112 static char libError[2048+1];
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
113 std::memset(libError, 0, sizeof(libError));
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
114
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
115 try {
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
116 const DWORD winErrorCode = ::GetLastError();
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
117 const int winErrorFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS;
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
118 LPVOID winErrorString;
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
119
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
120 ::FormatMessage(winErrorFlags, nullptr, winErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&winErrorString, 0, nullptr);
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
121
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
122 std::snprintf(libError, 2048, "%s: error code %li: %s", filename, winErrorCode, (const char*)winErrorString);
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
123 ::LocalFree(winErrorString);
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
124 } DISTRHO_SAFE_EXCEPTION("lib_error");
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
125
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
126 return (libError[0] != '\0') ? libError : nullptr;
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
127 #else
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
128 return ::dlerror();
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
129 #endif
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
130 }
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
131
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
132 // -----------------------------------------------------------------------
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
133
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
134 END_NAMESPACE_DISTRHO
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
135
84e66ea83026 DPF-Prymula-audioplugins-0.231015-2
prymula <prymula76@outlook.com>
parents:
diff changeset
136 #endif // DISTRHO_LIBRARY_UTILS_HPP_INCLUDED