property_visitor_invoker.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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_PROPERTY_VISITOR_INVOKER_H_
  28. #define RTTR_PROPERTY_VISITOR_INVOKER_H_
  29. #include "rttr/detail/base/core_prerequisites.h"
  30. namespace rttr
  31. {
  32. class method;
  33. namespace detail
  34. {
  35. struct invalid_type;
  36. struct read_only;
  37. /////////////////////////////////////////////////////////////////////////////////////////
  38. /*!
  39. *
  40. */
  41. template<typename T, typename Policy = void>
  42. class property_visitor_invoker
  43. {
  44. private:
  45. using declaring_type_t = typename visitor::property_info<T>::declaring_type;
  46. template<typename U>
  47. using is_global_property = std::is_same<U, invalid_type>;
  48. template<typename U>
  49. using is_read_only = std::is_same<U, read_only>;
  50. /////////////////////////////////////////////////////////////////////////////////////
  51. template<typename U, typename P, typename V>
  52. enable_if_t<!is_global_property<U>::value && !is_read_only<P>::value, void>
  53. call_impl(V& visitor) const
  54. {
  55. visitor.visit_property(m_info);
  56. }
  57. template<typename U, typename P, typename V>
  58. enable_if_t<!is_global_property<U>::value && is_read_only<P>::value, void>
  59. call_impl(V& visitor) const
  60. {
  61. visitor.visit_readonly_property(m_info);
  62. }
  63. template<typename U, typename P, typename V>
  64. enable_if_t<is_global_property<U>::value && !is_read_only<P>::value, void>
  65. call_impl(V& visitor) const
  66. {
  67. visitor.visit_global_property(m_info);
  68. }
  69. template<typename U, typename P, typename V>
  70. enable_if_t<is_global_property<U>::value && is_read_only<P>::value, void>
  71. call_impl(V& visitor) const
  72. {
  73. visitor.visit_global_readonly_property(m_info);
  74. }
  75. /////////////////////////////////////////////////////////////////////////////////////
  76. public:
  77. property_visitor_invoker(const visitor::property_info<T>& info)
  78. : m_info(info)
  79. {
  80. }
  81. template<typename W>
  82. void call(W& visitor) const
  83. {
  84. call_impl<declaring_type_t, Policy>(visitor);
  85. }
  86. private:
  87. const visitor::property_info<T>& m_info;
  88. };
  89. /////////////////////////////////////////////////////////////////////////////////////////
  90. //! \param Policy Specifies whether the property is read only or not.
  91. template<typename Policy = void, typename T = void>
  92. static property_visitor_invoker<T, Policy> make_property_visitor_invoker(const visitor::property_info<T>& info)
  93. {
  94. return property_visitor_invoker<T, Policy>(info);
  95. }
  96. /////////////////////////////////////////////////////////////////////////////////////////
  97. /////////////////////////////////////////////////////////////////////////////////////////
  98. /////////////////////////////////////////////////////////////////////////////////////////
  99. /*!
  100. * The visitor invoker for getter/setter based properties.
  101. */
  102. template<typename T>
  103. class property_getter_setter_visitor_invoker
  104. {
  105. private:
  106. using declaring_type_t = typename visitor::property_getter_setter_info<T>::declaring_type;
  107. template<typename U>
  108. using is_global_property = std::is_same<U, invalid_type>;
  109. /////////////////////////////////////////////////////////////////////////////////////
  110. template<typename U, typename V>
  111. enable_if_t<!is_global_property<U>::value, void>
  112. call_impl(V& visitor) const
  113. {
  114. visitor.visit_getter_setter_property(m_info);
  115. }
  116. template<typename U, typename V>
  117. enable_if_t<is_global_property<U>::value, void>
  118. call_impl(V& visitor) const
  119. {
  120. visitor.visit_global_getter_setter_property(m_info);
  121. }
  122. /////////////////////////////////////////////////////////////////////////////////////
  123. public:
  124. property_getter_setter_visitor_invoker(const visitor::property_getter_setter_info<T>& info)
  125. : m_info(info)
  126. {
  127. }
  128. template<typename V>
  129. void call(V& visitor) const
  130. {
  131. call_impl<declaring_type_t>(visitor);
  132. }
  133. private:
  134. const visitor::property_getter_setter_info<T>& m_info;
  135. };
  136. /////////////////////////////////////////////////////////////////////////////////////////
  137. template<typename T>
  138. static property_getter_setter_visitor_invoker<T>
  139. make_property_getter_setter_visitor_invoker(const visitor::property_getter_setter_info<T>& info)
  140. {
  141. return property_getter_setter_visitor_invoker<T>(info);
  142. }
  143. /////////////////////////////////////////////////////////////////////////////////////////
  144. } // end namespace detail
  145. } // end namespace rttr
  146. #endif // RTTR_PROPERTY_VISITOR_INVOKER_H_