| 1 |
/* |
| 2 |
* Copyright (C) 2023 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2023 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-runtime-lib |
| 6 |
* Created on: 24 апр. 2019 г. |
| 7 |
* |
| 8 |
* lsp-runtime-lib is free software: you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU Lesser General Public License as published by |
| 10 |
* the Free Software Foundation, either version 3 of the License, or |
| 11 |
* any later version. |
| 12 |
* |
| 13 |
* lsp-runtime-lib is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public License |
| 19 |
* along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#ifndef LSP_PLUG_IN_IPC_LIBRARY_H_ |
| 23 |
#define LSP_PLUG_IN_IPC_LIBRARY_H_ |
| 24 |
|
| 25 |
#include <lsp-plug.in/runtime/version.h> |
| 26 |
#include <lsp-plug.in/common/types.h> |
| 27 |
#include <lsp-plug.in/common/status.h> |
| 28 |
#include <lsp-plug.in/io/Path.h> |
| 29 |
|
| 30 |
#if defined(PLATFORM_WINDOWS) |
| 31 |
#include <libloaderapi.h> |
| 32 |
#else |
| 33 |
#include <dlfcn.h> |
| 34 |
#endif /* PLATFORM_WINDOWS */ |
| 35 |
|
| 36 |
namespace lsp |
| 37 |
{ |
| 38 |
namespace ipc |
| 39 |
{ |
| 40 |
/** |
| 41 |
* Class for loading dynamic libraries |
| 42 |
*/ |
| 43 |
class Library |
| 44 |
{ |
| 45 |
private: |
| 46 |
#ifdef PLATFORM_WINDOWS |
| 47 |
HMODULE hDlSym; |
| 48 |
#else |
| 49 |
void *hDlSym; |
| 50 |
#endif |
| 51 |
status_t nLastError; |
| 52 |
|
| 53 |
static int hTag; |
| 54 |
|
| 55 |
public: |
| 56 |
explicit Library(); |
| 57 |
Library(const Library &) = delete; |
| 58 |
Library(Library &&) = delete; |
| 59 |
~Library(); |
| 60 |
|
| 61 |
Library & operator = (const Library &) = delete; |
| 62 |
Library & operator = (Library &&) = delete; |
| 63 |
|
| 64 |
public: |
| 65 |
/** |
| 66 |
* Check that library is opened |
| 67 |
* @return true if library is opened |
| 68 |
*/ |
| 69 |
inline bool is_opened() const { return hDlSym != NULL; } |
| 70 |
|
| 71 |
/** |
| 72 |
* Return last error |
| 73 |
* @return last error |
| 74 |
*/ |
| 75 |
inline status_t last_error() const { return nLastError; } |
| 76 |
|
| 77 |
/** |
| 78 |
* Load library at specified path |
| 79 |
* @param path UTF-8 path |
| 80 |
* @param new_space load library into new namespace |
| 81 |
* @return status of operation |
| 82 |
*/ |
| 83 |
status_t open(const char *path, bool new_space = false); |
| 84 |
|
| 85 |
/** |
| 86 |
* Load library at specified path |
| 87 |
* @param path path |
| 88 |
* @param new_space load library into new namespace |
| 89 |
* @return status of operation |
| 90 |
*/ |
| 91 |
status_t open(const LSPString *path, bool new_space = false); |
| 92 |
|
| 93 |
/** |
| 94 |
* Load library at specified path |
| 95 |
* @param path UTF-8 path |
| 96 |
* @param new_space load library into new namespace |
| 97 |
* @return status of operation |
| 98 |
*/ |
| 99 |
status_t open(const io::Path *path, bool new_space = false); |
| 100 |
|
| 101 |
/** |
| 102 |
* Import symbol from library |
| 103 |
* @param name symbol name |
| 104 |
*/ |
| 105 |
void *import(const char *name); |
| 106 |
|
| 107 |
/** |
| 108 |
* Import symbol from library |
| 109 |
* @param name symbol name |
| 110 |
*/ |
| 111 |
void *import(const LSPString *name); |
| 112 |
|
| 113 |
/** |
| 114 |
* Unload loaded library |
| 115 |
* @return status of operation |
| 116 |
*/ |
| 117 |
status_t close(); |
| 118 |
|
| 119 |
/** |
| 120 |
* Swap contents with another library handle |
| 121 |
* @param dst target library handle to perform swap |
| 122 |
*/ |
| 123 |
void swap(Library *dst); |
| 124 |
|
| 125 |
/** |
| 126 |
* Get path to the module by the specified address |
| 127 |
* @param path variable to store path to the module |
| 128 |
* @param ptr specified address |
| 129 |
* @return status of operation |
| 130 |
*/ |
| 131 |
static status_t get_module_file(LSPString *path, const void *ptr); |
| 132 |
|
| 133 |
/** |
| 134 |
* Get path to the module by the specified address |
| 135 |
* @param path variable to store path to the module |
| 136 |
* @param ptr specified address |
| 137 |
* @return status of operation |
| 138 |
*/ |
| 139 |
static status_t get_module_file(io::Path *path, const void *ptr); |
| 140 |
|
| 141 |
/** |
| 142 |
* Get path to the current module |
| 143 |
* @param path variable to store path to the current module |
| 144 |
* @return status of operation |
| 145 |
*/ |
| 146 |
static inline status_t get_self_file(LSPString *path) { return get_module_file(path, &hTag); }; |
| 147 |
|
| 148 |
/** |
| 149 |
* Get path to the current module |
| 150 |
* @param path variable to store path to the current module |
| 151 |
* @return status of operation |
| 152 |
*/ |
| 153 |
static inline status_t get_self_file(io::Path *path) { return get_module_file(path, &hTag); }; |
| 154 |
|
| 155 |
/** |
| 156 |
* Check that library file name is valid |
| 157 |
* @param path UTF-8 path |
| 158 |
* @return true if library file name is valid |
| 159 |
*/ |
| 160 |
static bool valid_library_name(const char *path); |
| 161 |
|
| 162 |
/** |
| 163 |
* Check that library file name is valid |
| 164 |
* @param path UTF-8 path |
| 165 |
* @return true if library file name is valid |
| 166 |
*/ |
| 167 |
static bool valid_library_name(const LSPString *path); |
| 168 |
|
| 169 |
/** |
| 170 |
* Check that library file name is valid |
| 171 |
* @param path UTF-8 path |
| 172 |
* @return true if library file name is valid |
| 173 |
*/ |
| 174 |
static bool valid_library_name(const io::Path *path); |
| 175 |
}; |
| 176 |
|
| 177 |
} /* namespace ipc */ |
| 178 |
} /* namespace lsp */ |
| 179 |
|
| 180 |
#endif /* LSP_PLUG_IN_IPC_LIBRARY_H_ */ |