mirror of
https://github.com/BlackMATov/meta.hpp.git
synced 2025-12-14 11:40:35 +07:00
first dirty proof of concept
This commit is contained in:
@@ -6,6 +6,14 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace meta_hpp
|
||||
{
|
||||
}
|
||||
#include "meta_fwd.hpp"
|
||||
|
||||
#include "meta_value.hpp"
|
||||
#include "meta_registry.hpp"
|
||||
|
||||
#include "meta_class.hpp"
|
||||
#include "meta_field.hpp"
|
||||
#include "meta_function.hpp"
|
||||
#include "meta_method.hpp"
|
||||
#include "meta_namespace.hpp"
|
||||
#include "meta_variable.hpp"
|
||||
|
||||
141
headers/meta.hpp/meta_class.hpp
Normal file
141
headers/meta.hpp/meta_class.hpp
Normal file
@@ -0,0 +1,141 @@
|
||||
/*******************************************************************************
|
||||
* This file is part of the "https://github.com/blackmatov/meta.hpp"
|
||||
* For conditions of distribution and use, see copyright notice in LICENSE.md
|
||||
* Copyright (C) 2021, by Matvey Cherevko (blackmatov@gmail.com)
|
||||
******************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "meta_fwd.hpp"
|
||||
|
||||
#include "meta_field.hpp"
|
||||
#include "meta_function.hpp"
|
||||
#include "meta_method.hpp"
|
||||
#include "meta_variable.hpp"
|
||||
|
||||
namespace meta_hpp
|
||||
{
|
||||
class class_info {
|
||||
public:
|
||||
class_info() = delete;
|
||||
|
||||
class_info(class_info&&) = default;
|
||||
class_info(const class_info&) = default;
|
||||
|
||||
class_info& operator=(class_info&&) = default;
|
||||
class_info& operator=(const class_info&) = default;
|
||||
|
||||
class_info(std::string id)
|
||||
: id_(std::move(id)) {}
|
||||
|
||||
const std::string& id() const noexcept {
|
||||
return id_;
|
||||
}
|
||||
|
||||
std::optional<class_info> get_class(std::string_view id) const {
|
||||
return detail::find_opt(classes_, id);
|
||||
}
|
||||
|
||||
std::optional<field_info> get_field(std::string_view id) const {
|
||||
return detail::find_opt(fields_, id);
|
||||
}
|
||||
|
||||
std::optional<function_info> get_function(std::string_view id) const {
|
||||
return detail::find_opt(functions_, id);
|
||||
}
|
||||
|
||||
std::optional<method_info> get_method(std::string_view id) const {
|
||||
return detail::find_opt(methods_, id);
|
||||
}
|
||||
|
||||
std::optional<variable_info> get_variable(std::string_view id) const {
|
||||
return detail::find_opt(variables_, id);
|
||||
}
|
||||
private:
|
||||
friend class namespace_info;
|
||||
template < typename Class > friend class class_;
|
||||
friend class namespace_;
|
||||
private:
|
||||
void merge_with_(const class_info& other) {
|
||||
detail::merge_with(classes_, other.classes_, &class_info::merge_with_);
|
||||
detail::merge_with(fields_, other.fields_, &field_info::merge_with_);
|
||||
detail::merge_with(functions_, other.functions_, &function_info::merge_with_);
|
||||
detail::merge_with(methods_, other.methods_, &method_info::merge_with_);
|
||||
detail::merge_with(variables_, other.variables_, &variable_info::merge_with_);
|
||||
}
|
||||
private:
|
||||
std::string id_;
|
||||
std::map<std::string, class_info, std::less<>> classes_;
|
||||
std::map<std::string, field_info, std::less<>> fields_;
|
||||
std::map<std::string, function_info, std::less<>> functions_;
|
||||
std::map<std::string, method_info, std::less<>> methods_;
|
||||
std::map<std::string, variable_info, std::less<>> variables_;
|
||||
};
|
||||
}
|
||||
|
||||
namespace meta_hpp
|
||||
{
|
||||
template < typename Class >
|
||||
class class_ {
|
||||
public:
|
||||
explicit class_(std::string id)
|
||||
: info_(std::move(id)) {}
|
||||
|
||||
const class_info& info() const noexcept {
|
||||
return info_;
|
||||
}
|
||||
|
||||
template < typename... Internals >
|
||||
class_& operator()(Internals&&...internals) {
|
||||
(add_(std::forward<Internals>(internals)), ...);
|
||||
return *this;
|
||||
}
|
||||
private:
|
||||
template < typename InternalClass >
|
||||
void add_(const class_<InternalClass>& internal) {
|
||||
detail::merge_with(
|
||||
info_.classes_,
|
||||
internal.info().id(),
|
||||
internal.info(),
|
||||
&class_info::merge_with_);
|
||||
}
|
||||
|
||||
template < auto InternalField >
|
||||
void add_(const field_<InternalField>& internal) {
|
||||
detail::merge_with(
|
||||
info_.fields_,
|
||||
internal.info().id(),
|
||||
internal.info(),
|
||||
&field_info::merge_with_);
|
||||
}
|
||||
|
||||
template < auto InternalFunction >
|
||||
void add_(const function_<InternalFunction>& internal) {
|
||||
detail::merge_with(
|
||||
info_.functions_,
|
||||
internal.info().id(),
|
||||
internal.info(),
|
||||
&function_info::merge_with_);
|
||||
}
|
||||
|
||||
template < auto InternalMethod >
|
||||
void add_(const method_<InternalMethod>& internal) {
|
||||
detail::merge_with(
|
||||
info_.methods_,
|
||||
internal.info().id(),
|
||||
internal.info(),
|
||||
&method_info::merge_with_);
|
||||
}
|
||||
|
||||
template < auto InternalVariable >
|
||||
void add_(const variable_<InternalVariable>& internal) {
|
||||
detail::merge_with(
|
||||
info_.variables_,
|
||||
internal.info().id(),
|
||||
internal.info(),
|
||||
&variable_info::merge_with_);
|
||||
}
|
||||
private:
|
||||
class_info info_;
|
||||
};
|
||||
}
|
||||
120
headers/meta.hpp/meta_field.hpp
Normal file
120
headers/meta.hpp/meta_field.hpp
Normal file
@@ -0,0 +1,120 @@
|
||||
/*******************************************************************************
|
||||
* This file is part of the "https://github.com/blackmatov/meta.hpp"
|
||||
* For conditions of distribution and use, see copyright notice in LICENSE.md
|
||||
* Copyright (C) 2021, by Matvey Cherevko (blackmatov@gmail.com)
|
||||
******************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "meta_fwd.hpp"
|
||||
|
||||
#include "meta_value.hpp"
|
||||
|
||||
namespace meta_hpp::field_detail
|
||||
{
|
||||
template < typename Field >
|
||||
struct field_traits;
|
||||
|
||||
template < typename T, typename Base >
|
||||
struct field_traits<T Base::*> {
|
||||
static constexpr bool is_const = false;
|
||||
using value_type = T;
|
||||
using instance_type = Base;
|
||||
};
|
||||
|
||||
template < typename T, typename Base >
|
||||
struct field_traits<const T Base::*> {
|
||||
static constexpr bool is_const = true;
|
||||
using value_type = T;
|
||||
using instance_type = Base;
|
||||
};
|
||||
|
||||
template < auto Field >
|
||||
value getter(const void* instance) {
|
||||
using ft = field_traits<decltype(Field)>;
|
||||
using value_type = typename ft::value_type;
|
||||
using instance_type = typename ft::instance_type;
|
||||
|
||||
auto instance_ptr = static_cast<const instance_type*>(instance);
|
||||
value_type typed_value = std::invoke(Field, *instance_ptr);
|
||||
|
||||
return value{std::move(typed_value)};
|
||||
}
|
||||
|
||||
template < auto Field >
|
||||
void setter(void* instance, value value) {
|
||||
using ft = field_traits<decltype(Field)>;
|
||||
using value_type = typename ft::value_type;
|
||||
using instance_type = typename ft::instance_type;
|
||||
|
||||
if constexpr ( !ft::is_const ) {
|
||||
auto instance_ptr = static_cast<instance_type*>(instance);
|
||||
std::invoke(Field, *instance_ptr) = value.cast<value_type>();
|
||||
} else {
|
||||
throw std::logic_error("an attempt to change a constant field");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
namespace meta_hpp
|
||||
{
|
||||
class field_info {
|
||||
public:
|
||||
field_info() = delete;
|
||||
|
||||
field_info(field_info&&) = default;
|
||||
field_info(const field_info&) = default;
|
||||
|
||||
field_info& operator=(field_info&&) = default;
|
||||
field_info& operator=(const field_info&) = default;
|
||||
|
||||
field_info(std::string id)
|
||||
: id_(std::move(id)) {}
|
||||
|
||||
const std::string& id() const noexcept {
|
||||
return id_;
|
||||
}
|
||||
public:
|
||||
value get(const void* instance) const {
|
||||
return getter_(instance);
|
||||
}
|
||||
|
||||
template < typename Value >
|
||||
void set(void* instance, Value&& value) const {
|
||||
return setter_(instance, std::forward<Value>(value));
|
||||
}
|
||||
private:
|
||||
friend class class_info;
|
||||
template < typename Class > friend class class_;
|
||||
template < auto Field > friend class field_;
|
||||
private:
|
||||
void merge_with_(const field_info& other) {
|
||||
(void)other;
|
||||
}
|
||||
private:
|
||||
std::string id_;
|
||||
value(*getter_)(const void*);
|
||||
void(*setter_)(void*, value);
|
||||
};
|
||||
}
|
||||
|
||||
namespace meta_hpp
|
||||
{
|
||||
template < auto Field >
|
||||
class field_ {
|
||||
public:
|
||||
static_assert(std::is_member_object_pointer_v<decltype(Field)>);
|
||||
|
||||
explicit field_(std::string id)
|
||||
: info_(std::move(id)) {
|
||||
info_.getter_ = &field_detail::getter<Field>;
|
||||
info_.setter_ = &field_detail::setter<Field>;
|
||||
}
|
||||
|
||||
const field_info& info() const noexcept {
|
||||
return info_;
|
||||
}
|
||||
private:
|
||||
field_info info_;
|
||||
};
|
||||
}
|
||||
125
headers/meta.hpp/meta_function.hpp
Normal file
125
headers/meta.hpp/meta_function.hpp
Normal file
@@ -0,0 +1,125 @@
|
||||
/*******************************************************************************
|
||||
* This file is part of the "https://github.com/blackmatov/meta.hpp"
|
||||
* For conditions of distribution and use, see copyright notice in LICENSE.md
|
||||
* Copyright (C) 2021, by Matvey Cherevko (blackmatov@gmail.com)
|
||||
******************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "meta_fwd.hpp"
|
||||
|
||||
#include "meta_value.hpp"
|
||||
|
||||
namespace meta_hpp::function_detail
|
||||
{
|
||||
template < typename Function >
|
||||
struct function_traits;
|
||||
|
||||
template < typename R, typename... Args >
|
||||
struct function_traits<R(*)(Args...)> {
|
||||
static constexpr std::size_t arity = sizeof...(Args);
|
||||
using return_type = R;
|
||||
using argument_types = std::tuple<Args...>;
|
||||
};
|
||||
|
||||
template < typename R, typename... Args >
|
||||
struct function_traits<R(*)(Args...) noexcept>
|
||||
: function_traits<R(*)(Args...)> {};
|
||||
|
||||
template < auto Function, std::size_t... Is >
|
||||
value invoke(value* args, std::index_sequence<Is...>) {
|
||||
using ft = function_traits<decltype(Function)>;
|
||||
using return_type = typename ft::return_type;
|
||||
using argument_types = typename ft::argument_types;
|
||||
|
||||
auto typed_arguments = std::make_tuple(
|
||||
(args + Is)->try_cast<std::tuple_element_t<Is, argument_types>>()...);
|
||||
|
||||
if ( !(std::get<Is>(typed_arguments) && ...) ) {
|
||||
throw std::logic_error("an attempt to call a function with incorrect argument types");
|
||||
}
|
||||
|
||||
if constexpr ( std::is_void_v<return_type> ) {
|
||||
std::invoke(Function,
|
||||
*std::get<Is>(typed_arguments)...);
|
||||
return value{};
|
||||
} else {
|
||||
return_type return_value = std::invoke(Function,
|
||||
*std::get<Is>(typed_arguments)...);
|
||||
return value{std::move(return_value)};
|
||||
}
|
||||
}
|
||||
|
||||
template < auto Function >
|
||||
value invoke(value* args, std::size_t arg_count) {
|
||||
using ft = function_traits<decltype(Function)>;
|
||||
|
||||
if ( arg_count != ft::arity ) {
|
||||
throw std::logic_error("an attempt to call a function with an incorrect arity");
|
||||
}
|
||||
|
||||
return invoke<Function>(args, std::make_index_sequence<ft::arity>());
|
||||
}
|
||||
}
|
||||
|
||||
namespace meta_hpp
|
||||
{
|
||||
class function_info {
|
||||
public:
|
||||
function_info() = delete;
|
||||
|
||||
function_info(function_info&&) = default;
|
||||
function_info(const function_info&) = default;
|
||||
|
||||
function_info& operator=(function_info&&) = default;
|
||||
function_info& operator=(const function_info&) = default;
|
||||
|
||||
function_info(std::string id)
|
||||
: id_(std::move(id)) {}
|
||||
|
||||
const std::string& id() const noexcept {
|
||||
return id_;
|
||||
}
|
||||
public:
|
||||
template < typename... Args >
|
||||
value invoke(Args&&... args) const {
|
||||
std::array<value, sizeof...(Args)> vargs{{std::forward<Args>(args)...}};
|
||||
return invoke_(vargs.data(), vargs.size());
|
||||
}
|
||||
private:
|
||||
friend class class_info;
|
||||
friend class namespace_info;
|
||||
|
||||
template < typename Class > friend class class_;
|
||||
friend class namespace_;
|
||||
|
||||
template < auto Function > friend class function_;
|
||||
private:
|
||||
void merge_with_(const function_info& other) {
|
||||
(void)other;
|
||||
}
|
||||
private:
|
||||
std::string id_;
|
||||
value(*invoke_)(value*, std::size_t);
|
||||
};
|
||||
}
|
||||
|
||||
namespace meta_hpp
|
||||
{
|
||||
template < auto Function >
|
||||
class function_ {
|
||||
public:
|
||||
static_assert(std::is_function_v<std::remove_pointer_t<decltype(Function)>>);
|
||||
|
||||
explicit function_(std::string id)
|
||||
: info_(std::move(id)) {
|
||||
info_.invoke_ = &function_detail::invoke<Function>;
|
||||
}
|
||||
|
||||
const function_info& info() const noexcept {
|
||||
return info_;
|
||||
}
|
||||
private:
|
||||
function_info info_;
|
||||
};
|
||||
}
|
||||
99
headers/meta.hpp/meta_fwd.hpp
Normal file
99
headers/meta.hpp/meta_fwd.hpp
Normal file
@@ -0,0 +1,99 @@
|
||||
/*******************************************************************************
|
||||
* This file is part of the "https://github.com/blackmatov/meta.hpp"
|
||||
* For conditions of distribution and use, see copyright notice in LICENSE.md
|
||||
* Copyright (C) 2021, by Matvey Cherevko (blackmatov@gmail.com)
|
||||
******************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <any>
|
||||
#include <array>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <optional>
|
||||
#include <stdexcept>
|
||||
#include <string_view>
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
namespace meta_hpp
|
||||
{
|
||||
class value;
|
||||
class registry;
|
||||
|
||||
class class_info;
|
||||
class field_info;
|
||||
class function_info;
|
||||
class method_info;
|
||||
class namespace_info;
|
||||
class variable_info;
|
||||
|
||||
template < typename Class >
|
||||
class class_;
|
||||
|
||||
template < auto Field >
|
||||
class field_;
|
||||
|
||||
template < auto Function >
|
||||
class function_;
|
||||
|
||||
template < auto Method >
|
||||
class method_;
|
||||
|
||||
class namespace_;
|
||||
|
||||
template < auto Variable >
|
||||
class variable_;
|
||||
}
|
||||
|
||||
namespace meta_hpp
|
||||
{
|
||||
template < typename Signature >
|
||||
constexpr auto select(Signature* f) noexcept {
|
||||
return f;
|
||||
}
|
||||
|
||||
template < typename Signature, typename Base >
|
||||
constexpr auto select(Signature Base::*f) noexcept {
|
||||
return f;
|
||||
}
|
||||
|
||||
template < typename R, typename Base, typename... Args >
|
||||
constexpr auto select_const(R (Base::*f)(Args...) const) noexcept {
|
||||
return f;
|
||||
}
|
||||
|
||||
template < typename R, typename Base, typename... Args >
|
||||
constexpr auto select_non_const(R(Base::*f)(Args...)) noexcept {
|
||||
return f;
|
||||
}
|
||||
}
|
||||
|
||||
namespace meta_hpp::detail
|
||||
{
|
||||
template < typename K, typename V, typename C, typename K2 >
|
||||
std::optional<V> find_opt(const std::map<K, V, C>& src, K2&& key) {
|
||||
if ( auto iter = src.find(key); iter != src.end() ) {
|
||||
return iter->second;
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
template < typename K, typename V, typename C, typename K2, typename V2, typename F >
|
||||
void merge_with(std::map<K, V, C>& dst, K2&& key, V2&& value, F&& f) {
|
||||
if ( auto iter = dst.find(key); iter != dst.end() ) {
|
||||
std::invoke(std::forward<F>(f), iter->second, std::forward<V2>(value));
|
||||
} else {
|
||||
dst.emplace(std::forward<K2>(key), std::forward<V2>(value));
|
||||
}
|
||||
}
|
||||
|
||||
template < typename K, typename V, typename C, typename F >
|
||||
void merge_with(std::map<K, V, C>& dst, const std::map<K, V, C>& src, F&& f) {
|
||||
for ( auto [key, value] : src ) {
|
||||
merge_with(dst, key, value, f);
|
||||
}
|
||||
}
|
||||
}
|
||||
186
headers/meta.hpp/meta_method.hpp
Normal file
186
headers/meta.hpp/meta_method.hpp
Normal file
@@ -0,0 +1,186 @@
|
||||
/*******************************************************************************
|
||||
* This file is part of the "https://github.com/blackmatov/meta.hpp"
|
||||
* For conditions of distribution and use, see copyright notice in LICENSE.md
|
||||
* Copyright (C) 2021, by Matvey Cherevko (blackmatov@gmail.com)
|
||||
******************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "meta_fwd.hpp"
|
||||
|
||||
#include "meta_value.hpp"
|
||||
|
||||
namespace meta_hpp::method_detail
|
||||
{
|
||||
template < typename Method >
|
||||
struct method_traits;
|
||||
|
||||
template < typename R, typename Base, typename... Args >
|
||||
struct method_traits<R(Base::*)(Args...)> {
|
||||
static constexpr bool is_const = false;
|
||||
static constexpr std::size_t arity = sizeof...(Args);
|
||||
using return_type = R;
|
||||
using instance_type = Base;
|
||||
using argument_types = std::tuple<Args...>;
|
||||
};
|
||||
|
||||
template < typename R, typename Base, typename... Args >
|
||||
struct method_traits<R(Base::*)(Args...) const>
|
||||
: method_traits<R(Base::*)(Args...)> {
|
||||
static constexpr bool is_const = true;
|
||||
};
|
||||
|
||||
template < typename R, typename Base, typename... Args >
|
||||
struct method_traits<R(Base::*)(Args...) noexcept>
|
||||
: method_traits<R(Base::*)(Args...)> {};
|
||||
|
||||
template < typename R, typename Base, typename... Args >
|
||||
struct method_traits<R(Base::*)(Args...) const noexcept>
|
||||
: method_traits<R(Base::*)(Args...) const> {};
|
||||
|
||||
template < auto Method, std::size_t... Is >
|
||||
value invoke(void* instance, value* args, std::index_sequence<Is...>) {
|
||||
using mt = method_traits<decltype(Method)>;
|
||||
using return_type = typename mt::return_type;
|
||||
using instance_type = typename mt::instance_type;
|
||||
using argument_types = typename mt::argument_types;
|
||||
|
||||
auto typed_arguments = std::make_tuple(
|
||||
(args + Is)->try_cast<std::tuple_element_t<Is, argument_types>>()...);
|
||||
|
||||
if ( !(std::get<Is>(typed_arguments) && ...) ) {
|
||||
throw std::logic_error("an attempt to call a method with incorrect argument types");
|
||||
}
|
||||
|
||||
if constexpr ( std::is_void_v<return_type> ) {
|
||||
std::invoke(Method,
|
||||
std::ref(*static_cast<instance_type*>(instance)),
|
||||
*std::get<Is>(typed_arguments)...);
|
||||
return value{};
|
||||
} else {
|
||||
return_type return_value = std::invoke(Method,
|
||||
std::ref(*static_cast<instance_type*>(instance)),
|
||||
*std::get<Is>(typed_arguments)...);
|
||||
return value{std::move(return_value)};
|
||||
}
|
||||
}
|
||||
|
||||
template < auto Method >
|
||||
value invoke(void* instance, value* args, std::size_t arg_count) {
|
||||
using mt = method_traits<decltype(Method)>;
|
||||
|
||||
if ( arg_count != mt::arity ) {
|
||||
throw std::logic_error("an attempt to call a method with an incorrect arity");
|
||||
}
|
||||
|
||||
return invoke<Method>(instance, args, std::make_index_sequence<mt::arity>());
|
||||
}
|
||||
|
||||
template < auto Method, std::size_t... Is >
|
||||
value cinvoke(const void* instance, value* args, std::index_sequence<Is...>) {
|
||||
using mt = method_traits<decltype(Method)>;
|
||||
using return_type = typename mt::return_type;
|
||||
using instance_type = typename mt::instance_type;
|
||||
using argument_types = typename mt::argument_types;
|
||||
|
||||
auto typed_arguments = std::make_tuple(
|
||||
(args + Is)->try_cast<std::tuple_element_t<Is, argument_types>>()...);
|
||||
|
||||
if ( !(std::get<Is>(typed_arguments) && ...) ) {
|
||||
throw std::logic_error("an attempt to call a method with incorrect argument types");
|
||||
}
|
||||
|
||||
if constexpr ( mt::is_const ) {
|
||||
if constexpr ( std::is_void_v<return_type> ) {
|
||||
std::invoke(Method,
|
||||
std::ref(*static_cast<const instance_type*>(instance)),
|
||||
*std::get<Is>(typed_arguments)...);
|
||||
return value{};
|
||||
} else {
|
||||
return_type return_value = std::invoke(Method,
|
||||
std::ref(*static_cast<const instance_type*>(instance)),
|
||||
*std::get<Is>(typed_arguments)...);
|
||||
return value{std::move(return_value)};
|
||||
}
|
||||
} else {
|
||||
throw std::logic_error("an attempt to call a non-constant method by constant instance");
|
||||
}
|
||||
}
|
||||
|
||||
template < auto Method >
|
||||
value cinvoke(const void* instance, value* args, std::size_t arg_count) {
|
||||
using mt = method_traits<decltype(Method)>;
|
||||
|
||||
if ( arg_count != mt::arity ) {
|
||||
throw std::logic_error("an attempt to call a method with a different arity");
|
||||
}
|
||||
|
||||
return cinvoke<Method>(instance, args, std::make_index_sequence<mt::arity>());
|
||||
}
|
||||
}
|
||||
|
||||
namespace meta_hpp
|
||||
{
|
||||
class method_info {
|
||||
public:
|
||||
method_info() = delete;
|
||||
|
||||
method_info(method_info&&) = default;
|
||||
method_info(const method_info&) = default;
|
||||
|
||||
method_info& operator=(method_info&&) = default;
|
||||
method_info& operator=(const method_info&) = default;
|
||||
|
||||
method_info(std::string id)
|
||||
: id_(std::move(id)) {}
|
||||
|
||||
const std::string& id() const noexcept {
|
||||
return id_;
|
||||
}
|
||||
public:
|
||||
template < typename... Args >
|
||||
value invoke(void* instance, Args&&... args) const {
|
||||
std::array<value, sizeof...(Args)> vargs{{std::forward<Args>(args)...}};
|
||||
return invoke_(instance, vargs.data(), vargs.size());
|
||||
}
|
||||
|
||||
template < typename... Args >
|
||||
value invoke(const void* instance, Args&&... args) const {
|
||||
std::array<value, sizeof...(Args)> vargs{{std::forward<Args>(args)...}};
|
||||
return cinvoke_(instance, vargs.data(), vargs.size());
|
||||
}
|
||||
private:
|
||||
friend class class_info;
|
||||
template < typename Class > friend class class_;
|
||||
template < auto Method > friend class method_;
|
||||
private:
|
||||
void merge_with_(const method_info& other) {
|
||||
(void)other;
|
||||
}
|
||||
private:
|
||||
std::string id_;
|
||||
value(*invoke_)(void*, value*, std::size_t);
|
||||
value(*cinvoke_)(const void*, value*, std::size_t);
|
||||
};
|
||||
}
|
||||
|
||||
namespace meta_hpp
|
||||
{
|
||||
template < auto Method >
|
||||
class method_ {
|
||||
public:
|
||||
static_assert(std::is_member_function_pointer_v<decltype(Method)>);
|
||||
|
||||
explicit method_(std::string id)
|
||||
: info_(std::move(id)) {
|
||||
info_.invoke_ = &method_detail::invoke<Method>;
|
||||
info_.cinvoke_ = &method_detail::cinvoke<Method>;
|
||||
}
|
||||
|
||||
const method_info& info() const noexcept {
|
||||
return info_;
|
||||
}
|
||||
private:
|
||||
method_info info_;
|
||||
};
|
||||
}
|
||||
121
headers/meta.hpp/meta_namespace.hpp
Normal file
121
headers/meta.hpp/meta_namespace.hpp
Normal file
@@ -0,0 +1,121 @@
|
||||
/*******************************************************************************
|
||||
* This file is part of the "https://github.com/blackmatov/meta.hpp"
|
||||
* For conditions of distribution and use, see copyright notice in LICENSE.md
|
||||
* Copyright (C) 2021, by Matvey Cherevko (blackmatov@gmail.com)
|
||||
******************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "meta_fwd.hpp"
|
||||
|
||||
#include "meta_class.hpp"
|
||||
#include "meta_function.hpp"
|
||||
#include "meta_variable.hpp"
|
||||
|
||||
namespace meta_hpp
|
||||
{
|
||||
class namespace_info {
|
||||
public:
|
||||
namespace_info() = delete;
|
||||
|
||||
namespace_info(namespace_info&&) = default;
|
||||
namespace_info(const namespace_info&) = default;
|
||||
|
||||
namespace_info& operator=(namespace_info&&) = default;
|
||||
namespace_info& operator=(const namespace_info&) = default;
|
||||
|
||||
namespace_info(std::string id)
|
||||
: id_(std::move(id)) {}
|
||||
|
||||
const std::string& id() const noexcept {
|
||||
return id_;
|
||||
}
|
||||
|
||||
std::optional<class_info> get_class(std::string_view id) const {
|
||||
return detail::find_opt(classes_, id);
|
||||
}
|
||||
|
||||
std::optional<function_info> get_function(std::string_view id) const {
|
||||
return detail::find_opt(functions_, id);
|
||||
}
|
||||
|
||||
std::optional<namespace_info> get_namespace(std::string_view id) const {
|
||||
return detail::find_opt(namespaces_, id);
|
||||
}
|
||||
|
||||
std::optional<variable_info> get_variable(std::string_view id) const {
|
||||
return detail::find_opt(variables_, id);
|
||||
}
|
||||
private:
|
||||
friend class namespace_;
|
||||
private:
|
||||
void merge_with_(const namespace_info& other) {
|
||||
detail::merge_with(classes_, other.classes_, &class_info::merge_with_);
|
||||
detail::merge_with(functions_, other.functions_, &function_info::merge_with_);
|
||||
detail::merge_with(namespaces_, other.namespaces_, &namespace_info::merge_with_);
|
||||
detail::merge_with(variables_, other.variables_, &variable_info::merge_with_);
|
||||
}
|
||||
private:
|
||||
std::string id_;
|
||||
std::map<std::string, class_info, std::less<>> classes_;
|
||||
std::map<std::string, function_info, std::less<>> functions_;
|
||||
std::map<std::string, namespace_info, std::less<>> namespaces_;
|
||||
std::map<std::string, variable_info, std::less<>> variables_;
|
||||
};
|
||||
}
|
||||
|
||||
namespace meta_hpp
|
||||
{
|
||||
class namespace_ {
|
||||
public:
|
||||
explicit namespace_(std::string id)
|
||||
: info_(std::move(id)) {}
|
||||
|
||||
const namespace_info& info() const noexcept {
|
||||
return info_;
|
||||
}
|
||||
|
||||
template < typename... Internals >
|
||||
namespace_& operator()(Internals&&...internals) {
|
||||
(add_(std::forward<Internals>(internals)), ...);
|
||||
return *this;
|
||||
}
|
||||
private:
|
||||
template < typename InternalClass >
|
||||
void add_(const class_<InternalClass>& internal) {
|
||||
detail::merge_with(
|
||||
info_.classes_,
|
||||
internal.info().id(),
|
||||
internal.info(),
|
||||
&class_info::merge_with_);
|
||||
}
|
||||
|
||||
template < auto InternalFunction >
|
||||
void add_(const function_<InternalFunction>& internal) {
|
||||
detail::merge_with(
|
||||
info_.functions_,
|
||||
internal.info().id(),
|
||||
internal.info(),
|
||||
&function_info::merge_with_);
|
||||
}
|
||||
|
||||
void add_(const namespace_& internal) {
|
||||
detail::merge_with(
|
||||
info_.namespaces_,
|
||||
internal.info().id(),
|
||||
internal.info(),
|
||||
&namespace_info::merge_with_);
|
||||
}
|
||||
|
||||
template < auto Internalvariable >
|
||||
void add_(const variable_<Internalvariable>& internal) {
|
||||
detail::merge_with(
|
||||
info_.variables_,
|
||||
internal.info().id(),
|
||||
internal.info(),
|
||||
&variable_info::merge_with_);
|
||||
}
|
||||
private:
|
||||
namespace_info info_;
|
||||
};
|
||||
}
|
||||
46
headers/meta.hpp/meta_registry.hpp
Normal file
46
headers/meta.hpp/meta_registry.hpp
Normal file
@@ -0,0 +1,46 @@
|
||||
/*******************************************************************************
|
||||
* This file is part of the "https://github.com/blackmatov/meta.hpp"
|
||||
* For conditions of distribution and use, see copyright notice in LICENSE.md
|
||||
* Copyright (C) 2021, by Matvey Cherevko (blackmatov@gmail.com)
|
||||
******************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "meta_fwd.hpp"
|
||||
|
||||
#include "meta_class.hpp"
|
||||
#include "meta_function.hpp"
|
||||
#include "meta_namespace.hpp"
|
||||
#include "meta_variable.hpp"
|
||||
|
||||
namespace meta_hpp
|
||||
{
|
||||
class registry {
|
||||
public:
|
||||
registry() = default;
|
||||
|
||||
template < typename... Internals >
|
||||
registry& operator()(Internals&&...internals) {
|
||||
default_namespace_(std::forward<Internals>(internals)...);
|
||||
return *this;
|
||||
}
|
||||
|
||||
std::optional<class_info> get_class(std::string_view id) const {
|
||||
return default_namespace_.info().get_class(id);
|
||||
}
|
||||
|
||||
std::optional<function_info> get_function(std::string_view id) const {
|
||||
return default_namespace_.info().get_function(id);
|
||||
}
|
||||
|
||||
std::optional<namespace_info> get_namespace(std::string_view id) const {
|
||||
return default_namespace_.info().get_namespace(id);
|
||||
}
|
||||
|
||||
std::optional<variable_info> get_variable(std::string_view id) const {
|
||||
return default_namespace_.info().get_variable(id);
|
||||
}
|
||||
private:
|
||||
namespace_ default_namespace_{""};
|
||||
};
|
||||
}
|
||||
38
headers/meta.hpp/meta_value.hpp
Normal file
38
headers/meta.hpp/meta_value.hpp
Normal file
@@ -0,0 +1,38 @@
|
||||
/*******************************************************************************
|
||||
* This file is part of the "https://github.com/blackmatov/meta.hpp"
|
||||
* For conditions of distribution and use, see copyright notice in LICENSE.md
|
||||
* Copyright (C) 2021, by Matvey Cherevko (blackmatov@gmail.com)
|
||||
******************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "meta_fwd.hpp"
|
||||
|
||||
namespace meta_hpp
|
||||
{
|
||||
class value {
|
||||
public:
|
||||
value() = default;
|
||||
|
||||
template < typename T >
|
||||
value(T&& value)
|
||||
: raw_{std::forward<T>(value)} {}
|
||||
|
||||
template < typename T >
|
||||
auto cast() const {
|
||||
return std::any_cast<T>(raw_);
|
||||
}
|
||||
|
||||
template < typename T >
|
||||
auto try_cast() noexcept {
|
||||
return std::any_cast<T>(&raw_);
|
||||
}
|
||||
|
||||
template < typename T >
|
||||
auto try_cast() const noexcept {
|
||||
return std::any_cast<T>(&raw_);
|
||||
}
|
||||
private:
|
||||
std::any raw_;
|
||||
};
|
||||
}
|
||||
116
headers/meta.hpp/meta_variable.hpp
Normal file
116
headers/meta.hpp/meta_variable.hpp
Normal file
@@ -0,0 +1,116 @@
|
||||
/*******************************************************************************
|
||||
* This file is part of the "https://github.com/blackmatov/meta.hpp"
|
||||
* For conditions of distribution and use, see copyright notice in LICENSE.md
|
||||
* Copyright (C) 2021, by Matvey Cherevko (blackmatov@gmail.com)
|
||||
******************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "meta_fwd.hpp"
|
||||
|
||||
#include "meta_value.hpp"
|
||||
|
||||
namespace meta_hpp::variable_detail
|
||||
{
|
||||
template < typename Variable >
|
||||
struct variable_traits;
|
||||
|
||||
template < typename T >
|
||||
struct variable_traits<T*> {
|
||||
static constexpr bool is_const = false;
|
||||
using value_type = T;
|
||||
};
|
||||
|
||||
template < typename T >
|
||||
struct variable_traits<const T*> {
|
||||
static constexpr bool is_const = true;
|
||||
using value_type = T;
|
||||
};
|
||||
|
||||
template < auto Variable >
|
||||
value getter() {
|
||||
using vt = variable_traits<decltype(Variable)>;
|
||||
using value_type = typename vt::value_type;
|
||||
|
||||
value_type typed_value = *Variable;
|
||||
|
||||
return value{std::move(typed_value)};
|
||||
}
|
||||
|
||||
template < auto Variable >
|
||||
void setter(value value) {
|
||||
using vt = variable_traits<decltype(Variable)>;
|
||||
using value_type = typename vt::value_type;
|
||||
|
||||
if constexpr ( !vt::is_const ) {
|
||||
*Variable = value.cast<value_type>();
|
||||
} else {
|
||||
throw std::logic_error("an attempt to change a constant variable");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
namespace meta_hpp
|
||||
{
|
||||
class variable_info {
|
||||
public:
|
||||
variable_info() = delete;
|
||||
|
||||
variable_info(variable_info&&) = default;
|
||||
variable_info(const variable_info&) = default;
|
||||
|
||||
variable_info& operator=(variable_info&&) = default;
|
||||
variable_info& operator=(const variable_info&) = default;
|
||||
|
||||
variable_info(std::string id)
|
||||
: id_(std::move(id)) {}
|
||||
|
||||
const std::string& id() const noexcept {
|
||||
return id_;
|
||||
}
|
||||
public:
|
||||
value get() const {
|
||||
return getter_();
|
||||
}
|
||||
|
||||
template < typename Value >
|
||||
void set(Value&& value) const {
|
||||
return setter_(std::forward<Value>(value));
|
||||
}
|
||||
private:
|
||||
friend class class_info;
|
||||
friend class namespace_info;
|
||||
template < typename Class > friend class class_;
|
||||
friend class namespace_;
|
||||
template < auto Variable > friend class variable_;
|
||||
private:
|
||||
void merge_with_(const variable_info& other) {
|
||||
(void)other;
|
||||
}
|
||||
private:
|
||||
std::string id_;
|
||||
value(*getter_)();
|
||||
void(*setter_)(value);
|
||||
};
|
||||
}
|
||||
|
||||
namespace meta_hpp
|
||||
{
|
||||
template < auto Variable >
|
||||
class variable_ {
|
||||
public:
|
||||
static_assert(std::is_pointer_v<decltype(Variable)>);
|
||||
|
||||
explicit variable_(std::string id)
|
||||
: info_(std::move(id)) {
|
||||
info_.getter_ = &variable_detail::getter<Variable>;
|
||||
info_.setter_ = &variable_detail::setter<Variable>;
|
||||
}
|
||||
|
||||
const variable_info& info() const noexcept {
|
||||
return info_;
|
||||
}
|
||||
private:
|
||||
variable_info info_;
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user