method_bind.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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-2015 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. #include "list.h"
  32. #include "variant.h"
  33. #include "object.h"
  34. #include <stdio.h>
  35. /**
  36. @author Juan Linietsky <reduzio@gmail.com>
  37. */
  38. #ifdef DEBUG_ENABLED
  39. #define DEBUG_METHODS_ENABLED
  40. #endif
  41. enum MethodFlags {
  42. METHOD_FLAG_NORMAL=1,
  43. METHOD_FLAG_EDITOR=2,
  44. METHOD_FLAG_NOSCRIPT=4,
  45. METHOD_FLAG_CONST=8,
  46. METHOD_FLAG_REVERSE=16, // used for events
  47. METHOD_FLAG_VIRTUAL=32,
  48. METHOD_FLAGS_DEFAULT=METHOD_FLAG_NORMAL,
  49. };
  50. template<class T>
  51. struct VariantCaster {
  52. static _FORCE_INLINE_ T cast(const Variant& p_variant) {
  53. return p_variant;
  54. }
  55. };
  56. template<class T>
  57. struct VariantCaster<T&> {
  58. static _FORCE_INLINE_ T cast(const Variant& p_variant) {
  59. return p_variant;
  60. }
  61. };
  62. template<class T>
  63. struct VariantCaster<const T&> {
  64. static _FORCE_INLINE_ T cast(const Variant& p_variant) {
  65. return p_variant;
  66. }
  67. };
  68. #define _VC( m_idx )\
  69. (VariantCaster<P##m_idx>::cast( (m_idx-1)>=p_arg_count?get_default_argument(m_idx-1):*p_args[m_idx-1] ))
  70. //SIMPLE_NUMERIC_TYPE is used to avoid a warning on Variant::get_type_for
  71. #define VARIANT_ENUM_CAST( m_enum ) \
  72. SIMPLE_NUMERIC_TYPE( m_enum );\
  73. template<> \
  74. struct VariantCaster<m_enum> {\
  75. \
  76. static _FORCE_INLINE_ m_enum cast(const Variant& p_variant) {\
  77. return (m_enum)p_variant.operator int();\
  78. }\
  79. };
  80. #define CHECK_ARG(m_arg)\
  81. if ((m_arg-1)<p_arg_count) {\
  82. Variant::Type argtype=get_argument_type(m_arg-1);\
  83. if (!Variant::can_convert_strict(p_args[m_arg-1]->get_type(),argtype)) {\
  84. r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;\
  85. r_error.argument=m_arg-1;\
  86. r_error.expected=argtype;\
  87. return Variant();\
  88. }\
  89. }
  90. #define CHECK_NOARG(m_arg)\
  91. {\
  92. if (p_arg##m_arg.get_type()!=Variant::NIL) {\
  93. if (r_argerror) *r_argerror=(m_arg-1);\
  94. return CALL_ERROR_EXTRA_ARGUMENT;\
  95. }\
  96. }
  97. // some helpers
  98. VARIANT_ENUM_CAST( Vector3::Axis );
  99. VARIANT_ENUM_CAST( Image::Format );
  100. VARIANT_ENUM_CAST( Error );
  101. VARIANT_ENUM_CAST( wchar_t );
  102. VARIANT_ENUM_CAST( Margin );
  103. VARIANT_ENUM_CAST( Orientation );
  104. VARIANT_ENUM_CAST( HAlign );
  105. class MethodBind {
  106. int method_id;
  107. uint32_t hint_flags;
  108. StringName name;
  109. Vector<Variant> default_arguments;
  110. int default_argument_count;
  111. int argument_count;
  112. #ifdef DEBUG_METHODS_ENABLED
  113. Vector<StringName> arg_names;
  114. Variant::Type *argument_types;
  115. StringName ret_type;
  116. #endif
  117. bool _const;
  118. protected:
  119. void _set_const(bool p_const);
  120. #ifdef DEBUG_METHODS_ENABLED
  121. virtual Variant::Type _gen_argument_type(int p_arg) const=0;
  122. void _generate_argument_types(int p_count);
  123. void set_argument_types(Variant::Type *p_types) { argument_types=p_types; }
  124. #endif
  125. void set_argument_count(int p_count) { argument_count=p_count; }
  126. public:
  127. Vector<Variant> get_default_arguments() const { return default_arguments; }
  128. _FORCE_INLINE_ int get_default_argument_count() const { return default_argument_count; }
  129. _FORCE_INLINE_ Variant has_default_argument(int p_arg) const {
  130. int idx=argument_count-p_arg-1;
  131. if (idx<0 || idx>=default_arguments.size())
  132. return false;
  133. else
  134. return true;
  135. }
  136. _FORCE_INLINE_ Variant get_default_argument(int p_arg) const {
  137. int idx=argument_count-p_arg-1;
  138. if (idx<0 || idx>=default_arguments.size())
  139. return Variant();
  140. else
  141. return default_arguments[idx];
  142. }
  143. #ifdef DEBUG_METHODS_ENABLED
  144. _FORCE_INLINE_ void set_return_type(const StringName& p_type) { ret_type=p_type; }
  145. _FORCE_INLINE_ StringName get_return_type() const { return ret_type; }
  146. _FORCE_INLINE_ Variant::Type get_argument_type(int p_argument) const {
  147. ERR_FAIL_COND_V(p_argument<-1 || p_argument>argument_count,Variant::NIL);
  148. return argument_types[p_argument+1];
  149. }
  150. PropertyInfo get_argument_info(int p_argument) const;
  151. void set_argument_names(const Vector<StringName>& p_names);
  152. Vector<StringName> get_argument_names() const;
  153. #endif
  154. void set_hint_flags(uint32_t p_hint) { hint_flags=p_hint; }
  155. uint32_t get_hint_flags() const { return hint_flags|(is_const()?METHOD_FLAG_CONST:0); }
  156. virtual String get_instance_type() const=0;
  157. _FORCE_INLINE_ int get_argument_count() const { return argument_count; };
  158. #if 0
  159. _FORCE_INLINE_ Variant call_safe(const Variant** p_args,int p_arg_count, Variant::CallError& r_error) {
  160. r_error.error=Variant::CallError::CALL_OK;
  161. check_call( p_args, &errorarg );
  162. if (!err)
  163. return call(p_object, VARIANT_ARG_PASS );
  164. VARIANT_ARGPTRS
  165. String errstr;
  166. String methodname = get_instance_type()+"::"+name;
  167. if (err==CALL_ERROR_ARGUMENT_TYPE) {
  168. 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())+".";
  169. }
  170. if (err==CALL_ERROR_EXTRA_ARGUMENT) {
  171. errstr="Invalid call. Member function '"+methodname+"' takes "+itos(get_argument_count())+" argument, but argument "+itos(errorarg+1)+" was received.";
  172. }
  173. ERR_PRINT(errstr.ascii().get_data());
  174. return Variant();
  175. }
  176. #endif
  177. virtual Variant call(Object* p_object,const Variant** p_args,int p_arg_count, Variant::CallError& r_error)=0;
  178. StringName get_name() const;
  179. void set_name(const StringName& p_name);
  180. _FORCE_INLINE_ int get_method_id() const { return method_id; }
  181. _FORCE_INLINE_ bool is_const() const { return _const; }
  182. void set_default_arguments(const Vector<Variant>& p_defargs);
  183. MethodBind();
  184. virtual ~MethodBind();
  185. };
  186. template<class T>
  187. class MethodBindNative : public MethodBind {
  188. public:
  189. typedef Variant (T::*NativeCall)(const Variant**,int ,Variant::CallError &);
  190. protected:
  191. NativeCall call_method;
  192. public:
  193. virtual Variant::Type _gen_argument_type(int p_arg) const {
  194. return Variant::NIL;
  195. }
  196. virtual Variant call(Object* p_object,const Variant** p_args,int p_arg_count, Variant::CallError& r_error) {
  197. T* instance=static_cast<T*>(p_object);
  198. return (instance->*call_method)(p_args,p_arg_count,r_error);
  199. }
  200. void set_method_info(const MethodInfo& p_info) {
  201. set_argument_count( p_info.arguments.size() );
  202. #ifdef DEBUG_METHODS_ENABLED
  203. Variant::Type *at = memnew_arr( Variant::Type , p_info.arguments.size()+1 );
  204. at[0]=p_info.return_val.type;
  205. if (p_info.arguments.size()) {
  206. Vector<StringName> names;
  207. names.resize(p_info.arguments.size());
  208. for(int i=0;i<p_info.arguments.size();i++) {
  209. at[i+1]=p_info.arguments[i].type;
  210. names[i]=p_info.arguments[i].name;
  211. }
  212. set_argument_names(names);
  213. }
  214. set_argument_types(at);
  215. #endif
  216. }
  217. void set_method(NativeCall p_method) { call_method=p_method; }
  218. virtual bool is_const() const { return false; }
  219. virtual String get_instance_type() const { return T::get_type_static(); }
  220. MethodBindNative() { call_method=NULL; }
  221. };
  222. template<class T >
  223. MethodBind* create_native_method_bind( Variant (T::*p_method)(const Variant**,int ,Variant::CallError &), const MethodInfo& p_info ) {
  224. MethodBindNative<T > * a = memnew( (MethodBindNative<T >) );
  225. a->set_method(p_method);
  226. a->set_method_info(p_info);
  227. return a;
  228. }
  229. /** This amazing hack is based on the FastDelegates theory */
  230. // tale of an amazing hack.. //
  231. // if you declare an nonexistent class..
  232. class __UnexistingClass;
  233. #include "method_bind.inc"
  234. #endif