Debian Codegraph @

tests/framework/util/dynamic_library_wrapper.h

raw
1 /*
2 * Copyright (c) 2025 The Khronos Group Inc.
3 * Copyright (c) 2025 Valve Corporation
4 * Copyright (c) 2025 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and/or associated documentation files (the "Materials"), to
8 * deal in the Materials without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Materials, and to permit persons to whom the Materials are
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice(s) and this permission notice shall be included in
14 * all copies or substantial portions of the Materials.
15 *
16 * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 *
20 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE
23 * USE OR OTHER DEALINGS IN THE MATERIALS.
24 *
25 */
26
27 #pragma once
28
29 #include <filesystem>
30
31 #include "test_defines.h"
32
33 #include "functions.h"
34
35 #if defined(_WIN32)
36 #include <direct.h>
37 #include <windows.h>
38 #include <strsafe.h>
39 #elif TESTING_COMMON_UNIX_PLATFORMS
40 #include <dirent.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <unistd.h>
44 #include <dlfcn.h>
45 #endif
46
47 struct LibraryWrapper {
48 explicit LibraryWrapper() noexcept;
49 explicit LibraryWrapper(std::filesystem::path const& lib_path) noexcept;
50 ~LibraryWrapper() noexcept;
51 LibraryWrapper(LibraryWrapper const& wrapper) = delete;
52 LibraryWrapper& operator=(LibraryWrapper const& wrapper) = delete;
53 LibraryWrapper(LibraryWrapper&& wrapper) noexcept;
54 LibraryWrapper& operator=(LibraryWrapper&& wrapper) noexcept;
55 FromVoidStarFunc get_symbol(const char* symbol_name) const;
56
57 std::filesystem::path const& get_path() const { return lib_path; }
58 explicit operator bool() const noexcept { return lib_handle != nullptr; }
59
60 private:
61 void close_library() noexcept;
62
63 #if defined(_WIN32)
64 HMODULE lib_handle = nullptr;
65 #elif TESTING_COMMON_UNIX_PLATFORMS
66 void* lib_handle = nullptr;
67 #else
68 #error "Unhandled platform in dynamic_library_wrapper.h!"
69 #endif
70 std::filesystem::path lib_path;
71 };