method_bind.h
1 /*************************************************************************/
2 /* method_bind.h */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* http://www.godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
9 /* */
10 /* Permission is hereby granted, free of charge, to any person obtaining */
11 /* a copy of this software and associated documentation files (the */
12 /* "Software"), to deal in the Software without restriction, including */
13 /* without limitation the rights to use, copy, modify, merge, publish, */
14 /* distribute, sublicense, and/or sell copies of the Software, and to */
15 /* permit persons to whom the Software is furnished to do so, subject to */
16 /* the following conditions: */
17 /* */
18 /* The above copyright notice and this permission notice shall be */
19 /* included in all copies or substantial portions of the Software. */
20 /* */
21 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
22 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
23 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
24 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
25 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
26 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
27 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
28 /*************************************************************************/
29 #ifndef METHOD_BIND_H
30 #define METHOD_BIND_H
31 
32 #include "list.h"
33 #include "variant.h"
34 #include "object.h"
35 #include <stdio.h>
36 
41 #ifdef DEBUG_ENABLED
42 #define DEBUG_METHODS_ENABLED
43 #endif
44 
45 enum MethodFlags {
46 
47  METHOD_FLAG_NORMAL=1,
48  METHOD_FLAG_EDITOR=2,
49  METHOD_FLAG_NOSCRIPT=4,
50  METHOD_FLAG_CONST=8,
51  METHOD_FLAG_REVERSE=16, // used for events
52  METHOD_FLAG_VIRTUAL=32,
53  METHOD_FLAG_FROM_SCRIPT=64,
54  METHOD_FLAGS_DEFAULT=METHOD_FLAG_NORMAL,
55 };
56 
57 template<class T>
58 struct VariantCaster {
59 
60  static _FORCE_INLINE_ T cast(const Variant& p_variant) {
61 
62  return p_variant;
63  }
64 };
65 
66 template<class T>
67 struct VariantCaster<T&> {
68 
69  static _FORCE_INLINE_ T cast(const Variant& p_variant) {
70 
71  return p_variant;
72  }
73 };
74 
75 template<class T>
76 struct VariantCaster<const T&> {
77 
78  static _FORCE_INLINE_ T cast(const Variant& p_variant) {
79 
80  return p_variant;
81  }
82 };
83 
84 #define _VC( m_idx )\
85  (VariantCaster<P##m_idx>::cast( (m_idx-1)>=p_arg_count?get_default_argument(m_idx-1):*p_args[m_idx-1] ))
86 
87 //SIMPLE_NUMERIC_TYPE is used to avoid a warning on Variant::get_type_for
88 #define VARIANT_ENUM_CAST( m_enum ) \
89 SIMPLE_NUMERIC_TYPE( m_enum );\
90 template<> \
91 struct VariantCaster<m_enum> {\
92 \
93  static _FORCE_INLINE_ m_enum cast(const Variant& p_variant) {\
94  return (m_enum)p_variant.operator int();\
95  }\
96 };
97 
98 
99 #define CHECK_ARG(m_arg)\
100  if ((m_arg-1)<p_arg_count) {\
101  Variant::Type argtype=get_argument_type(m_arg-1);\
102  if (!Variant::can_convert_strict(p_args[m_arg-1]->get_type(),argtype)) {\
103  r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;\
104  r_error.argument=m_arg-1;\
105  r_error.expected=argtype;\
106  return Variant();\
107  }\
108  }
109 
110 #define CHECK_NOARG(m_arg)\
111  {\
112  if (p_arg##m_arg.get_type()!=Variant::NIL) {\
113  if (r_argerror) *r_argerror=(m_arg-1);\
114  return CALL_ERROR_EXTRA_ARGUMENT;\
115  }\
116  }
117 
118 // some helpers
119 
120 VARIANT_ENUM_CAST( Vector3::Axis );
121 VARIANT_ENUM_CAST( Image::Format );
122 VARIANT_ENUM_CAST( Error );
123 VARIANT_ENUM_CAST( wchar_t );
124 VARIANT_ENUM_CAST( Margin );
125 VARIANT_ENUM_CAST( Orientation );
126 VARIANT_ENUM_CAST( HAlign );
127 
128 class MethodBind {
129 
130 
131  int method_id;
132  uint32_t hint_flags;
133  StringName name;
134  Vector<Variant> default_arguments;
135  int default_argument_count;
136  int argument_count;
137 #ifdef DEBUG_METHODS_ENABLED
138  Vector<StringName> arg_names;
139  Variant::Type *argument_types;
140  StringName ret_type;
141 #endif
142  bool _const;
143 
144 
145 protected:
146 
147  void _set_const(bool p_const);
148 #ifdef DEBUG_METHODS_ENABLED
149  virtual Variant::Type _gen_argument_type(int p_arg) const=0;
150  void _generate_argument_types(int p_count);
151  void set_argument_types(Variant::Type *p_types) { argument_types=p_types; }
152 #endif
153  void set_argument_count(int p_count) { argument_count=p_count; }
154 public:
155 
156  Vector<Variant> get_default_arguments() const { return default_arguments; }
157  _FORCE_INLINE_ int get_default_argument_count() const { return default_argument_count; }
158 
159  _FORCE_INLINE_ Variant has_default_argument(int p_arg) const {
160 
161  int idx=argument_count-p_arg-1;
162 
163  if (idx<0 || idx>=default_arguments.size())
164  return false;
165  else
166  return true;
167  }
168 
169  _FORCE_INLINE_ Variant get_default_argument(int p_arg) const {
170 
171  int idx=argument_count-p_arg-1;
172 
173  if (idx<0 || idx>=default_arguments.size())
174  return Variant();
175  else
176  return default_arguments[idx];
177  }
178 
179 #ifdef DEBUG_METHODS_ENABLED
180 
181  _FORCE_INLINE_ void set_return_type(const StringName& p_type) { ret_type=p_type; }
182  _FORCE_INLINE_ StringName get_return_type() const { return ret_type; }
183 
184  _FORCE_INLINE_ Variant::Type get_argument_type(int p_argument) const {
185 
186  ERR_FAIL_COND_V(p_argument<-1 || p_argument>argument_count,Variant::NIL);
187  return argument_types[p_argument+1];
188 
189  }
190 
191  PropertyInfo get_argument_info(int p_argument) const;
192 
193  void set_argument_names(const Vector<StringName>& p_names);
194  Vector<StringName> get_argument_names() const;
195 #endif
196  void set_hint_flags(uint32_t p_hint) { hint_flags=p_hint; }
197  uint32_t get_hint_flags() const { return hint_flags|(is_const()?METHOD_FLAG_CONST:0); }
198  virtual String get_instance_type() const=0;
199 
200  _FORCE_INLINE_ int get_argument_count() const { return argument_count; };
201 
202 #if 0
203  _FORCE_INLINE_ Variant call_safe(const Variant** p_args,int p_arg_count, Variant::CallError& r_error) {
204 
205  r_error.error=Variant::CallError::CALL_OK;
206  check_call( p_args, &errorarg );
207  if (!err)
208  return call(p_object, VARIANT_ARG_PASS );
209 
210  VARIANT_ARGPTRS
211  String errstr;
212  String methodname = get_instance_type()+"::"+name;
213  if (err==CALL_ERROR_ARGUMENT_TYPE) {
214  errstr="Invalid Argument to call: '"+methodname+"'. Cannot convert argument "+itos(errorarg+1)+" from "+Variant::get_type_name(get_argument_type(errorarg))+" to "+Variant::get_type_name(argptr[errorarg]->get_type())+".";
215  }
216  if (err==CALL_ERROR_EXTRA_ARGUMENT) {
217  errstr="Invalid call. Member function '"+methodname+"' takes "+itos(get_argument_count())+" argument, but argument "+itos(errorarg+1)+" was received.";
218  }
219 
220  ERR_PRINT(errstr.ascii().get_data());
221  return Variant();
222  }
223 #endif
224  virtual Variant call(Object* p_object,const Variant** p_args,int p_arg_count, Variant::CallError& r_error)=0;
225  StringName get_name() const;
226  void set_name(const StringName& p_name);
227  _FORCE_INLINE_ int get_method_id() const { return method_id; }
228  _FORCE_INLINE_ bool is_const() const { return _const; }
229 
230 
231  void set_default_arguments(const Vector<Variant>& p_defargs);
232 
233  MethodBind();
234  virtual ~MethodBind();
235 };
236 
237 
238 template<class T>
239 class MethodBindNative : public MethodBind {
240 public:
241  typedef Variant (T::*NativeCall)(const Variant**,int ,Variant::CallError &);
242 protected:
243 
244  NativeCall call_method;
245 public:
246 
247  virtual Variant::Type _gen_argument_type(int p_arg) const {
248 
249  return Variant::NIL;
250  }
251 
252  virtual Variant call(Object* p_object,const Variant** p_args,int p_arg_count, Variant::CallError& r_error) {
253 
254  T* instance=static_cast<T*>(p_object);
255  return (instance->*call_method)(p_args,p_arg_count,r_error);
256  }
257  void set_method_info(const MethodInfo& p_info) {
258 
259 
260  set_argument_count( p_info.arguments.size() );
261 #ifdef DEBUG_METHODS_ENABLED
262  Variant::Type *at = memnew_arr( Variant::Type , p_info.arguments.size()+1 );
263  at[0]=p_info.return_val.type;
264  if (p_info.arguments.size()) {
265 
266  Vector<StringName> names;
267  names.resize(p_info.arguments.size());
268  for(int i=0;i<p_info.arguments.size();i++) {
269 
270  at[i+1]=p_info.arguments[i].type;
271  names[i]=p_info.arguments[i].name;
272  }
273 
274  set_argument_names(names);
275  }
276  set_argument_types(at);
277 #endif
278  }
279 
280  void set_method(NativeCall p_method) { call_method=p_method; }
281  virtual bool is_const() const { return false; }
282  virtual String get_instance_type() const { return T::get_type_static(); }
283 
284  MethodBindNative() { call_method=NULL; }
285 };
286 
287 
288 template<class T >
289 MethodBind* create_native_method_bind( Variant (T::*p_method)(const Variant**,int ,Variant::CallError &), const MethodInfo& p_info ) {
290 
291  MethodBindNative<T > * a = memnew( (MethodBindNative<T >) );
292  a->set_method(p_method);
293  a->set_method_info(p_info);
294  return a;
295 }
296 
297 
300 // tale of an amazing hack.. //
301 
302 // if you declare an nonexistent class..
303 class __UnexistingClass;
304 
305 
306 #include "method_bind.inc"
307 
308 #endif
Definition: method_bind.h:58
Definition: variant.h:74
Format
Definition: image.h:57
Definition: object.h:104
Definition: string_db.h:48
Definition: object.h:127
Definition: ustring.h:64
Definition: object.h:317
Definition: method_bind.h:128
Definition: method_bind.h:239
Definition: variant.h:379