method_invoker.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /************************************************************************************
  2. * *
  3. * Copyright (c) 2014 - 2018 Axel Menzel <info@rttr.org> *
  4. * *
  5. * This file is part of RTTR (Run Time Type Reflection) *
  6. * License: MIT License *
  7. * *
  8. * Permission is hereby granted, free of charge, to any person obtaining *
  9. * a copy of this software and associated documentation files (the "Software"), *
  10. * to deal in the Software without restriction, including without limitation *
  11. * the rights to use, copy, modify, merge, publish, distribute, sublicense, *
  12. * and/or sell copies of the Software, and to permit persons to whom the *
  13. * Software is furnished to do so, subject to the following conditions: *
  14. * *
  15. * The above copyright notice and this permission notice shall be included in *
  16. * all copies or substantial portions of the Software. *
  17. * *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *
  24. * SOFTWARE. *
  25. * *
  26. *************************************************************************************/
  27. #ifndef RTTR_METHOD_INVOKER_H_
  28. #define RTTR_METHOD_INVOKER_H_
  29. #include "rttr/detail/misc/function_traits.h"
  30. #include "rttr/detail/misc/utility.h"
  31. namespace rttr
  32. {
  33. namespace detail
  34. {
  35. /////////////////////////////////////////////////////////////////////////////////////////
  36. template<typename F, typename Policy, typename Method_Type, typename IndexSequence>
  37. struct method_invoker;
  38. template<typename F, std::size_t... ArgCount>
  39. struct method_invoker<F, default_invoke, void_member_func, index_sequence<ArgCount...>>
  40. {
  41. template<typename... TArgs>
  42. RTTR_INLINE static variant invoke(const F& func_ptr, const instance& obj, const TArgs&...args)
  43. {
  44. using class_t = typename function_traits<F>::class_type;
  45. class_t* ptr = obj.try_convert<class_t>();
  46. if (ptr && check_all_true(args.template is_type< param_types_t<F, ArgCount> >()...))
  47. {
  48. (ptr->*func_ptr)(args.template get_value< param_types_t<F, ArgCount> >()...);
  49. return void_variant_type{};
  50. }
  51. else
  52. return variant();
  53. }
  54. };
  55. /////////////////////////////////////////////////////////////////////////////////////////
  56. template<typename F, std::size_t... ArgCount>
  57. struct method_invoker<F, default_invoke, void_func, index_sequence<ArgCount...>>
  58. {
  59. template<typename... TArgs>
  60. RTTR_INLINE static variant invoke(const F& func, const instance& obj, const TArgs&...args)
  61. {
  62. if (check_all_true(args.template is_type< param_types_t<F, ArgCount> >()...))
  63. {
  64. func(args.template get_value< param_types_t<F, ArgCount> >()...);
  65. return void_variant_type{};
  66. }
  67. else
  68. return variant();
  69. }
  70. };
  71. /////////////////////////////////////////////////////////////////////////////////////////
  72. template<typename F, std::size_t... ArgCount>
  73. struct method_invoker<F, default_invoke, return_member_func, index_sequence<ArgCount...>>
  74. {
  75. template<typename... TArgs>
  76. RTTR_INLINE static variant invoke(const F& func_ptr, const instance& obj, const TArgs&...args)
  77. {
  78. using class_t = typename function_traits<F>::class_type;
  79. class_t* ptr = obj.try_convert<class_t>();
  80. if (ptr && check_all_true(args.template is_type< param_types_t<F, ArgCount> >()...))
  81. return (ptr->*func_ptr)(args.template get_value< param_types_t<F, ArgCount> >()...);
  82. else
  83. return variant();
  84. }
  85. };
  86. /////////////////////////////////////////////////////////////////////////////////////////
  87. template<typename F, std::size_t... ArgCount>
  88. struct method_invoker<F, default_invoke, return_func, index_sequence<ArgCount...>>
  89. {
  90. template<typename... TArgs>
  91. RTTR_INLINE static variant invoke(const F& func, const instance& obj, const TArgs&...args)
  92. {
  93. if (check_all_true(args.template is_type< param_types_t<F, ArgCount> >()...))
  94. return func(args.template get_value< param_types_t<F, ArgCount> >()...);
  95. else
  96. return variant();
  97. }
  98. };
  99. /////////////////////////////////////////////////////////////////////////////////////////
  100. template<typename F, std::size_t... ArgCount>
  101. struct method_invoker<F, discard_return, return_member_func, index_sequence<ArgCount...>>
  102. {
  103. template<typename... TArgs>
  104. RTTR_INLINE static variant invoke(const F& func_ptr, const instance& obj, const TArgs&...args)
  105. {
  106. using class_t = typename function_traits<F>::class_type;
  107. class_t* ptr = obj.try_convert<class_t>();
  108. if (ptr && check_all_true(args.template is_type< param_types_t<F, ArgCount> >()...))
  109. {
  110. (ptr->*func_ptr)(args.template get_value< param_types_t<F, ArgCount> >()...);
  111. return void_variant_type{};
  112. }
  113. else
  114. return variant();
  115. }
  116. };
  117. /////////////////////////////////////////////////////////////////////////////////////////
  118. template<typename F, std::size_t... ArgCount>
  119. struct method_invoker<F, discard_return, return_func, index_sequence<ArgCount...>>
  120. {
  121. template<typename... TArgs>
  122. RTTR_INLINE static variant invoke(const F& func, const instance& obj, const TArgs&...args)
  123. {
  124. if (check_all_true(args.template is_type< param_types_t<F, ArgCount> >()...))
  125. {
  126. func(args.template get_value< param_types_t<F, ArgCount> >()...);
  127. return void_variant_type{};
  128. }
  129. else
  130. return variant();
  131. }
  132. };
  133. /////////////////////////////////////////////////////////////////////////////////////////
  134. template<typename F, std::size_t... ArgCount>
  135. struct method_invoker<F, return_as_ptr, return_member_func, index_sequence<ArgCount...>>
  136. {
  137. template<typename... TArgs>
  138. RTTR_INLINE static variant invoke(const F& func_ptr, const instance& obj, const TArgs&...args)
  139. {
  140. using class_t = typename function_traits<F>::class_type;
  141. class_t* ptr = obj.try_convert<class_t>();
  142. if (ptr && check_all_true(args.template is_type< param_types_t<F, ArgCount> >()...))
  143. {
  144. return &(ptr->*func_ptr)(args.template get_value< param_types_t<F, ArgCount> >()...);
  145. }
  146. else
  147. return variant();
  148. }
  149. };
  150. /////////////////////////////////////////////////////////////////////////////////////////
  151. template<typename F, std::size_t... ArgCount>
  152. struct method_invoker<F, return_as_ptr, return_func, index_sequence<ArgCount...>>
  153. {
  154. template<typename... TArgs>
  155. RTTR_INLINE static variant invoke(const F& func, const instance& obj, const TArgs&...args)
  156. {
  157. if (check_all_true(args.template is_type< param_types_t<F, ArgCount> >()...))
  158. {
  159. return &func(args.template get_value< param_types_t<F, ArgCount> >()...);
  160. }
  161. else
  162. return variant();
  163. }
  164. };
  165. /////////////////////////////////////////////////////////////////////////////////////////
  166. } // end namespace detail
  167. } // end namespace rttr
  168. #endif // RTTR_METHOD_INVOKER_H_