-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathscopes_example.cpp
110 lines (88 loc) · 3.75 KB
/
scopes_example.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*******************************************************************************
* 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-2025, by Matvey Cherevko (blackmatov@gmail.com)
******************************************************************************/
#include <meta.hpp/meta_all.hpp>
#include <doctest/doctest.h>
#include <fmt/core.h>
namespace
{
struct ivec2 {
int x{}, y{};
};
struct ivec3 {
int x{}, y{}, z{};
};
const ivec2 unit2{1, 1};
const ivec3 unit3{1, 1, 1};
int dot2(const ivec2& a, const ivec2& b) {
return a.x * b.x
+ a.y * b.y;
}
int dot3(const ivec3& a, const ivec3& b) {
return a.x * b.x
+ a.y * b.y
+ a.z * b.z;
}
bool operator==(const ivec2& a, const ivec2& b) {
return a.x == b.x
&& a.y == b.y;
}
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}).as<int>() == 39);
CHECK(dot3_function(ivec3{3,4,5}, ivec3{6,7,8}).as<int>() == 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().as<ivec2>() == ivec2{1,1});
CHECK(unit3_variable.get().as<ivec3>() == ivec3{1,1,1});
CHECK(dot2_function(unit2_variable.get(), unit2_variable.get()).as<int>() == 2);
CHECK(dot3_function(unit3_variable.get(), unit3_variable.get()).as<int>() == 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()).as<int>() == 2);
CHECK(dot3_function(unit3_variable.get(), unit3_variable.get()).as<int>() == 3);
}