wrap.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. // WRAP_MFN_PR for GCC
  5. // GCC has problems with templates, VS with macros, so implementations is different
  6. /*
  7. struct A
  8. {
  9. bool f(int i) { return true; }
  10. };
  11. struct B : public A
  12. {
  13. // f() is inherited
  14. };
  15. template <bool (B::*fp)(int)>
  16. struct WrapF
  17. {
  18. };
  19. WrapF<&B::f> a;
  20. // Doesn't work for any compiler
  21. // Error: non-type template argument of type 'bool (Urho3D::A::*)(int)' cannot be converted to a value of type 'bool (Urho3D::B::*)(int)'
  22. WrapF<static_cast<bool (B::*)(int)>(&B::f)> b;
  23. // Works for VS but not works for GCC
  24. // Error: non-type template argument is not a pointer to member constant
  25. ==================================
  26. MACROS1(a, b, MACROS2)
  27. GCC replaces the parameter MACROS2 first and after that MACROS1
  28. VS replaces MACROS1 first
  29. VS has parameters:
  30. https://docs.microsoft.com/en-us/cpp/preprocessor/preprocessor-experimental-overview
  31. https://docs.microsoft.com/en-us/cpp/build/reference/experimental-preprocessor
  32. but it cause banch of errors in Windows 10 SDK <= 10.0.17763.0
  33. and third party libs
  34. */
  35. #ifndef _MSC_VER
  36. #include <type_traits>
  37. #include <AngelScript/angelscript.h>
  38. //only Boost.Preprocessor is needed, so it may be reasonable to include it in the Urho repository directly
  39. #include <boost/preprocessor.hpp>
  40. //Return void enable if: http://stackoverflow.com/questions/12002447/template-method-enable-if-specialization
  41. //# do with // http://stackoverflow.com/questions/5588855/standard-alternative-to-gccs-va-args-trick
  42. #define EV(...) __VA_ARGS__
  43. #define UNWRAP(...) __VA_ARGS__
  44. #define EXTRACT_CONST(...) (__VA_ARGS__),
  45. // from http://stackoverflow.com/questions/18851889/boost-preprocessor-skip-if-variadic-is-empty
  46. // based on the: http://gustedt.wordpress.com/2010/06/08/detect-empty-macro-arguments
  47. #define __ARG16(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, ...) _15
  48. #define __HAS_COMMA(...) __ARG16(__VA_ARGS__, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0)
  49. #define __TRIGGER_PARENTHESIS_(...) ,
  50. #define __PASTE5(_0, _1, _2, _3, _4) _0 ## _1 ## _2 ## _3 ## _4
  51. #define __IS_EMPTY_CASE_0001 ,
  52. #define __IS_EMPTY(_0, _1, _2, _3) __HAS_COMMA(__PASTE5(__IS_EMPTY_CASE_, _0, _1, _2, _3))
  53. #define TUPLE_IS_EMPTY(...) \
  54. __IS_EMPTY( \
  55. /* test if there is just one argument, eventually an empty one */ \
  56. __HAS_COMMA(__VA_ARGS__), \
  57. /* test if _TRIGGER_PARENTHESIS_ together with the argument adds a comma */ \
  58. __HAS_COMMA(__TRIGGER_PARENTHESIS_ __VA_ARGS__), \
  59. /* test if the argument together with a parenthesis adds a comma */ \
  60. __HAS_COMMA(__VA_ARGS__ (/*empty*/)), \
  61. /* test if placing it between _TRIGGER_PARENTHESIS_ and the parenthesis adds a comma */ \
  62. __HAS_COMMA(__TRIGGER_PARENTHESIS_ __VA_ARGS__ (/*empty*/)) \
  63. )
  64. #define __GEN_EMPTY_ARGS(...)
  65. #define __GEN_NONEMPTY_ARGS_CB(unused, data, idx, elem) \
  66. , static_cast<gw::Proxy <elem> *>(gen->GetAddressOfArg(idx))->value
  67. #define __GEN_NONEMPTY_ARGS(seq) \
  68. BOOST_PP_SEQ_FOR_EACH_I( \
  69. __GEN_NONEMPTY_ARGS_CB \
  70. ,~ \
  71. ,seq \
  72. )
  73. #define CreateGenericFromMethod_2(...) EV(CreateGenericFromMethod_3(__VA_ARGS__))
  74. #define CreateGenericFromMethod_3(class_t,method,parameters,const,return_t) \
  75. asFunctionPtr<asGENFUNC_t>([] (asIScriptGeneric* gen) {\
  76. BOOST_PP_CAT(wrap::call,const) <return_t,class_t BOOST_PP_COMMA_IF(BOOST_PP_IF(TUPLE_IS_EMPTY parameters ,0,1)) EV parameters>\
  77. (gen,/*&(Proxy<class_t>::cast(gen->GetObject()))*/((class_t*)gen->GetObject()), static_cast<return_t (class_t::*) parameters const>(&class_t::method)\
  78. BOOST_PP_IF( \
  79. TUPLE_IS_EMPTY parameters \
  80. ,__GEN_EMPTY_ARGS \
  81. ,__GEN_NONEMPTY_ARGS \
  82. )(BOOST_PP_TUPLE_TO_SEQ( parameters )) \
  83. );\
  84. })
  85. #define WRAP_MFN_PR(class_t,method,parameters,return_t) EV(EV(EV(CreateGenericFromMethod_2(class_t,method,EXTRACT_CONST parameters,return_t))))
  86. namespace wrap
  87. {
  88. template<class R, class C, typename ...P>
  89. void call_helper(::std::false_type, asIScriptGeneric*g, C*obj, R (C::*fn)(P...), P... args)
  90. {
  91. new (g->GetAddressOfReturnLocation()) gw::Proxy<R> ((obj->*fn)(args...));
  92. }
  93. template<class R, class C, typename ...P>
  94. void call_helper(::std::true_type,asIScriptGeneric*g, C*obj, R (C::*fn)(P...), P... args)
  95. {
  96. (obj->*fn)(args...);
  97. }
  98. template<class R, class C, typename ...P>
  99. void call_helper(::std::false_type, asIScriptGeneric*g, C*obj, R (C::*fn)(P...)const, P... args)
  100. {
  101. new (g->GetAddressOfReturnLocation()) gw::Proxy<R> ((obj->*fn)(args...));
  102. }
  103. template<class R, class C, typename ...P>
  104. void call_helper(::std::true_type,asIScriptGeneric*g, C*obj, R (C::*fn)(P...)const, P... args)
  105. {
  106. (obj->*fn)(args...);
  107. }
  108. template<class R, class C, typename ...P>
  109. void call(asIScriptGeneric*g, C*obj, R (C::*fn)(P...), P... args)
  110. {
  111. call_helper<R,C,P...>(::std::is_void<R>(),
  112. g,
  113. obj,
  114. fn,
  115. args...);
  116. }
  117. template<class R, class C, typename ...P>
  118. void callconst(asIScriptGeneric*g, C*obj, R (C::*fn)(P...)const, P... args)
  119. {
  120. call_helper<R,C,P...>(std::is_void<R>(),
  121. g,
  122. obj,
  123. fn,
  124. args...);
  125. }
  126. } // namespace wrap
  127. #endif // ndef _MSC_VER