Air-Trap 1.0.0
A multiplayer R-Type clone game engine built with C++23 and ECS architecture
Loading...
Searching...
No Matches
DynamicLibrary.cpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2025
3** R-Type
4** File description:
5** DynamicLibrary.cpp, implementation of dynamic library handling
6*/
7
8/*
9** MIT License
10**
11** Copyright (c) 2025 Robin Toillon
12**
13** Permission is hereby granted, free of charge, to any person obtaining
14** a copy of this software and associated documentation files (the
15** "Software"), to deal in the Software without restriction, including
16** without limitation the rights to use, copy, modify, merge, publish,
17** distribute, sublicense, and/or sell copies of the Software, and to
18** permit persons to whom the Software is furnished to do so, subject to
19** the following conditions:
20**
21** The above copyright notice and this permission notice shall be
22** included in all copies or substantial portions of the Software.
23**
24** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
27** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
28** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
29** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
30** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31*/
32
42#include "RType/Logger.hpp"
44#include "LibraryBackend.hpp"
45
46namespace rtp::sys
47{
49 // Public API
51
52 DynamicLibrary::DynamicLibrary(void *handle) noexcept : _handle(handle)
53 {
54 }
55
57 {
58 if (this->_handle) [[likely]] {
59 auto result = impl::LibraryBackend::close(this->_handle);
60 if (!result.has_value()) {
61 rtp::log::warning("Failed to close dynamic library: {}",
62 result.error().message());
63 }
64 }
65 }
66
68 : _handle(other._handle)
69 {
70 other._handle = nullptr;
71 }
72
74 {
75 if (this != &other) {
76 if (this->_handle) [[unlikely]] {
77 auto result = impl::LibraryBackend::close(this->_handle);
78 if (!result.has_value()) {
79 rtp::log::warning("Failed to close dynamic library: {}",
80 result.error().message());
81 }
82 }
83 this->_handle = other._handle;
84 other._handle = nullptr;
85 }
86
87 return *this;
88 }
89
90 auto DynamicLibrary::getSymbolAddress(std::string_view name) const
91 -> std::expected<void *, rtp::Error>
92 {
93 return impl::LibraryBackend::getSymbol(this->_handle, name);
94 }
95}
Declaration of the DynamicLibrary class.
Declarations for platform-specific dynamic library handling.
Logger declaration with support for multiple log levels.
Represents a dynamically loaded library.
void * _handle
The handle to the dynamic library.
auto getSymbolAddress(std::string_view name) const -> std::expected< void *, rtp::Error >
Get the address of a symbol from the dynamic library.
DynamicLibrary & operator=(DynamicLibrary &&other) noexcept
~DynamicLibrary() noexcept
Destructor.
DynamicLibrary(void *handle) noexcept
Construct a DynamicLibrary with the given handle.
static auto getSymbol(void *handle, std::string_view name) -> std::expected< void *, rtp::Error >
Get a symbol from the dynamic library.
static auto close(void *handle) noexcept -> std::expected< void, rtp::Error >
Close the dynamic library handle.
void warning(LogFmt< std::type_identity_t< Args >... > fmt, Args &&...args) noexcept
Log a warning message.