| 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 | |
| 37 | uint32_t MethodBind::get_hash() const { |
| 38 | MethodInfo mi; |
| 39 | mi.return_val = get_return_info(); |
| 40 | mi.flags = get_hint_flags(); |
| 41 | for (int i = 0; i < get_argument_count(); i++) { |
| 42 | mi.arguments.push_back(get_argument_info(i)); |
| 43 | } |
| 44 | mi.default_arguments = default_arguments; |
| 45 | |
| 46 | return mi.get_compatibility_hash(); |
| 47 | } |
| 48 | |
| 49 | PropertyInfo MethodBind::get_argument_info(int p_argument) const { |
| 50 | ERR_FAIL_INDEX_V(p_argument, get_argument_count(), PropertyInfo()); |
| 51 | |
| 52 | PropertyInfo info = _gen_argument_type_info(p_argument); |
| 53 | #ifdef DEBUG_ENABLED |
| 54 | if (info.name.is_empty()) { |
| 55 | info.name = p_argument < arg_names.size() ? String(arg_names[p_argument]) : String("_unnamed_arg" + itos(p_argument)); |
| 56 | } |
| 57 | #endif // DEBUG_ENABLED |
| 58 | return info; |
| 59 | } |
| 60 | |
| 61 | PropertyInfo MethodBind::get_return_info() const { |
| 62 | return _gen_argument_type_info(-1); |
| 63 | } |
| 64 | |
| 65 | void MethodBind::_set_const(bool p_const) { |
| 66 | _const = p_const; |
| 67 | } |
| 68 | |
| 69 | void MethodBind::_set_static(bool p_static) { |
| 70 | _static = p_static; |
| 71 | } |
| 72 | |
| 73 | void MethodBind::_set_returns(bool p_returns) { |
| 74 | _returns = p_returns; |
| 75 | } |
| 76 | |
| 77 | StringName MethodBind::get_name() const { |
| 78 | return name; |
| 79 | } |
| 80 | |
| 81 | void MethodBind::set_name(const StringName &p_name) { |
| 82 | name = p_name; |
| 83 | } |
| 84 | |
| 85 | #ifdef DEBUG_ENABLED |
| 86 | void MethodBind::set_argument_names(const Vector<StringName> &p_names) { |
| 87 | arg_names = p_names; |
| 88 | } |
| 89 | |
| 90 | Vector<StringName> MethodBind::get_argument_names() const { |
| 91 | return arg_names; |
| 92 | } |
| 93 | |
| 94 | #endif // DEBUG_ENABLED |
| 95 | |
| 96 | void MethodBind::set_default_arguments(const Vector<Variant> &p_defargs) { |
| 97 | default_arguments = p_defargs; |
| 98 | default_argument_count = default_arguments.size(); |
| 99 | } |
| 100 | |
| 101 | void MethodBind::_generate_argument_types(int p_count) { |
| 102 | set_argument_count(p_count); |
| 103 | |
| 104 | Variant::Type *argt = memnew_arr(Variant::Type, p_count + 1); |
| 105 | argt[0] = _gen_argument_type(-1); // return type |
| 106 | |
| 107 | for (int i = 0; i < p_count; i++) { |
| 108 | argt[i + 1] = _gen_argument_type(i); |
| 109 | } |
| 110 | |
| 111 | argument_types = argt; |
| 112 | } |
| 113 | |
| 114 | MethodBind::MethodBind() { |
| 115 | static int last_id = 0; |
| 116 | method_id = last_id++; |
| 117 | } |
| 118 | |
| 119 | MethodBind::~MethodBind() { |
| 120 | if (argument_types) { |
| 121 | memdelete_arr(argument_types); |
| 122 | } |
| 123 | } |