wrap.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. //
  2. // Copyright (c) 2008-2022 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. // WRAP_MFN_PR for GCC
  24. // GCC has problems with templates, VS with macros, so implementations is different
  25. /*
  26. struct A
  27. {
  28. bool f(int i) { return true; }
  29. };
  30. struct B : public A
  31. {
  32. // f() is inherited
  33. };
  34. template <bool (B::*fp)(int)>
  35. struct WrapF
  36. {
  37. };
  38. WrapF<&B::f> a;
  39. // Doesn't work for any compiler
  40. // Error: non-type template argument of type 'bool (Urho3D::A::*)(int)' cannot be converted to a value of type 'bool (Urho3D::B::*)(int)'
  41. WrapF<static_cast<bool (B::*)(int)>(&B::f)> b;
  42. // Works for VS but not works for GCC
  43. // Error: non-type template argument is not a pointer to member constant
  44. ==================================
  45. MACROS1(a, b, MACROS2)
  46. GCC replaces the parameter MACROS2 first and after that MACROS1
  47. VS replaces MACROS1 first
  48. VS has parameters:
  49. https://docs.microsoft.com/en-us/cpp/preprocessor/preprocessor-experimental-overview
  50. https://docs.microsoft.com/en-us/cpp/build/reference/experimental-preprocessor
  51. but it cause banch of errors in Windows 10 SDK <= 10.0.17763.0
  52. and third party libs
  53. */
  54. #ifndef _MSC_VER
  55. #include <type_traits>
  56. #include <AngelScript/angelscript.h>
  57. //only Boost.Preprocessor is needed, so it may be reasonable to include it in the Urho repository directly
  58. #include <boost/preprocessor.hpp>
  59. //Return void enable if: http://stackoverflow.com/questions/12002447/template-method-enable-if-specialization
  60. //# do with // http://stackoverflow.com/questions/5588855/standard-alternative-to-gccs-va-args-trick
  61. #define EV(...) __VA_ARGS__
  62. #define UNWRAP(...) __VA_ARGS__
  63. #define EXTRACT_CONST(...) (__VA_ARGS__),
  64. // from http://stackoverflow.com/questions/18851889/boost-preprocessor-skip-if-variadic-is-empty
  65. // based on the: http://gustedt.wordpress.com/2010/06/08/detect-empty-macro-arguments
  66. #define __ARG16(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, ...) _15
  67. #define __HAS_COMMA(...) __ARG16(__VA_ARGS__, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0)
  68. #define __TRIGGER_PARENTHESIS_(...) ,
  69. #define __PASTE5(_0, _1, _2, _3, _4) _0 ## _1 ## _2 ## _3 ## _4
  70. #define __IS_EMPTY_CASE_0001 ,
  71. #define __IS_EMPTY(_0, _1, _2, _3) __HAS_COMMA(__PASTE5(__IS_EMPTY_CASE_, _0, _1, _2, _3))
  72. #define TUPLE_IS_EMPTY(...) \
  73. __IS_EMPTY( \
  74. /* test if there is just one argument, eventually an empty one */ \
  75. __HAS_COMMA(__VA_ARGS__), \
  76. /* test if _TRIGGER_PARENTHESIS_ together with the argument adds a comma */ \
  77. __HAS_COMMA(__TRIGGER_PARENTHESIS_ __VA_ARGS__), \
  78. /* test if the argument together with a parenthesis adds a comma */ \
  79. __HAS_COMMA(__VA_ARGS__ (/*empty*/)), \
  80. /* test if placing it between _TRIGGER_PARENTHESIS_ and the parenthesis adds a comma */ \
  81. __HAS_COMMA(__TRIGGER_PARENTHESIS_ __VA_ARGS__ (/*empty*/)) \
  82. )
  83. #define __GEN_EMPTY_ARGS(...)
  84. #define __GEN_NONEMPTY_ARGS_CB(unused, data, idx, elem) \
  85. , static_cast<gw::Proxy <elem> *>(gen->GetAddressOfArg(idx))->value
  86. #define __GEN_NONEMPTY_ARGS(seq) \
  87. BOOST_PP_SEQ_FOR_EACH_I( \
  88. __GEN_NONEMPTY_ARGS_CB \
  89. ,~ \
  90. ,seq \
  91. )
  92. #define CreateGenericFromMethod_2(...) EV(CreateGenericFromMethod_3(__VA_ARGS__))
  93. #define CreateGenericFromMethod_3(class_t,method,parameters,const,return_t) \
  94. asFunctionPtr<asGENFUNC_t>([] (asIScriptGeneric* gen) {\
  95. 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>\
  96. (gen,/*&(Proxy<class_t>::cast(gen->GetObject()))*/((class_t*)gen->GetObject()), static_cast<return_t (class_t::*) parameters const>(&class_t::method)\
  97. BOOST_PP_IF( \
  98. TUPLE_IS_EMPTY parameters \
  99. ,__GEN_EMPTY_ARGS \
  100. ,__GEN_NONEMPTY_ARGS \
  101. )(BOOST_PP_TUPLE_TO_SEQ( parameters )) \
  102. );\
  103. })
  104. #define WRAP_MFN_PR(class_t,method,parameters,return_t) EV(EV(EV(CreateGenericFromMethod_2(class_t,method,EXTRACT_CONST parameters,return_t))))
  105. namespace wrap
  106. {
  107. template<class R, class C, typename ...P>
  108. void call_helper(::std::false_type, asIScriptGeneric*g, C*obj, R (C::*fn)(P...), P... args)
  109. {
  110. new (g->GetAddressOfReturnLocation()) gw::Proxy<R> ((obj->*fn)(args...));
  111. }
  112. template<class R, class C, typename ...P>
  113. void call_helper(::std::true_type,asIScriptGeneric*g, C*obj, R (C::*fn)(P...), P... args)
  114. {
  115. (obj->*fn)(args...);
  116. }
  117. template<class R, class C, typename ...P>
  118. void call_helper(::std::false_type, asIScriptGeneric*g, C*obj, R (C::*fn)(P...)const, P... args)
  119. {
  120. new (g->GetAddressOfReturnLocation()) gw::Proxy<R> ((obj->*fn)(args...));
  121. }
  122. template<class R, class C, typename ...P>
  123. void call_helper(::std::true_type,asIScriptGeneric*g, C*obj, R (C::*fn)(P...)const, P... args)
  124. {
  125. (obj->*fn)(args...);
  126. }
  127. template<class R, class C, typename ...P>
  128. void call(asIScriptGeneric*g, C*obj, R (C::*fn)(P...), P... args)
  129. {
  130. call_helper<R,C,P...>(::std::is_void<R>(),
  131. g,
  132. obj,
  133. fn,
  134. args...);
  135. }
  136. template<class R, class C, typename ...P>
  137. void callconst(asIScriptGeneric*g, C*obj, R (C::*fn)(P...)const, P... args)
  138. {
  139. call_helper<R,C,P...>(std::is_void<R>(),
  140. g,
  141. obj,
  142. fn,
  143. args...);
  144. }
  145. } // namespace wrap
  146. #endif // ndef _MSC_VER