Implement ClassManager
Very similar to CallbackManager in its functioning.
This commit is contained in:
parent
74670f4683
commit
258237cbf3
11 changed files with 218 additions and 30 deletions
97
src/class_manager.cpp
Normal file
97
src/class_manager.cpp
Normal file
|
@ -0,0 +1,97 @@
|
|||
/* 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/>.
|
||||
*/
|
||||
|
||||
#include "wrenpp/class_manager.hpp"
|
||||
|
||||
namespace wren {
|
||||
namespace detail {
|
||||
ClassNameBase::ClassNameBase (const std::string_view& module_name, const std::string_view& class_name) :
|
||||
m_module_hash(std::hash<std::string_view>{}(module_name)),
|
||||
m_class_name_hash(std::hash<std::string_view>{}(class_name))
|
||||
{
|
||||
}
|
||||
|
||||
bool ClassNameBase::operator==(const ClassNameBase& other) const {
|
||||
return m_module_hash == other.m_module_hash and
|
||||
m_class_name_hash == other.m_class_name_hash;
|
||||
}
|
||||
|
||||
ClassNameOwning::ClassNameOwning(const std::string_view& module_name, const std::string_view& class_name) :
|
||||
StringsInVector<2>(module_name, class_name),
|
||||
ClassNameBase(module_name, class_name)
|
||||
{
|
||||
}
|
||||
|
||||
bool ClassNameOwning::operator== (const ClassNameOwning& other) const {
|
||||
return
|
||||
*static_cast<const ClassNameBase*>(this) == static_cast<const ClassNameBase&>(other) or
|
||||
*static_cast<const StringsInVector<2>*>(this) == static_cast<const StringsInVector<2>&>(other);
|
||||
}
|
||||
|
||||
TempClassName::TempClassName(std::string_view module_name, std::string_view class_name) :
|
||||
ClassNameBase(module_name, class_name),
|
||||
m_module_name(module_name),
|
||||
m_class_name(class_name)
|
||||
{
|
||||
}
|
||||
|
||||
TempClassName::TempClassName (const ClassNameOwning& other) :
|
||||
ClassNameBase(other),
|
||||
m_module_name(other.module_name()),
|
||||
m_class_name(other.class_name())
|
||||
{
|
||||
}
|
||||
|
||||
bool TempClassName::operator== (const TempClassName& other) const {
|
||||
return *static_cast<const ClassNameBase*>(this) == static_cast<const ClassNameBase&>(other) and
|
||||
m_module_name == other.m_module_name and
|
||||
m_class_name == other.m_class_name;
|
||||
}
|
||||
|
||||
bool ClassNameEqual::operator()(TempClassName left, TempClassName right) const {
|
||||
return left == right;
|
||||
}
|
||||
|
||||
std::size_t ClassNameHash::operator()(TempClassName value) const {
|
||||
return value.module_hash() ^ (value.class_hash() << 1);
|
||||
}
|
||||
} //namespace detail
|
||||
|
||||
ClassManager::ClassManager() = default;
|
||||
ClassManager::~ClassManager() noexcept = default;
|
||||
|
||||
void ClassManager::add_class_maker (std::string_view module_name, std::string_view class_name, make_foreign_class_t maker) {
|
||||
using detail::TempClassName;
|
||||
using detail::ClassNameOwning;
|
||||
|
||||
auto it_found = m_classes.find(TempClassName{module_name, class_name});
|
||||
if (m_classes.cend() == it_found)
|
||||
m_classes.insert(it_found, std::make_pair(ClassNameOwning{module_name, class_name}, maker));
|
||||
else
|
||||
it_found->second = maker;
|
||||
}
|
||||
|
||||
foreign_class_t ClassManager::make_class (std::string_view module_name, std::string_view class_name) {
|
||||
using detail::TempClassName;
|
||||
|
||||
auto it_found = m_classes.find(TempClassName{module_name, class_name});
|
||||
if (m_classes.cend() != it_found)
|
||||
return (*it_found->second)();
|
||||
else
|
||||
return {nullptr, nullptr};
|
||||
}
|
||||
} //namespace wren
|
|
@ -17,6 +17,7 @@
|
|||
|
||||
#include "wrenpp/def_configuration.hpp"
|
||||
#include "wrenpp/callback_manager.hpp"
|
||||
#include "wrenpp/class_manager.hpp"
|
||||
#include "wrenpp/vm.hpp"
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
|
@ -68,7 +69,11 @@ namespace wren {
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
foreign_method_t DefConfiguration::foreign_method_fn (VM* vm, wren_string_t module, wren_string_t class_name, bool is_static, wren_string_t signature) {
|
||||
return vm->callback_manager().callback(is_static, module, class_name, signature);
|
||||
foreign_method_t DefConfiguration::foreign_method_fn (VM* vm, wren_string_t module_name, wren_string_t class_name, bool is_static, wren_string_t signature) {
|
||||
return vm->callback_manager().callback(is_static, module_name, class_name, signature);
|
||||
}
|
||||
|
||||
foreign_class_t DefConfiguration::foreign_class_fn (wren::VM* vm, std::string_view module_name, std::string_view class_name) {
|
||||
return vm->class_manager().make_class(module_name, class_name);
|
||||
}
|
||||
} //namespace wren
|
||||
|
|
|
@ -17,13 +17,11 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "wrenpp/wren_types.hpp"
|
||||
#include <vector>
|
||||
#include <cstddef>
|
||||
|
||||
namespace wren {
|
||||
class VM;
|
||||
typedef void(*foreign_method_t)(VM&);
|
||||
|
||||
class DynafuncMaker {
|
||||
public:
|
||||
DynafuncMaker();
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
#include "wrenpp/vm.hpp"
|
||||
#include "wrenpp/configuration.hpp"
|
||||
#include "wrenpp/callback_manager.hpp"
|
||||
#include "wrenpp/class_manager.hpp"
|
||||
#include "dynafunc_maker.hpp"
|
||||
#include <wren.hpp>
|
||||
#include <cassert>
|
||||
|
@ -154,6 +155,7 @@ namespace wren {
|
|||
}
|
||||
|
||||
CallbackManager callback_manager;
|
||||
ClassManager class_manager;
|
||||
detail::Callbacks callbacks;
|
||||
DynafuncMaker dynafunc;
|
||||
WrenVM* wvm;
|
||||
|
@ -322,6 +324,10 @@ namespace wren {
|
|||
return m_local->callback_manager;
|
||||
}
|
||||
|
||||
ClassManager& VM::class_manager() {
|
||||
return m_local->class_manager;
|
||||
}
|
||||
|
||||
void VM::ensure_slots (int num_slots) {
|
||||
wrenEnsureSlots(m_local->wvm, num_slots);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue