Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/object/method_info.h
45997 views
1
/**************************************************************************/
2
/* method_info.h */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#pragma once
32
33
#include "core/object/property_info.h"
34
#include "core/variant/variant.h"
35
36
enum MethodFlags {
37
METHOD_FLAG_NORMAL = 1,
38
METHOD_FLAG_EDITOR = 2,
39
METHOD_FLAG_CONST = 4,
40
METHOD_FLAG_VIRTUAL = 8,
41
METHOD_FLAG_VARARG = 16,
42
METHOD_FLAG_STATIC = 32,
43
METHOD_FLAG_OBJECT_CORE = 64,
44
METHOD_FLAG_VIRTUAL_REQUIRED = 128,
45
METHOD_FLAGS_DEFAULT = METHOD_FLAG_NORMAL,
46
};
47
48
struct MethodInfo {
49
String name;
50
PropertyInfo return_val;
51
uint32_t flags = METHOD_FLAGS_DEFAULT;
52
int id = 0;
53
Vector<PropertyInfo> arguments;
54
Vector<Variant> default_arguments;
55
int return_val_metadata = 0;
56
Vector<int> arguments_metadata;
57
58
int get_argument_meta(int p_arg) const {
59
ERR_FAIL_COND_V(p_arg < -1 || p_arg > arguments.size(), 0);
60
if (p_arg == -1) {
61
return return_val_metadata;
62
}
63
return arguments_metadata.size() > p_arg ? arguments_metadata[p_arg] : 0;
64
}
65
66
inline bool operator==(const MethodInfo &p_method) const { return id == p_method.id && name == p_method.name; }
67
inline bool operator<(const MethodInfo &p_method) const { return id == p_method.id ? (name < p_method.name) : (id < p_method.id); }
68
69
operator Dictionary() const;
70
71
static MethodInfo from_dict(const Dictionary &p_dict);
72
73
uint32_t get_compatibility_hash() const;
74
75
MethodInfo() {}
76
77
explicit MethodInfo(const GDExtensionMethodInfo &pinfo) :
78
name(*reinterpret_cast<StringName *>(pinfo.name)),
79
return_val(PropertyInfo(pinfo.return_value)),
80
flags(pinfo.flags),
81
id(pinfo.id) {
82
for (uint32_t i = 0; i < pinfo.argument_count; i++) {
83
arguments.push_back(PropertyInfo(pinfo.arguments[i]));
84
}
85
const Variant *def_values = (const Variant *)pinfo.default_arguments;
86
for (uint32_t j = 0; j < pinfo.default_argument_count; j++) {
87
default_arguments.push_back(def_values[j]);
88
}
89
}
90
91
MethodInfo(const String &p_name) { name = p_name; }
92
93
template <typename... VarArgs>
94
MethodInfo(const String &p_name, VarArgs... p_params) {
95
name = p_name;
96
arguments = Vector<PropertyInfo>{ p_params... };
97
}
98
99
MethodInfo(Variant::Type ret) { return_val.type = ret; }
100
MethodInfo(Variant::Type ret, const String &p_name) {
101
return_val.type = ret;
102
name = p_name;
103
}
104
105
template <typename... VarArgs>
106
MethodInfo(Variant::Type ret, const String &p_name, VarArgs... p_params) {
107
name = p_name;
108
return_val.type = ret;
109
arguments = Vector<PropertyInfo>{ p_params... };
110
}
111
112
MethodInfo(const PropertyInfo &p_ret, const String &p_name) {
113
return_val = p_ret;
114
name = p_name;
115
}
116
117
template <typename... VarArgs>
118
MethodInfo(const PropertyInfo &p_ret, const String &p_name, VarArgs... p_params) {
119
return_val = p_ret;
120
name = p_name;
121
arguments = Vector<PropertyInfo>{ p_params... };
122
}
123
};
124
125