mirror of
https://github.com/BlackMATov/meta.hpp.git
synced 2025-12-15 03:45:30 +07:00
add scopes manual for github issue #18
This commit is contained in:
6
.vscode/settings.json
vendored
6
.vscode/settings.json
vendored
@@ -1,4 +1,10 @@
|
||||
{
|
||||
"[cpp]": {
|
||||
"files.encoding": "utf8",
|
||||
"files.insertFinalNewline": true,
|
||||
"files.trimFinalNewlines": true,
|
||||
"files.trimTrailingWhitespace": true
|
||||
},
|
||||
"clangd.arguments": [
|
||||
"--all-scopes-completion",
|
||||
"--background-index",
|
||||
|
||||
@@ -51,7 +51,7 @@ TEST_CASE("meta/meta_examples/function/usage") {
|
||||
// also, we can find it with argument types (for overloaded functions)
|
||||
CHECK(sub_function == math_scope.get_function_with<int, int>("sub"));
|
||||
|
||||
// checks a type of the founded function
|
||||
// checks a type of the found function
|
||||
CHECK(sub_function.get_type() == meta::resolve_type<int(*)(int, int)>());
|
||||
|
||||
// checks the ability to call the function with specific arguments
|
||||
|
||||
111
manuals/meta_examples/scopes_example.cpp
Normal file
111
manuals/meta_examples/scopes_example.cpp
Normal file
@@ -0,0 +1,111 @@
|
||||
/*******************************************************************************
|
||||
* 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-2022, by Matvey Cherevko (blackmatov@gmail.com)
|
||||
******************************************************************************/
|
||||
|
||||
#include "../meta_manuals.hpp"
|
||||
|
||||
namespace
|
||||
{
|
||||
struct ivec2 {
|
||||
int x{}, y{};
|
||||
};
|
||||
|
||||
struct ivec3 {
|
||||
int x{}, y{}, z{};
|
||||
};
|
||||
|
||||
const ivec2 unit2{1, 1};
|
||||
const ivec3 unit3{1, 1, 1};
|
||||
|
||||
[[maybe_unused]]
|
||||
int dot2(const ivec2& a, const ivec2& b) {
|
||||
return a.x * b.x
|
||||
+ a.y * b.y;
|
||||
}
|
||||
|
||||
[[maybe_unused]]
|
||||
int dot3(const ivec3& a, const ivec3& b) {
|
||||
return a.x * b.x
|
||||
+ a.y * b.y
|
||||
+ a.z * b.z;
|
||||
}
|
||||
|
||||
[[maybe_unused]]
|
||||
bool operator==(const ivec2& a, const ivec2& b) {
|
||||
return a.x == b.x
|
||||
&& a.y == b.y;
|
||||
}
|
||||
|
||||
[[maybe_unused]]
|
||||
bool operator==(const ivec3& a, const ivec3& b) {
|
||||
return a.x == b.x
|
||||
&& a.y == b.y
|
||||
&& a.z == b.z;
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("meta/meta_examples/scopes/local") {
|
||||
namespace meta = meta_hpp;
|
||||
|
||||
// creates new local scope
|
||||
// you can use it inplace or store anywhere to use it later
|
||||
const meta::scope math_scope = meta::local_scope_("math")
|
||||
.typedef_<ivec2>("ivec2")
|
||||
.typedef_<ivec3>("ivec3")
|
||||
.function_("dot2", &dot2)
|
||||
.function_("dot3", &dot3)
|
||||
.variable_("unit2", &unit2)
|
||||
.variable_("unit3", &unit3);
|
||||
|
||||
// finds types by names
|
||||
const meta::class_type ivec2_type = math_scope.get_typedef("ivec2").as_class();
|
||||
const meta::class_type ivec3_type = math_scope.get_typedef("ivec3").as_class();
|
||||
|
||||
// checks found types
|
||||
CHECK(ivec2_type == meta::resolve_type<ivec2>());
|
||||
CHECK(ivec3_type == meta::resolve_type<ivec3>());
|
||||
|
||||
// similarly, we can find free functions by name
|
||||
const meta::function dot2_function = math_scope.get_function("dot2");
|
||||
const meta::function dot3_function = math_scope.get_function("dot3");
|
||||
|
||||
// calls and checks found functions
|
||||
CHECK(dot2_function(ivec2{3,4}, ivec2{5,6}) == 39);
|
||||
CHECK(dot3_function(ivec3{3,4,5}, ivec3{6,7,8}) == 86);
|
||||
|
||||
// and free variables, of course
|
||||
const meta::variable unit2_variable = math_scope.get_variable("unit2");
|
||||
const meta::variable unit3_variable = math_scope.get_variable("unit3");
|
||||
|
||||
// checks and uses found variables with functions
|
||||
CHECK(unit2_variable.get() == ivec2{1,1});
|
||||
CHECK(unit3_variable.get() == ivec3{1,1,1});
|
||||
CHECK(dot2_function(unit2_variable.get(), unit2_variable.get()) == 2);
|
||||
CHECK(dot3_function(unit3_variable.get(), unit3_variable.get()) == 3);
|
||||
}
|
||||
|
||||
TEST_CASE("meta/meta_examples/scopes/global") {
|
||||
namespace meta = meta_hpp;
|
||||
|
||||
// also, we can create global scopes
|
||||
meta::static_scope_("meta/meta_examples/scopes/global/math")
|
||||
.function_("dot2", &dot2)
|
||||
.function_("dot3", &dot3)
|
||||
.variable_("unit2", &unit2)
|
||||
.variable_("unit3", &unit3);
|
||||
|
||||
// finds our global scope by name
|
||||
const meta::scope math_scope = meta::resolve_scope("meta/meta_examples/scopes/global/math");
|
||||
|
||||
// and uses it in the same way
|
||||
const meta::function dot2_function = math_scope.get_function("dot2");
|
||||
const meta::function dot3_function = math_scope.get_function("dot3");
|
||||
const meta::variable unit2_variable = math_scope.get_variable("unit2");
|
||||
const meta::variable unit3_variable = math_scope.get_variable("unit3");
|
||||
|
||||
// everything works as expected
|
||||
CHECK(dot2_function(unit2_variable.get(), unit2_variable.get()) == 2);
|
||||
CHECK(dot3_function(unit3_variable.get(), unit3_variable.get()) == 3);
|
||||
}
|
||||
@@ -23,7 +23,7 @@ TEST_CASE("meta/meta_examples/variable/usage") {
|
||||
// finds the variable by name 'pi_v'
|
||||
const meta::variable pi_variable = constants_scope.get_variable("pi_v");
|
||||
|
||||
// checks the type and data type of the founded variable
|
||||
// checks the type and data type of the found variable
|
||||
CHECK(pi_variable.get_type() == meta::resolve_type<const double*>());
|
||||
CHECK(pi_variable.get_type().get_data_type() == meta::resolve_type<double>());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user