Implement ClassManager

Very similar to CallbackManager in its functioning.
This commit is contained in:
King_DuckZ 2022-05-14 17:06:25 +02:00
commit 258237cbf3
11 changed files with 218 additions and 30 deletions

View file

@ -22,6 +22,7 @@
#include <unordered_map>
#include <vector>
#include <string_view>
#include <type_traits>
namespace wren::detail {
class FullSignatureOwning;

View file

@ -0,0 +1,88 @@
/* 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 "strings_in_vector.hpp"
#include <unordered_map>
#include <string>
#include <type_traits>
namespace wren {
namespace detail {
class ClassNameBase {
public:
ClassNameBase (const std::string_view& module_name, const std::string_view& class_name);
ClassNameBase (const ClassNameBase& other) = default;
std::size_t module_hash() const noexcept { return m_module_hash; }
std::size_t class_hash() const noexcept { return m_class_name_hash; }
bool operator== (const ClassNameBase& other) const;
private:
std::size_t m_module_hash;
std::size_t m_class_name_hash;
};
class ClassNameOwning : StringsInVector<2>, public ClassNameBase {
public:
ClassNameOwning (const std::string_view& module_name, const std::string_view& class_name);
std::string_view module_name() const { return this->as_string_view<0>(); }
std::string_view class_name() const { return this->as_string_view<1>(); }
using StringsInVector<2>::operator<;
bool operator== (const ClassNameOwning& other) const;
};
class TempClassName : public ClassNameBase {
public:
TempClassName (std::string_view module_name, std::string_view class_name);
TempClassName (const ClassNameOwning& model);
bool operator== (const TempClassName& other) const;
private:
std::string_view m_module_name, m_class_name;
};
struct ClassNameEqual {
using is_transparent = std::true_type;
bool operator()(TempClassName left, TempClassName right) const;
};
struct ClassNameHash {
using is_transparent = std::true_type;
std::size_t operator()(TempClassName value) const;
};
} //namespace detail
class ClassManager {
typedef foreign_class_t(*make_foreign_class_t)();
typedef std::unordered_map<detail::ClassNameOwning, make_foreign_class_t, detail::ClassNameHash, detail::ClassNameEqual> ClassMap;
public:
ClassManager();
~ClassManager() noexcept;
void add_class_maker (std::string_view module_name, std::string_view class_name, make_foreign_class_t);
foreign_class_t make_class(std::string_view module_name, std::string_view class_name);
private:
ClassMap m_classes;
};
} //namespace wren

View file

@ -25,8 +25,9 @@ namespace wren {
class DefConfiguration : public Configuration {
public:
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 error_fn (VM*, ErrorType, wren_string_t module_name, 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);
static foreign_method_t foreign_method_fn (VM* vm, wren_string_t module_name, wren_string_t class_name, bool is_static, wren_string_t signature);
static foreign_class_t foreign_class_fn (wren::VM* vm, std::string_view module_name, std::string_view class_name);
};
} //namespace wren

View file

@ -36,14 +36,7 @@ namespace wren {
class VM;
class DynafuncMaker;
class CallbackManager;
typedef void(*foreign_method_t)(VM&);
typedef void(*finalizer_t)(void*);
struct foreign_class_t {
foreign_method_t allocate;
finalizer_t finalize;
};
class ClassManager;
enum class SlotType {
Bool, Num, Foreign, List, Null, String, Unknown, Map
@ -114,6 +107,7 @@ namespace wren {
void reset (Configuration* conf);
CallbackManager& callback_manager();
ClassManager& class_manager();
private:
struct LocalData;

View file

@ -15,13 +15,21 @@
* along with wrenpp. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <string_view>
#include <tuple>
namespace wren {
class VM;
typedef std::string_view wren_string_t;
typedef void(*foreign_method_t)(VM&);
typedef std::tuple<const char*, const char*> ModuleAndName;
typedef std::string_view wren_string_t;
typedef void(*finalizer_t)(void*);
typedef void(*foreign_method_t)(VM&);
struct foreign_class_t {
foreign_method_t allocate;
finalizer_t finalize;
};
} //namespace wren