diff options
author | Juan Linietsky <reduzio@gmail.com> | 2016-06-22 23:12:20 -0300 |
---|---|---|
committer | Juan Linietsky <reduzio@gmail.com> | 2016-06-22 23:13:41 -0300 |
commit | d6225b1e0004c57cc50452ddb5d512fd6556a523 (patch) | |
tree | eeb6169dcc735c6b66052581b81bb5a71b466509 /core | |
parent | 842e7bfc2f81cda73fd9f16a1ade323a1a5d0292 (diff) |
Improved binding system (ObjectTypeDB::bind_method) to be friendlier to statically typed languages, should help in the Mono integration.
Disabled by default.
Diffstat (limited to 'core')
-rw-r--r-- | core/io/http_client.cpp | 2 | ||||
-rw-r--r-- | core/io/http_client.h | 1 | ||||
-rw-r--r-- | core/make_binders.py | 15 | ||||
-rw-r--r-- | core/math/bsp_tree.h | 27 | ||||
-rw-r--r-- | core/math/face3.h | 2 | ||||
-rw-r--r-- | core/method_bind.h | 40 | ||||
-rw-r--r-- | core/method_ptrcall.h | 354 | ||||
-rw-r--r-- | core/reference.h | 56 |
8 files changed, 492 insertions, 5 deletions
diff --git a/core/io/http_client.cpp b/core/io/http_client.cpp index 0dfae6584b..2a831dd992 100644 --- a/core/io/http_client.cpp +++ b/core/io/http_client.cpp @@ -29,8 +29,6 @@ #include "http_client.h" #include "io/stream_peer_ssl.h" -VARIANT_ENUM_CAST(HTTPClient::Status); - Error HTTPClient::connect(const String &p_host, int p_port, bool p_ssl,bool p_verify_host){ diff --git a/core/io/http_client.h b/core/io/http_client.h index ceb0273a7d..32d2e72101 100644 --- a/core/io/http_client.h +++ b/core/io/http_client.h @@ -201,5 +201,6 @@ public: }; VARIANT_ENUM_CAST(HTTPClient::Method); +VARIANT_ENUM_CAST(HTTPClient::Status); #endif // HTTP_CLIENT_H diff --git a/core/make_binders.py b/core/make_binders.py index 93371dc0a3..1d55c92b89 100644 --- a/core/make_binders.py +++ b/core/make_binders.py @@ -48,7 +48,13 @@ public: $ifnoret return Variant();$ } - +#ifdef PTRCAL_ENABLED + virtual void ptrcall(Object*p_object,const void** p_args,void *r_ret) { + + T *instance=p_object->cast_to<T>(); + $ifret PtrToArg<R>::encode( $ (instance->*method)($arg, PtrToArg<P@>::convert(p_args[@-1])$) $ifret ,r_ret)$ ; + } +#endif MethodBind$argc$$ifret R$$ifconst C$ () { #ifdef DEBUG_METHODS_ENABLED _set_const($ifconst true$$ifnoconst false$); @@ -121,7 +127,12 @@ public: $ifret return Variant(ret);$ $ifnoret return Variant();$ } - +#ifdef PTRCALL_ENABLED + virtual void ptrcall(Object*p_object,const void** p_args,void *r_ret) { + __UnexistingClass *instance = (__UnexistingClass*)p_object; + $ifret PtrToArg<R>::encode( $ (instance->*method)($arg, PtrToArg<P@>::convert(p_args[@-1])$) $ifret ,r_ret) $ ; + } +#endif MethodBind$argc$$ifret R$$ifconst C$ () { #ifdef DEBUG_METHODS_ENABLED _set_const($ifconst true$$ifnoconst false$); diff --git a/core/math/bsp_tree.h b/core/math/bsp_tree.h index 2bfc26b51e..6c36d80e3e 100644 --- a/core/math/bsp_tree.h +++ b/core/math/bsp_tree.h @@ -34,8 +34,8 @@ #include "face3.h" #include "vector.h" #include "dvector.h" - #include "variant.h" +#include "method_ptrcall.h" /** @author Juan Linietsky <reduzio@gmail.com> */ @@ -138,4 +138,29 @@ bool BSP_Tree::convex_is_inside(const T& p_convex) const { } +#ifdef PTRCALL_ENABLED + + +template<> +struct PtrToArg<BSP_Tree> { + _FORCE_INLINE_ static BSP_Tree convert(const void* p_ptr) { + BSP_Tree s( Variant( *reinterpret_cast<const Dictionary*>(p_ptr) ) ); + return s; + } + _FORCE_INLINE_ static void encode(BSP_Tree p_val,void* p_ptr) { + Dictionary *d = reinterpret_cast<Dictionary*>(p_ptr); + *d=Variant(p_val); + } +}; + +template<> +struct PtrToArg<const BSP_Tree&> { + _FORCE_INLINE_ static BSP_Tree convert(const void* p_ptr) { + BSP_Tree s( Variant( *reinterpret_cast<const Dictionary*>(p_ptr) ) ); + return s; + } +}; + +#endif + #endif diff --git a/core/math/face3.h b/core/math/face3.h index bc34be9935..3a81da74db 100644 --- a/core/math/face3.h +++ b/core/math/face3.h @@ -264,4 +264,6 @@ bool Face3::intersects_aabb2(const AABB& p_aabb) const { } +//this sucks... + #endif // FACE3_H diff --git a/core/method_bind.h b/core/method_bind.h index 48848c5848..30a848270d 100644 --- a/core/method_bind.h +++ b/core/method_bind.h @@ -33,6 +33,7 @@ #include "variant.h" #include "object.h" #include <stdio.h> +#include "method_ptrcall.h" /** @author Juan Linietsky <reduzio@gmail.com> @@ -85,6 +86,32 @@ struct VariantCaster<const T&> { (VariantCaster<P##m_idx>::cast( (m_idx-1)>=p_arg_count?get_default_argument(m_idx-1):*p_args[m_idx-1] )) //SIMPLE_NUMERIC_TYPE is used to avoid a warning on Variant::get_type_for + +#ifdef PTRCALL_ENABLED + + +#define VARIANT_ENUM_CAST( m_enum ) \ +SIMPLE_NUMERIC_TYPE( m_enum );\ +template<> \ +struct VariantCaster<m_enum> {\ +\ + static _FORCE_INLINE_ m_enum cast(const Variant& p_variant) {\ + return (m_enum)p_variant.operator int();\ + }\ +};\ +template<>\ +struct PtrToArg< m_enum > {\ + _FORCE_INLINE_ static m_enum convert(const void* p_ptr) {\ + return m_enum(*reinterpret_cast<const int*>(p_ptr));\ + }\ + _FORCE_INLINE_ static void encode(m_enum p_val,const void* p_ptr) {\ + *(int*)p_ptr=p_val;\ + }\ +}; + +#else + + #define VARIANT_ENUM_CAST( m_enum ) \ SIMPLE_NUMERIC_TYPE( m_enum );\ template<> \ @@ -96,6 +123,9 @@ struct VariantCaster<m_enum> {\ }; +#endif + + #define CHECK_ARG(m_arg)\ if ((m_arg-1)<p_arg_count) {\ Variant::Type argtype=get_argument_type(m_arg-1);\ @@ -222,6 +252,11 @@ public: } #endif virtual Variant call(Object* p_object,const Variant** p_args,int p_arg_count, Variant::CallError& r_error)=0; + +#ifdef PTRCALL_ENABLED + virtual void ptrcall(Object* p_object,const void** p_args,void* r_ret)=0; +#endif + StringName get_name() const; void set_name(const StringName& p_name); _FORCE_INLINE_ int get_method_id() const { return method_id; } @@ -277,6 +312,11 @@ public: #endif } +#ifdef PTRCALL_ENABLED + virtual void ptrcall(Object* p_object,const void** p_args,void* r_ret) {} //todo +#endif + + void set_method(NativeCall p_method) { call_method=p_method; } virtual bool is_const() const { return false; } virtual String get_instance_type() const { return T::get_type_static(); } diff --git a/core/method_ptrcall.h b/core/method_ptrcall.h new file mode 100644 index 0000000000..e38d59fd8f --- /dev/null +++ b/core/method_ptrcall.h @@ -0,0 +1,354 @@ +#ifndef METHOD_PTRCALL_H +#define METHOD_PTRCALL_H + +#include "typedefs.h" +#include "math_2d.h" +#include "variant.h" + +#ifdef PTRCALL_ENABLED + +template<class T> +struct PtrToArg { + +}; + +#define MAKE_PTRARG(m_type) \ +template<>\ +struct PtrToArg<m_type> {\ + _FORCE_INLINE_ static m_type convert(const void* p_ptr) {\ + return *reinterpret_cast<const m_type*>(p_ptr);\ + }\ + _FORCE_INLINE_ static void encode(m_type p_val, void* p_ptr) {\ + *((m_type*)p_ptr)=p_val;\ + }\ +};\ +template<>\ +struct PtrToArg<const m_type&> {\ + _FORCE_INLINE_ static m_type convert(const void* p_ptr) {\ + return *reinterpret_cast<const m_type*>(p_ptr);\ + }\ + _FORCE_INLINE_ static void encode(m_type p_val, void* p_ptr) {\ + *((m_type*)p_ptr)=p_val;\ + }\ +} + + +#define MAKE_PTRARGR(m_type,m_ret) \ +template<>\ +struct PtrToArg<m_type> {\ + _FORCE_INLINE_ static m_type convert(const void* p_ptr) {\ + return *reinterpret_cast<const m_type*>(p_ptr);\ + }\ + _FORCE_INLINE_ static void encode(m_type p_val, void* p_ptr) {\ + *((m_ret*)p_ptr)=p_val;\ + }\ +};\ +template<>\ +struct PtrToArg<const m_type&> {\ + _FORCE_INLINE_ static m_type convert(const void* p_ptr) {\ + return *reinterpret_cast<const m_type*>(p_ptr);\ + }\ + _FORCE_INLINE_ static void encode(m_type p_val, void* p_ptr) {\ + *((m_ret*)p_ptr)=p_val;\ + }\ +} + + + +MAKE_PTRARG(bool); +MAKE_PTRARGR(uint8_t,int); +MAKE_PTRARGR(int8_t,int); +MAKE_PTRARGR(uint16_t,int); +MAKE_PTRARGR(int16_t,int); +MAKE_PTRARGR(uint32_t,int); +MAKE_PTRARGR(int32_t,int); +MAKE_PTRARGR(int64_t,int); +MAKE_PTRARGR(uint64_t,int); +MAKE_PTRARG(float); +MAKE_PTRARGR(double,float); + +MAKE_PTRARG(String); +MAKE_PTRARG(Vector2); +MAKE_PTRARG(Rect2); +MAKE_PTRARG(Vector3); +MAKE_PTRARG(Matrix32); +MAKE_PTRARG(Plane); +MAKE_PTRARG(Quat); +MAKE_PTRARG(AABB); +MAKE_PTRARG(Matrix3); +MAKE_PTRARG(Transform); +MAKE_PTRARG(Color); +MAKE_PTRARG(Image); +MAKE_PTRARG(NodePath); +MAKE_PTRARG(RID); +MAKE_PTRARG(InputEvent); +MAKE_PTRARG(Dictionary); +MAKE_PTRARG(Array); +MAKE_PTRARG(ByteArray); +MAKE_PTRARG(IntArray); +MAKE_PTRARG(RealArray); +MAKE_PTRARG(StringArray); +MAKE_PTRARG(Vector2Array); +MAKE_PTRARG(Vector3Array); +MAKE_PTRARG(ColorArray); +MAKE_PTRARG(Variant); + + +//this is for Object + +template<class T> +struct PtrToArg< T* > { + + _FORCE_INLINE_ static T* convert(const void* p_ptr) { + + return const_cast<T*>(reinterpret_cast<const T*>(p_ptr)); + } + + _FORCE_INLINE_ static void encode(T* p_var, void* p_ptr) { + + *((T**)p_ptr)=p_var; + } + +}; + +template<class T> +struct PtrToArg< const T* > { + + _FORCE_INLINE_ static const T* convert(const void* p_ptr) { + + return reinterpret_cast<const T*>(p_ptr); + } + + _FORCE_INLINE_ static void encode(T* p_var, void* p_ptr) { + + *((T**)p_ptr)=p_var; + } + +}; + + +//this is for the special cases used by Variant + +#define MAKE_VECARG(m_type) \ +template<>\ +struct PtrToArg<Vector<m_type> > {\ + _FORCE_INLINE_ static Vector<m_type> convert(const void* p_ptr) {\ + const DVector<m_type> *dvs = reinterpret_cast<const DVector<m_type> *>(p_ptr);\ + Vector<m_type> ret;\ + int len = dvs->size();\ + ret.resize(len);\ + {\ + DVector<m_type>::Read r=dvs->read();\ + for(int i=0;i<len;i++) {\ + ret[i]=r[i];\ + }\ + } \ + return ret;\ + }\ + _FORCE_INLINE_ static void encode(Vector<m_type> p_vec, void* p_ptr) {\ + DVector<m_type> *dv = reinterpret_cast<DVector<m_type> *>(p_ptr);\ + int len=p_vec.size();\ + dv->resize(len);\ + {\ + DVector<m_type>::Write w=dv->write();\ + for(int i=0;i<len;i++) {\ + w[i]=p_vec[i];\ + }\ + } \ + }\ +};\ +template<>\ +struct PtrToArg<const Vector<m_type>& > {\ + _FORCE_INLINE_ static Vector<m_type> convert(const void* p_ptr) {\ + const DVector<m_type> *dvs = reinterpret_cast<const DVector<m_type> *>(p_ptr);\ + Vector<m_type> ret;\ + int len = dvs->size();\ + ret.resize(len);\ + {\ + DVector<m_type>::Read r=dvs->read();\ + for(int i=0;i<len;i++) {\ + ret[i]=r[i];\ + }\ + } \ + return ret;\ + }\ +} + +MAKE_VECARG(String); +MAKE_VECARG(uint8_t); +MAKE_VECARG(int); +MAKE_VECARG(float); +MAKE_VECARG(Vector2); +MAKE_VECARG(Vector3); +MAKE_VECARG(Color); + +//for stuff that gets converted to Array vectors +#define MAKE_VECARR(m_type) \ +template<>\ +struct PtrToArg<Vector<m_type> > {\ + _FORCE_INLINE_ static Vector<m_type> convert(const void* p_ptr) {\ + const Array *arr = reinterpret_cast<const Array *>(p_ptr);\ + Vector<m_type> ret;\ + int len = arr->size();\ + ret.resize(len);\ + for(int i=0;i<len;i++) {\ + ret[i]=(*arr)[i];\ + }\ + return ret;\ + }\ + _FORCE_INLINE_ static void encode(Vector<m_type> p_vec, void* p_ptr) {\ + Array *arr = reinterpret_cast<Array *>(p_ptr);\ + int len = p_vec.size();\ + arr->resize(len);\ + for(int i=0;i<len;i++) {\ + (*arr)[i]=p_vec[i];\ + }\ + } \ +};\ +template<>\ +struct PtrToArg<const Vector<m_type>& > {\ + _FORCE_INLINE_ static Vector<m_type> convert(const void* p_ptr) {\ + const Array *arr = reinterpret_cast<const Array *>(p_ptr);\ + Vector<m_type> ret;\ + int len = arr->size();\ + ret.resize(len);\ + for(int i=0;i<len;i++) {\ + ret[i]=(*arr)[i];\ + }\ + return ret;\ + }\ +} + + +MAKE_VECARR(Variant); +MAKE_VECARR(RID); +MAKE_VECARR(Plane); + +#define MAKE_DVECARR(m_type) \ +template<>\ +struct PtrToArg<DVector<m_type> > {\ + _FORCE_INLINE_ static DVector<m_type> convert(const void* p_ptr) {\ + const Array *arr = reinterpret_cast<const Array *>(p_ptr);\ + DVector<m_type> ret;\ + int len = arr->size();\ + ret.resize(len);\ + {\ + DVector<m_type>::Write w=ret.write();\ + for(int i=0;i<len;i++) {\ + w[i]=(*arr)[i];\ + }\ + }\ + return ret;\ + }\ + _FORCE_INLINE_ static void encode(DVector<m_type> p_vec, void* p_ptr) {\ + Array *arr = reinterpret_cast<Array *>(p_ptr);\ + int len = p_vec.size();\ + arr->resize(len);\ + {\ + DVector<m_type>::Read r=p_vec.read();\ + for(int i=0;i<len;i++) {\ + (*arr)[i]=r[i];\ + }\ + }\ + } \ +};\ +template<>\ +struct PtrToArg<const DVector<m_type>& > {\ + _FORCE_INLINE_ static DVector<m_type> convert(const void* p_ptr) {\ + const Array *arr = reinterpret_cast<const Array *>(p_ptr);\ + DVector<m_type> ret;\ + int len = arr->size();\ + ret.resize(len);\ + {\ + DVector<m_type>::Write w=ret.write();\ + for(int i=0;i<len;i++) {\ + w[i]=(*arr)[i];\ + }\ + }\ + return ret;\ + }\ +} + +MAKE_DVECARR(Plane); +//for special case StringName + +#define MAKE_STRINGCONV(m_type) \ +template<>\ +struct PtrToArg<m_type> {\ + _FORCE_INLINE_ static m_type convert(const void* p_ptr) {\ + m_type s = *reinterpret_cast<const String*>(p_ptr);\ + return s;\ + }\ + _FORCE_INLINE_ static void encode(m_type p_vec, void* p_ptr) {\ + String *arr = reinterpret_cast<String *>(p_ptr);\ + *arr=p_vec;\ + }\ +};\ +\ +template<>\ +struct PtrToArg<const m_type&> {\ + _FORCE_INLINE_ static m_type convert(const void* p_ptr) {\ + m_type s = *reinterpret_cast<const String*>(p_ptr);\ + return s;\ + }\ +} + +MAKE_STRINGCONV(StringName); +MAKE_STRINGCONV(IP_Address); + +template<> +struct PtrToArg<DVector<Face3> > { + _FORCE_INLINE_ static DVector<Face3> convert(const void* p_ptr) { + const DVector<Vector3> *dvs = reinterpret_cast<const DVector<Vector3> *>(p_ptr); + DVector<Face3> ret; + int len = dvs->size()/3; + ret.resize(len); + { + DVector<Vector3>::Read r=dvs->read(); + DVector<Face3>::Write w=ret.write(); + for(int i=0;i<len;i++) { + w[i].vertex[0]=r[i*3+0]; + w[i].vertex[1]=r[i*3+1]; + w[i].vertex[2]=r[i*3+2]; + } + } + return ret; + } + _FORCE_INLINE_ static void encode(DVector<Face3> p_vec, void* p_ptr) {\ + DVector<Vector3> *arr = reinterpret_cast<DVector<Vector3> *>(p_ptr);\ + int len = p_vec.size();\ + arr->resize(len*3);\ + {\ + DVector<Face3>::Read r=p_vec.read();\ + DVector<Vector3>::Write w=arr->write();\ + for(int i=0;i<len;i++) {\ + w[i*3+0]=r[i].vertex[0];\ + w[i*3+1]=r[i].vertex[1];\ + w[i*3+2]=r[i].vertex[2];\ + }\ + }\ + } \ +}; +template<> +struct PtrToArg<const DVector<Face3>& > { + _FORCE_INLINE_ static DVector<Face3> convert(const void* p_ptr) { + const DVector<Vector3> *dvs = reinterpret_cast<const DVector<Vector3> *>(p_ptr); + DVector<Face3> ret; + int len = dvs->size()/3; + ret.resize(len); + { + DVector<Vector3>::Read r=dvs->read(); + DVector<Face3>::Write w=ret.write(); + for(int i=0;i<len;i++) { + w[i].vertex[0]=r[i*3+0]; + w[i].vertex[1]=r[i*3+1]; + w[i].vertex[2]=r[i*3+2]; + } + } + return ret; + } +}; + + +#endif // METHOD_PTRCALL_H +#endif diff --git a/core/reference.h b/core/reference.h index 8bfbf19ab6..60a256dc99 100644 --- a/core/reference.h +++ b/core/reference.h @@ -329,6 +329,62 @@ public: WeakRef(); }; +#ifdef PTRCALL_ENABLED + +template<class T> +struct PtrToArg< Ref<T> > { + + _FORCE_INLINE_ static Ref<T> convert(const void* p_ptr) { + + return Ref<T>(reinterpret_cast<const T*>(p_ptr)); + } + + _FORCE_INLINE_ static void encode(Ref<T> p_val,const void* p_ptr) { + + *((T**)p_ptr)=p_val.ptr(); + } + +}; + + +template<class T> +struct PtrToArg< const Ref<T>& > { + + _FORCE_INLINE_ static Ref<T> convert(const void* p_ptr) { + + return Ref<T>(reinterpret_cast<const T*>(p_ptr)); + } + +}; +//this is for RefPtr + +template<> +struct PtrToArg< RefPtr > { + + _FORCE_INLINE_ static RefPtr convert(const void* p_ptr) { + + return Ref<Reference>(reinterpret_cast<const Reference*>(p_ptr)).get_ref_ptr(); + } + + _FORCE_INLINE_ static void encode(RefPtr p_val,const void* p_ptr) { + + Ref<Reference> r = p_val; + *((Reference**)p_ptr)=r.ptr(); + } + +}; + +template<> +struct PtrToArg< const RefPtr& > { + + _FORCE_INLINE_ static RefPtr convert(const void* p_ptr) { + + return Ref<Reference>(reinterpret_cast<const Reference*>(p_ptr)).get_ref_ptr(); + } + +}; + +#endif #endif // REFERENCE_H |