wrap.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //
  2. // Copyright (c) 2008-2017 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. #include <type_traits>
  24. #include "angelscript.h"
  25. //only Boost.Preprocessor is needed, so it may be reasonable to include it in the Urho repository directly
  26. #include <boost/preprocessor.hpp>
  27. //Return void enable if: http://stackoverflow.com/questions/12002447/template-method-enable-if-specialization
  28. //# do with // http://stackoverflow.com/questions/5588855/standard-alternative-to-gccs-va-args-trick
  29. #define EV(...) __VA_ARGS__
  30. #define UNWRAP(...) __VA_ARGS__
  31. #define EXTRACT_CONST(...) (__VA_ARGS__),
  32. // from http://stackoverflow.com/questions/18851889/boost-preprocessor-skip-if-variadic-is-empty
  33. // based on the: http://gustedt.wordpress.com/2010/06/08/detect-empty-macro-arguments
  34. #define __ARG16(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, ...) _15
  35. #define __HAS_COMMA(...) __ARG16(__VA_ARGS__, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0)
  36. #define __TRIGGER_PARENTHESIS_(...) ,
  37. #define __PASTE5(_0, _1, _2, _3, _4) _0 ## _1 ## _2 ## _3 ## _4
  38. #define __IS_EMPTY_CASE_0001 ,
  39. #define __IS_EMPTY(_0, _1, _2, _3) __HAS_COMMA(__PASTE5(__IS_EMPTY_CASE_, _0, _1, _2, _3))
  40. #define TUPLE_IS_EMPTY(...) \
  41. __IS_EMPTY( \
  42. /* test if there is just one argument, eventually an empty one */ \
  43. __HAS_COMMA(__VA_ARGS__), \
  44. /* test if _TRIGGER_PARENTHESIS_ together with the argument adds a comma */ \
  45. __HAS_COMMA(__TRIGGER_PARENTHESIS_ __VA_ARGS__), \
  46. /* test if the argument together with a parenthesis adds a comma */ \
  47. __HAS_COMMA(__VA_ARGS__ (/*empty*/)), \
  48. /* test if placing it between _TRIGGER_PARENTHESIS_ and the parenthesis adds a comma */ \
  49. __HAS_COMMA(__TRIGGER_PARENTHESIS_ __VA_ARGS__ (/*empty*/)) \
  50. )
  51. #define __GEN_EMPTY_ARGS(...)
  52. #define __GEN_NONEMPTY_ARGS_CB(unused, data, idx, elem) \
  53. , static_cast<gw::Proxy <elem> *>(gen->GetAddressOfArg(idx))->value
  54. #define __GEN_NONEMPTY_ARGS(seq) \
  55. BOOST_PP_SEQ_FOR_EACH_I( \
  56. __GEN_NONEMPTY_ARGS_CB \
  57. ,~ \
  58. ,seq \
  59. )
  60. #define CreateGenericFromMethod_2(...) EV(CreateGenericFromMethod_3(__VA_ARGS__))
  61. #define CreateGenericFromMethod_3(class_t,method,parameters,const,return_t) \
  62. asFunctionPtr<asGENFUNC_t>([] (asIScriptGeneric* gen) {\
  63. 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>\
  64. (gen,/*&(Proxy<class_t>::cast(gen->GetObject()))*/((class_t*)gen->GetObject()), static_cast<return_t (class_t::*) parameters const>(&class_t::method)\
  65. BOOST_PP_IF( \
  66. TUPLE_IS_EMPTY parameters \
  67. ,__GEN_EMPTY_ARGS \
  68. ,__GEN_NONEMPTY_ARGS \
  69. )(BOOST_PP_TUPLE_TO_SEQ( parameters )) \
  70. );\
  71. })
  72. #define WRAP_MFN_PR(class_t,method,parameters,return_t) EV(EV(EV(CreateGenericFromMethod_2(class_t,method,EXTRACT_CONST parameters,return_t))))
  73. namespace wrap
  74. {
  75. template<class R, class C, typename ...P>
  76. void call_helper(::std::false_type, asIScriptGeneric*g, C*obj, R (C::*fn)(P...), P... args)
  77. {
  78. new (g->GetAddressOfReturnLocation()) gw::Proxy<R> ((obj->*fn)(args...));
  79. }
  80. template<class R, class C, typename ...P>
  81. void call_helper(::std::true_type,asIScriptGeneric*g, C*obj, R (C::*fn)(P...), P... args)
  82. {
  83. (obj->*fn)(args...);
  84. }
  85. template<class R, class C, typename ...P>
  86. void call_helper(::std::false_type, asIScriptGeneric*g, C*obj, R (C::*fn)(P...)const, P... args)
  87. {
  88. new (g->GetAddressOfReturnLocation()) gw::Proxy<R> ((obj->*fn)(args...));
  89. }
  90. template<class R, class C, typename ...P>
  91. void call_helper(::std::true_type,asIScriptGeneric*g, C*obj, R (C::*fn)(P...)const, P... args)
  92. {
  93. (obj->*fn)(args...);
  94. }
  95. template<class R, class C, typename ...P>
  96. void call(asIScriptGeneric*g, C*obj, R (C::*fn)(P...), P... args)
  97. {
  98. call_helper<R,C,P...>(::std::is_void<R>(),
  99. g,
  100. obj,
  101. fn,
  102. args...);
  103. }
  104. template<class R, class C, typename ...P>
  105. void callconst(asIScriptGeneric*g, C*obj, R (C::*fn)(P...)const, P... args)
  106. {
  107. call_helper<R,C,P...>(std::is_void<R>(),
  108. g,
  109. obj,
  110. fn,
  111. args...);
  112. }
  113. } // namespace wrap