Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
shared_library.hpp
Go to the documentation of this file.
1
4#pragma once
5
6#include <aegis2d/core/export.hpp>
7
8#include <cstdint>
9#include <filesystem>
10#include <stdexcept>
11#include <string>
12#include <string_view>
13
14namespace aegis2d::core {
15
19inline constexpr std::uint32_t library_api_version = 1;
20
22inline constexpr std::string_view library_entry = "aegis2d_library_entry";
23
38 std::uint32_t api_version = 0;
40 const char* name = nullptr;
41};
42
45class AEGIS2D_CORE_EXPORT LibraryError : public std::runtime_error {
46public:
47 using std::runtime_error::runtime_error;
48};
49
54class AEGIS2D_CORE_EXPORT SharedLibrary {
55public:
60 explicit SharedLibrary(const std::filesystem::path& path);
63 SharedLibrary(const SharedLibrary&) = delete;
67 SharedLibrary(SharedLibrary&& other) noexcept;
72
76 [[nodiscard]] void* symbol(const std::string& name) const noexcept;
77
82 template <typename Function>
83 [[nodiscard]] Function* function(const std::string& name) const noexcept {
84 return reinterpret_cast<Function*>(symbol(name));
85 }
86
89 [[nodiscard]] const std::filesystem::path& path() const noexcept { return path_; }
90
91private:
92 void close() noexcept;
93
94 std::filesystem::path path_;
95 void* handle_ = nullptr;
96};
97
103 std::string name;
104};
105
115[[nodiscard]] AEGIS2D_CORE_EXPORT EngineLibrary
116load_library(const std::filesystem::path& path, std::uint32_t api_version = library_api_version);
117
118} // namespace aegis2d::core
A shared library could not be opened or is not one the engine can load.
Definition shared_library.hpp:45
A shared library opened at run time (.so, .dylib or .dll), closed when destroyed.
Definition shared_library.hpp:54
SharedLibrary(const std::filesystem::path &path)
Opens a library, with every symbol resolved now.
SharedLibrary(const SharedLibrary &)=delete
SharedLibrary(SharedLibrary &&other) noexcept
Takes the library over; other no longer holds one.
Function * function(const std::string &name) const noexcept
An exported function, with its type.
Definition shared_library.hpp:83
void * symbol(const std::string &name) const noexcept
Address of an exported symbol.
SharedLibrary & operator=(const SharedLibrary &)=delete
const std::filesystem::path & path() const noexcept
Path the library was opened from.
Definition shared_library.hpp:89
~SharedLibrary()
Closes the library.
SharedLibrary & operator=(SharedLibrary &&other) noexcept
Closes the library held, then takes other's over.
Logging, time, main loop, events and configuration shared by client and server.
Definition clock.hpp:11
EngineLibrary load_library(const std::filesystem::path &path, std::uint32_t api_version=library_api_version)
Opens an Aegis2D library and checks that the engine can use it.
constexpr std::string_view library_entry
Name of the C function every Aegis2D library exports (see LibraryInfo).
Definition shared_library.hpp:22
constexpr std::uint32_t library_api_version
Version of the interface between the engine and the libraries it loads.
Definition shared_library.hpp:19
Definition endpoint.hpp:85
An Aegis2D library, opened and checked by load_library().
Definition shared_library.hpp:99
std::string name
Its name, from its LibraryInfo; empty if it gives none.
Definition shared_library.hpp:103
SharedLibrary library
The opened library.
Definition shared_library.hpp:101
What an Aegis2D library tells about itself, from its C entry point.
Definition shared_library.hpp:36
std::uint32_t api_version
library_api_version the library was built with.
Definition shared_library.hpp:38
const char * name
Name of the library, a static string; may be null.
Definition shared_library.hpp:40