wrenpp/include/wrenpp/class_manager.hpp
King_DuckZ 9921e59a6b Add support for foreign constructor overload
Make vm_fun.hpp fun again by moving setters/getters
into a separate header.
Add new make_overloaded_foreign_class(), but code is still
untested and it's a work in progress at this point.
Interface will change.
2022-05-15 15:35:25 +02:00

89 lines
2.9 KiB
C++

/* 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>
#include <functional>
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 std::function<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;
ClassManager& 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