mirror of
https://github.com/BlackMATov/meta.hpp.git
synced 2026-01-04 17:21:07 +07:00
common type info class
This commit is contained in:
@@ -10,6 +10,8 @@
|
|||||||
|
|
||||||
#include "meta_value.hpp"
|
#include "meta_value.hpp"
|
||||||
|
|
||||||
|
#include "meta_type.hpp"
|
||||||
|
|
||||||
#include "meta_class.hpp"
|
#include "meta_class.hpp"
|
||||||
#include "meta_class_info.hpp"
|
#include "meta_class_info.hpp"
|
||||||
|
|
||||||
|
|||||||
@@ -19,11 +19,14 @@
|
|||||||
#include <tuple>
|
#include <tuple>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
#include <variant>
|
||||||
|
|
||||||
namespace meta_hpp
|
namespace meta_hpp
|
||||||
{
|
{
|
||||||
class value;
|
class value;
|
||||||
|
|
||||||
|
class type;
|
||||||
|
|
||||||
class class_info;
|
class class_info;
|
||||||
class data_info;
|
class data_info;
|
||||||
class field_info;
|
class field_info;
|
||||||
|
|||||||
56
headers/meta.hpp/meta_type.hpp
Normal file
56
headers/meta.hpp/meta_type.hpp
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
/*******************************************************************************
|
||||||
|
* 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_info.hpp"
|
||||||
|
#include "meta_field_info.hpp"
|
||||||
|
#include "meta_function_info.hpp"
|
||||||
|
#include "meta_method_info.hpp"
|
||||||
|
#include "meta_namespace_info.hpp"
|
||||||
|
#include "meta_variable_info.hpp"
|
||||||
|
|
||||||
|
namespace meta_hpp
|
||||||
|
{
|
||||||
|
class type {
|
||||||
|
public:
|
||||||
|
type() = delete;
|
||||||
|
|
||||||
|
type(type&&) = default;
|
||||||
|
type(const type&) = default;
|
||||||
|
|
||||||
|
type& operator=(type&&) = default;
|
||||||
|
type& operator=(const type&) = default;
|
||||||
|
|
||||||
|
template < typename Info >
|
||||||
|
type(Info&& info)
|
||||||
|
: info_(std::forward<Info>(info)) {}
|
||||||
|
|
||||||
|
bool is_class() const noexcept { return std::holds_alternative<class_info>(info_); }
|
||||||
|
bool is_field() const noexcept { return std::holds_alternative<field_info>(info_); }
|
||||||
|
bool is_function() const noexcept { return std::holds_alternative<function_info>(info_); }
|
||||||
|
bool is_method() const noexcept { return std::holds_alternative<method_info>(info_); }
|
||||||
|
bool is_namespace() const noexcept { return std::holds_alternative<namespace_info>(info_); }
|
||||||
|
bool is_variable() const noexcept { return std::holds_alternative<variable_info>(info_); }
|
||||||
|
|
||||||
|
const class_info& get_class_info() const { return std::get<class_info>(info_); }
|
||||||
|
const field_info& get_field_info() const { return std::get<field_info>(info_); }
|
||||||
|
const function_info& get_function_info() const { return std::get<function_info>(info_); }
|
||||||
|
const method_info& get_method_info() const { return std::get<method_info>(info_); }
|
||||||
|
const namespace_info& get_namespace_info() const { return std::get<namespace_info>(info_); }
|
||||||
|
const variable_info& get_variable_info() const { return std::get<variable_info>(info_); }
|
||||||
|
private:
|
||||||
|
std::variant<
|
||||||
|
class_info,
|
||||||
|
field_info,
|
||||||
|
function_info,
|
||||||
|
method_info,
|
||||||
|
namespace_info,
|
||||||
|
variable_info> info_;
|
||||||
|
};
|
||||||
|
}
|
||||||
57
untests/meta_type_tests.cpp
Normal file
57
untests/meta_type_tests.cpp
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
/*******************************************************************************
|
||||||
|
* 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)
|
||||||
|
******************************************************************************/
|
||||||
|
|
||||||
|
#include <meta.hpp/meta_type.hpp>
|
||||||
|
|
||||||
|
#include <meta.hpp/meta_class.hpp>
|
||||||
|
#include <meta.hpp/meta_field.hpp>
|
||||||
|
#include <meta.hpp/meta_function.hpp>
|
||||||
|
#include <meta.hpp/meta_method.hpp>
|
||||||
|
#include <meta.hpp/meta_namespace.hpp>
|
||||||
|
#include <meta.hpp/meta_variable.hpp>
|
||||||
|
|
||||||
|
#include "doctest/doctest.hpp"
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
struct clazz {
|
||||||
|
int field{42};
|
||||||
|
static int variable;
|
||||||
|
int method() const { return 42; }
|
||||||
|
static int function() { return 42; }
|
||||||
|
};
|
||||||
|
|
||||||
|
int clazz::variable{42};
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("meta/type") {
|
||||||
|
namespace meta = meta_hpp;
|
||||||
|
using namespace std::string_literals;
|
||||||
|
|
||||||
|
meta::type class_type = meta::class_<clazz>("clazz").info();
|
||||||
|
CHECK(class_type.is_class());
|
||||||
|
CHECK(class_type.get_class_info().id() == "clazz");
|
||||||
|
|
||||||
|
meta::type field_type = meta::field_<&clazz::field>("field").info();
|
||||||
|
CHECK(field_type.is_field());
|
||||||
|
CHECK(field_type.get_field_info().id() == "field");
|
||||||
|
|
||||||
|
meta::type function_type = meta::function_<&clazz::function>("function").info();
|
||||||
|
CHECK(function_type.is_function());
|
||||||
|
CHECK(function_type.get_function_info().id() == "function");
|
||||||
|
|
||||||
|
meta::type method_type = meta::method_<&clazz::method>("method").info();
|
||||||
|
CHECK(method_type.is_method());
|
||||||
|
CHECK(method_type.get_method_info().id() == "method");
|
||||||
|
|
||||||
|
meta::type namespace_type = meta::namespace_("ns").info();
|
||||||
|
CHECK(namespace_type.is_namespace());
|
||||||
|
CHECK(namespace_type.get_namespace_info().id() == "ns");
|
||||||
|
|
||||||
|
meta::type variable_type = meta::variable_<&clazz::variable>("variable").info();
|
||||||
|
CHECK(variable_type.is_variable());
|
||||||
|
CHECK(variable_type.get_variable_info().id() == "variable");
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user