From bf7c86c791e3c3a374b85ff8f7d245a6eb307474 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Sun, 30 Oct 2022 05:55:43 +0700 Subject: [PATCH] add scopes manual for github issue #18 --- .vscode/settings.json | 6 ++ manuals/meta_examples/function_example.cpp | 2 +- manuals/meta_examples/scopes_example.cpp | 111 +++++++++++++++++++++ manuals/meta_examples/variable_example.cpp | 2 +- 4 files changed, 119 insertions(+), 2 deletions(-) create mode 100644 manuals/meta_examples/scopes_example.cpp diff --git a/.vscode/settings.json b/.vscode/settings.json index 53d3e94..dbf82d7 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,4 +1,10 @@ { + "[cpp]": { + "files.encoding": "utf8", + "files.insertFinalNewline": true, + "files.trimFinalNewlines": true, + "files.trimTrailingWhitespace": true + }, "clangd.arguments": [ "--all-scopes-completion", "--background-index", diff --git a/manuals/meta_examples/function_example.cpp b/manuals/meta_examples/function_example.cpp index a127907..9e2d481 100644 --- a/manuals/meta_examples/function_example.cpp +++ b/manuals/meta_examples/function_example.cpp @@ -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("sub")); - // checks a type of the founded function + // checks a type of the found function CHECK(sub_function.get_type() == meta::resolve_type()); // checks the ability to call the function with specific arguments diff --git a/manuals/meta_examples/scopes_example.cpp b/manuals/meta_examples/scopes_example.cpp new file mode 100644 index 0000000..2fb1f8f --- /dev/null +++ b/manuals/meta_examples/scopes_example.cpp @@ -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") + .typedef_("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()); + CHECK(ivec3_type == meta::resolve_type()); + + // 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); +} diff --git a/manuals/meta_examples/variable_example.cpp b/manuals/meta_examples/variable_example.cpp index 634f3cc..7e25d62 100644 --- a/manuals/meta_examples/variable_example.cpp +++ b/manuals/meta_examples/variable_example.cpp @@ -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()); CHECK(pi_variable.get_type().get_data_type() == meta::resolve_type());