1/**************************************************************************/
2/* method_bind.cpp */
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// object.h needs to be the first include *before* method_bind.h
32// FIXME: Find out why and fix potential cyclical dependencies.
33#include "core/object/object.h"
34
35#include "method_bind.h"
36
37uint32_t MethodBind::get_hash() const {
38 uint32_t hash = hash_murmur3_one_32(has_return() ? 1 : 0);
39 hash = hash_murmur3_one_32(get_argument_count(), hash);
40
41 for (int i = (has_return() ? -1 : 0); i < get_argument_count(); i++) {
42 PropertyInfo pi = i == -1 ? get_return_info() : get_argument_info(i);
43 hash = hash_murmur3_one_32(get_argument_type(i), hash);
44 if (pi.class_name != StringName()) {
45 hash = hash_murmur3_one_32(pi.class_name.operator String().hash(), hash);
46 }
47 }
48
49 hash = hash_murmur3_one_32(get_default_argument_count(), hash);
50 for (int i = 0; i < get_default_argument_count(); i++) {
51 Variant v = get_default_argument(i);
52 hash = hash_murmur3_one_32(v.hash(), hash);
53 }
54
55 hash = hash_murmur3_one_32(is_const(), hash);
56 hash = hash_murmur3_one_32(is_vararg(), hash);
57
58 return hash_fmix32(hash);
59}
60
61PropertyInfo MethodBind::get_argument_info(int p_argument) const {
62 ERR_FAIL_INDEX_V(p_argument, get_argument_count(), PropertyInfo());
63
64 PropertyInfo info = _gen_argument_type_info(p_argument);
65#ifdef DEBUG_METHODS_ENABLED
66 if (info.name.is_empty()) {
67 info.name = p_argument < arg_names.size() ? String(arg_names[p_argument]) : String("_unnamed_arg" + itos(p_argument));
68 }
69#endif
70 return info;
71}
72
73PropertyInfo MethodBind::get_return_info() const {
74 return _gen_argument_type_info(-1);
75}
76
77void MethodBind::_set_const(bool p_const) {
78 _const = p_const;
79}
80
81void MethodBind::_set_static(bool p_static) {
82 _static = p_static;
83}
84
85void MethodBind::_set_returns(bool p_returns) {
86 _returns = p_returns;
87}
88
89StringName MethodBind::get_name() const {
90 return name;
91}
92
93void MethodBind::set_name(const StringName &p_name) {
94 name = p_name;
95}
96
97#ifdef DEBUG_METHODS_ENABLED
98void MethodBind::set_argument_names(const Vector<StringName> &p_names) {
99 arg_names = p_names;
100}
101
102Vector<StringName> MethodBind::get_argument_names() const {
103 return arg_names;
104}
105
106#endif
107
108void MethodBind::set_default_arguments(const Vector<Variant> &p_defargs) {
109 default_arguments = p_defargs;
110 default_argument_count = default_arguments.size();
111}
112
113void MethodBind::_generate_argument_types(int p_count) {
114 set_argument_count(p_count);
115
116 Variant::Type *argt = memnew_arr(Variant::Type, p_count + 1);
117 argt[0] = _gen_argument_type(-1); // return type
118
119 for (int i = 0; i < p_count; i++) {
120 argt[i + 1] = _gen_argument_type(i);
121 }
122
123 argument_types = argt;
124}
125
126MethodBind::MethodBind() {
127 static int last_id = 0;
128 method_id = last_id++;
129}
130
131MethodBind::~MethodBind() {
132 if (argument_types) {
133 memdelete_arr(argument_types);
134 }
135}
136