λ61
Loading...
Searching...
No Matches
NativeExtension.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2025 Tetex7
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18//
19// Created by tete on 05/05/2025.
20//
21#pragma once
22
23#ifndef L61_NATIVEEXTENSION_HPP
24#define L61_NATIVEEXTENSION_HPP
25#include <expected>
26#include <mutex>
27#include <stdexcept>
28#include <type_traits>
29
30#include "l61/defs.hpp"
34
36{
40class NativeExtension final : public Object
41{
42public:
46 static constexpr const char* entryPointSymbolName = "__l61_rt_ex_init__";
47
51 static constexpr const char* headerSymbolName = "__lex61_header__";
52
54 using ExtensionEntryPointPtr_t = std::add_pointer_t<ExtensionEntryPoint_t>;
55 using ExtensionEntryPointCall_t = std::function<ExtensionEntryPoint_t>;
56private:
57 std::string extensionPath;
58
65
69 mutable std::mutex soMutex;
70
76 [[nodiscard]] l61_rosetta_SharedLibrary_symbol_t blindSymbolLookup(const std::string& symStr) const;
77
78 void isGoodExtension() const;
79public:
87 static int safeExtensionLoad(const std::expected<NativeExtension, std::string>& extension, l61_api_extension_ptr api, bool required = true);
88
95 static std::expected<NativeExtension, std::string> extensionLookUp(const std::vector<std::string>& spaths, const std::string& exName);
96
100 [[nodiscard]] bool isValid() const;
101
105 explicit NativeExtension(const std::string& path);
106
110 [[nodiscard]]
112
116 [[nodiscard]]
117 const std::string& getExtensionPath() const;
118
122 std::string toString() const override;
123
124 void unload();
125
133 template<typename T>
134 std::expected<std::add_pointer_t<std::type_identity_t<T>>, std::string> extensionSymbolLookup(const std::string& symStr) const
135 {
136 try
137 {
138 //This is very spooky but trust me Bro it's this way, or it does not work
139 //And yes this does break my rule of self enforced strict aliasing
140 return reinterpret_cast<std::add_pointer_t<T>>(blindSymbolLookup(symStr));
141 }
142 catch (std::runtime_error& e)
143 {
144 return std::unexpected(e.what());
145 }
146 }
147
151 const lex61_header_t* getExtensionHeader() const;
152
154 {
155 return *getExtensionHeader();
156 }
157
158 ~NativeExtension() override;
161 NativeExtension(NativeExtension&& nativeExtension) noexcept;
162};
163}
164
165#endif //L61_NATIVEEXTENSION_HPP
void isGoodExtension() const
Definition NativeExtension.cpp:77
static int safeExtensionLoad(const std::expected< NativeExtension, std::string > &extension, l61_api_extension_ptr api, bool required=true)
Safely loads an extension.
Definition NativeExtension.cpp:34
std::expected< std::add_pointer_t< std::type_identity_t< T > >, std::string > extensionSymbolLookup(const std::string &symStr) const
A relatively safe way to look up symbols within an extension.
Definition NativeExtension.hpp:134
__inline const lex61_header_t & getExtensionHeaderAsRef() const
Definition NativeExtension.hpp:153
std::string toString() const override
Definition NativeExtension.cpp:129
NativeExtension(NativeExtension &)=delete
void unload()
Definition NativeExtension.cpp:135
static constexpr const char * entryPointSymbolName
The standard entry point for an extension usually defined by the extension runtime.
Definition NativeExtension.hpp:46
static constexpr const char * headerSymbolName
The symbol name for the extension header this must be self defined.
Definition NativeExtension.hpp:51
const std::string & getExtensionPath() const
Definition NativeExtension.cpp:111
~NativeExtension() override
Definition NativeExtension.cpp:144
l61_rosetta_SharedLibrary_handle_t soHandle
an OS dependent handle to a shared library
Definition NativeExtension.hpp:63
const lex61_header_t * getExtensionHeader() const
Definition NativeExtension.cpp:118
std::function< ExtensionEntryPoint_t > ExtensionEntryPointCall_t
Definition NativeExtension.hpp:55
NativeExtension(const std::string &path)
Definition NativeExtension.cpp:91
bool isValid() const
Definition NativeExtension.cpp:85
NativeExtension(const NativeExtension &)=delete
ExtensionEntryPointCall_t extensionEntryPointCall
Definition NativeExtension.hpp:64
std::add_pointer_t< ExtensionEntryPoint_t > ExtensionEntryPointPtr_t
Definition NativeExtension.hpp:54
std::string extensionPath
Definition NativeExtension.hpp:57
static std::expected< NativeExtension, std::string > extensionLookUp(const std::vector< std::string > &spaths, const std::string &exName)
A lookup system.
Definition NativeExtension.cpp:48
l61_rosetta_SharedLibrary_symbol_t blindSymbolLookup(const std::string &symStr) const
Horrifically unsafe and that's why it's private.
Definition NativeExtension.cpp:63
const ExtensionEntryPointCall_t & getExtensionEntryPointCall() const
Definition NativeExtension.cpp:104
std::mutex soMutex
A mutex for a slight safety.
Definition NativeExtension.hpp:69
int(l61_api_extension_ptr) ExtensionEntryPoint_t
Definition NativeExtension.hpp:53
Definition Object.hpp:47
void * l61_rosetta_SharedLibrary_handle_t
Definition loadSharedLibraryTypes.h:29
void(* l61_rosetta_SharedLibrary_symbol_t)(void)
Definition loadSharedLibraryTypes.h:32
Definition defs.hpp:137
l61_api_extension_t * l61_api_extension_ptr
Definition ExtensionHeaders.hpp:53
Definition ExtensionHeaders.hpp:47