Implement CallbackManager

With it users don't have to provide a foreign_method_fn()
function anymore, it's become optional.
This commit is contained in:
King_DuckZ 2022-04-29 18:16:57 +02:00
commit fa0183a3bf
8 changed files with 272 additions and 22 deletions

View file

@ -0,0 +1,129 @@
/* Copyright 2020-2022, Michele Santullo
* This file is part of wrenpp.
*
* Wrenpp is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Wrenpp is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with wrenpp. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "wren_types.hpp"
#include <unordered_map>
#include <vector>
#include <string_view>
namespace wren::detail {
class FullSignatureOwning;
} //namespace wren::detail
namespace std {
template <>
struct hash<wren::detail::FullSignatureOwning> {
std::size_t operator() (const wren::detail::FullSignatureOwning& key) const;
};
};
namespace wren {
namespace detail {
class FullSignatureBase {
public:
FullSignatureBase (const FullSignatureBase& other) :
m_module_hash(other.m_module_hash),
m_class_name_hash(other.m_class_name_hash),
m_signature_hash(other.m_signature_hash)
{ }
std::size_t module_hash() const { return m_module_hash; }
std::size_t class_hash() const { return m_class_name_hash; }
std::size_t signature_hash() const { return m_signature_hash; }
protected:
FullSignatureBase (std::size_t module_hash_v, std::size_t class_hash_v, std::size_t signature_hash_v) :
m_module_hash(module_hash_v),
m_class_name_hash(class_hash_v),
m_signature_hash(signature_hash_v)
{ }
private:
std::size_t m_module_hash;
std::size_t m_class_name_hash;
std::size_t m_signature_hash;
};
class FullSignatureOwning : public FullSignatureBase {
public:
FullSignatureOwning (std::string_view module_name, std::string_view class_name, std::string_view signature);
std::string_view module_name() const { return make_string_view(m_module_range); }
std::string_view class_name() const { return make_string_view(m_class_range); }
std::string_view signature() const { return make_string_view(m_signature_range); }
private:
struct WordRange {
std::size_t start, length;
};
std::string_view make_string_view(WordRange range) const;
std::vector<char> m_full_name;
WordRange m_module_range;
WordRange m_class_range;
WordRange m_signature_range;
};
//Temp because it's only supposed to be used to store temporaries
//It will contain a dangling reference if the FullSignatureOwning object
//used to initialise an object of this class gets destroyed
class TempFullSignature : public FullSignatureBase {
public:
TempFullSignature (const FullSignatureOwning& mn);
TempFullSignature (std::string_view module_name, std::string_view class_name, std::string_view signature);
std::string_view module_name() const { return m_module_name; }
std::string_view class_name() const { return m_class_name; }
std::string_view signature() const { return m_signature; }
private:
std::string_view m_module_name;
std::string_view m_class_name;
std::string_view m_signature;
};
struct FullSignatureEqual {
using is_transparent = std::true_type;
bool operator()(TempFullSignature left, TempFullSignature right) const;
};
struct FullSignatureHash {
using is_transparent = std::true_type;
std::size_t operator()(TempFullSignature value) const;
};
} //namespace detail
class CallbackManager {
typedef std::unordered_map<detail::FullSignatureOwning, foreign_method_t, detail::FullSignatureHash, detail::FullSignatureEqual> MethodMap;
public:
CallbackManager();
~CallbackManager() noexcept;
void add_callback (bool is_static, std::string_view module_name, std::string_view class_name, std::string_view signature, foreign_method_t cb);
foreign_method_t callback (bool is_static, std::string_view module_name, std::string_view class_name, std::string_view signature) const;
private:
MethodMap* method_map (bool is_static) noexcept;
const MethodMap* method_map (bool is_static) const noexcept;
MethodMap m_static_methods;
MethodMap m_normal_methods;
};
} //namespace wren

View file

@ -18,7 +18,7 @@
#pragma once
#include "configuration.hpp"
#include "wrenpp/wren_types.hpp"
#include "wren_types.hpp"
#include "error_type.hpp"
namespace wren {
@ -27,5 +27,6 @@ namespace wren {
static void write_fn (VM*, wren_string_t text);
static void error_fn (VM*, ErrorType, wren_string_t module, int line, wren_string_t msg);
static void* reallocate_fn(void* ptr, std::size_t size);
static foreign_method_t foreign_method_fn (VM* vm, wren_string_t module, wren_string_t class_name, bool is_static, wren_string_t signature);
};
} //namespace wren

View file

@ -35,6 +35,7 @@ namespace wren {
class Configuration;
class VM;
class DynafuncMaker;
class CallbackManager;
typedef void(*foreign_method_t)(VM&);
typedef void(*finalizer_t)(void*);
@ -112,6 +113,8 @@ namespace wren {
std::size_t dynafunc_byte_size() const;
void reset (Configuration* conf);
CallbackManager& callback_manager();
private:
struct LocalData;