1 /*************************************************************************/
2 /*  method_bind.cpp                                                      */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md)    */
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 // object.h needs to be the first include *before* method_bind.h
31 // FIXME: Find out why and fix potential cyclical dependencies.
32 #include "object.h"
33 
34 #include "method_bind.h"
35 
36 #ifdef DEBUG_METHODS_ENABLED
get_argument_info(int p_argument) const37 PropertyInfo MethodBind::get_argument_info(int p_argument) const {
38 
39 	if (p_argument >= 0) {
40 
41 		String name = (p_argument < arg_names.size()) ? String(arg_names[p_argument]) : String("arg" + itos(p_argument));
42 		PropertyInfo pi(get_argument_type(p_argument), name);
43 		if ((pi.type == Variant::OBJECT) && name.find(":") != -1) {
44 			pi.hint = PROPERTY_HINT_RESOURCE_TYPE;
45 			pi.hint_string = name.get_slicec(':', 1);
46 			pi.name = name.get_slicec(':', 0);
47 		}
48 		return pi;
49 
50 	} else {
51 
52 		Variant::Type at = get_argument_type(-1);
53 		if (at == Variant::OBJECT && ret_type)
54 			return PropertyInfo(at, "ret", PROPERTY_HINT_RESOURCE_TYPE, ret_type);
55 		else
56 			return PropertyInfo(at, "ret");
57 	}
58 
59 	return PropertyInfo();
60 }
61 
62 #endif
_set_const(bool p_const)63 void MethodBind::_set_const(bool p_const) {
64 
65 	_const = p_const;
66 }
67 
get_name() const68 StringName MethodBind::get_name() const {
69 	return name;
70 }
set_name(const StringName & p_name)71 void MethodBind::set_name(const StringName &p_name) {
72 	name = p_name;
73 }
74 
75 #ifdef DEBUG_METHODS_ENABLED
set_argument_names(const Vector<StringName> & p_names)76 void MethodBind::set_argument_names(const Vector<StringName> &p_names) {
77 
78 	arg_names = p_names;
79 }
get_argument_names() const80 Vector<StringName> MethodBind::get_argument_names() const {
81 
82 	return arg_names;
83 }
84 
85 #endif
86 
set_default_arguments(const Vector<Variant> & p_defargs)87 void MethodBind::set_default_arguments(const Vector<Variant> &p_defargs) {
88 	default_arguments = p_defargs;
89 	default_argument_count = default_arguments.size();
90 }
91 
92 #ifdef DEBUG_METHODS_ENABLED
_generate_argument_types(int p_count)93 void MethodBind::_generate_argument_types(int p_count) {
94 
95 	set_argument_count(p_count);
96 	Variant::Type *argt = memnew_arr(Variant::Type, p_count + 1);
97 	argt[0] = _gen_argument_type(-1);
98 	for (int i = 0; i < p_count; i++) {
99 		argt[i + 1] = _gen_argument_type(i);
100 	}
101 	set_argument_types(argt);
102 }
103 
104 #endif
105 
MethodBind()106 MethodBind::MethodBind() {
107 	static int last_id = 0;
108 	method_id = last_id++;
109 	hint_flags = METHOD_FLAGS_DEFAULT;
110 	argument_count = 0;
111 	default_argument_count = 0;
112 #ifdef DEBUG_METHODS_ENABLED
113 	argument_types = NULL;
114 #endif
115 	_const = false;
116 }
117 
~MethodBind()118 MethodBind::~MethodBind() {
119 #ifdef DEBUG_METHODS_ENABLED
120 	if (argument_types)
121 		memdelete_arr(argument_types);
122 #endif
123 }
124