variant_sequential_view_private.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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_VARIANT_SEQUENTIAL_VIEW_PRIVATE_H_
  28. #define RTTR_VARIANT_SEQUENTIAL_VIEW_PRIVATE_H_
  29. #include "rttr/variant.h"
  30. #include "rttr/argument.h"
  31. #include "rttr/instance.h"
  32. #include "rttr/sequential_mapper.h"
  33. namespace rttr
  34. {
  35. namespace detail
  36. {
  37. /////////////////////////////////////////////////////////////////////////////////////////
  38. class RTTR_LOCAL variant_sequential_view_private
  39. {
  40. public:
  41. variant_sequential_view_private() RTTR_NOEXCEPT
  42. : m_type(get_invalid_type()),
  43. m_value_type(get_invalid_type()),
  44. m_container(nullptr),
  45. m_get_is_empty_func(sequential_container_empty::is_empty),
  46. m_get_size_func(sequential_container_empty::get_size),
  47. m_set_size_func(sequential_container_empty::set_size),
  48. m_is_dynamic_func(sequential_container_empty::is_dynamic),
  49. m_get_rank_func(sequential_container_empty::get_rank),
  50. m_get_rank_type_func(sequential_container_empty::get_rank_type),
  51. m_begin_func(sequential_container_empty::begin),
  52. m_end_func(sequential_container_empty::begin),
  53. m_equal_func(sequential_container_empty::equal),
  54. m_create_func(sequential_container_empty::create),
  55. m_delete_func(sequential_container_empty::destroy),
  56. m_get_data_func(sequential_container_empty::get_data),
  57. m_advance_func(sequential_container_empty::advance),
  58. m_erase_func(sequential_container_empty::erase),
  59. m_clear_func(sequential_container_empty::clear),
  60. m_insert_func(sequential_container_empty::insert),
  61. m_set_value_func(sequential_container_empty::set_value),
  62. m_get_value_func(sequential_container_empty::get_value)
  63. {
  64. }
  65. template<typename T, typename RawType = raw_type_t<T>, typename ConstType = remove_pointer_t<T>>
  66. variant_sequential_view_private(const T& container) RTTR_NOEXCEPT
  67. : m_type(type::get<RawType>()),
  68. m_value_type(type::get<typename sequential_container_mapper<RawType>::value_t>()),
  69. m_container(as_void_ptr(container)),
  70. m_get_is_empty_func(sequential_container_mapper_wrapper<RawType, ConstType>::is_empty),
  71. m_get_size_func(sequential_container_mapper_wrapper<RawType, ConstType>::get_size),
  72. m_set_size_func(sequential_container_mapper_wrapper<RawType, ConstType>::set_size),
  73. m_is_dynamic_func(sequential_container_mapper_wrapper<RawType, ConstType>::is_dynamic),
  74. m_get_rank_func(sequential_container_mapper_wrapper<RawType, ConstType>::get_rank),
  75. m_get_rank_type_func(sequential_container_mapper_wrapper<RawType, ConstType>::get_rank_type),
  76. m_begin_func(sequential_container_mapper_wrapper<RawType, ConstType>::begin),
  77. m_end_func(sequential_container_mapper_wrapper<RawType, ConstType>::end),
  78. m_equal_func(sequential_container_mapper_wrapper<RawType, ConstType>::equal),
  79. m_create_func(sequential_container_mapper_wrapper<RawType, ConstType>::create),
  80. m_delete_func(sequential_container_mapper_wrapper<RawType, ConstType>::destroy),
  81. m_get_data_func(sequential_container_mapper_wrapper<RawType, ConstType>::get_data),
  82. m_advance_func(sequential_container_mapper_wrapper<RawType, ConstType>::advance),
  83. m_erase_func(sequential_container_mapper_wrapper<RawType, ConstType>::erase),
  84. m_clear_func(sequential_container_mapper_wrapper<RawType, ConstType>::clear),
  85. m_insert_func(sequential_container_mapper_wrapper<RawType, ConstType>::insert),
  86. m_set_value_func(sequential_container_mapper_wrapper<RawType, ConstType>::set_value),
  87. m_get_value_func(sequential_container_mapper_wrapper<RawType, ConstType>::get_value)
  88. {
  89. }
  90. RTTR_INLINE variant_sequential_view_private(const variant_sequential_view_private& other) = default;
  91. RTTR_INLINE ~variant_sequential_view_private()
  92. {
  93. }
  94. RTTR_INLINE type get_type() const RTTR_NOEXCEPT
  95. {
  96. return m_type;
  97. }
  98. RTTR_INLINE type get_value_type() const RTTR_NOEXCEPT
  99. {
  100. return m_value_type;
  101. }
  102. RTTR_INLINE void copy(iterator_data& itr_tgt, const iterator_data& itr_src) const
  103. {
  104. m_create_func(itr_tgt, itr_src);
  105. }
  106. RTTR_INLINE void destroy(iterator_data& itr) const
  107. {
  108. m_delete_func(itr);
  109. }
  110. RTTR_INLINE void begin(iterator_data& itr) const
  111. {
  112. m_begin_func(m_container, itr);
  113. }
  114. RTTR_INLINE void end(iterator_data& itr) const
  115. {
  116. m_end_func(m_container, itr);
  117. }
  118. RTTR_INLINE bool is_empty() const RTTR_NOEXCEPT
  119. {
  120. return m_get_is_empty_func(m_container);
  121. }
  122. RTTR_INLINE bool is_dynamic() const RTTR_NOEXCEPT
  123. {
  124. return m_is_dynamic_func();
  125. }
  126. RTTR_INLINE std::size_t get_rank() const RTTR_NOEXCEPT
  127. {
  128. return m_get_rank_func();
  129. }
  130. RTTR_INLINE type get_rank_type(std::size_t index) const RTTR_NOEXCEPT
  131. {
  132. return m_get_rank_type_func(index);
  133. }
  134. RTTR_INLINE std::size_t get_size() const RTTR_NOEXCEPT
  135. {
  136. return m_get_size_func(m_container);
  137. }
  138. RTTR_INLINE bool set_size(std::size_t size) const RTTR_NOEXCEPT
  139. {
  140. return m_set_size_func(m_container, size);
  141. }
  142. RTTR_INLINE bool equal(const iterator_data& lhs_itr, const iterator_data& rhs_itr) const RTTR_NOEXCEPT
  143. {
  144. return m_equal_func(lhs_itr, rhs_itr);
  145. }
  146. RTTR_INLINE const variant get_data(const iterator_data& itr) const
  147. {
  148. return m_get_data_func(itr);
  149. }
  150. RTTR_INLINE void advance(iterator_data& itr, std::ptrdiff_t index) const
  151. {
  152. m_advance_func(itr, index);
  153. }
  154. RTTR_INLINE void clear()
  155. {
  156. m_clear_func(m_container);
  157. }
  158. RTTR_INLINE void erase(const iterator_data& itr_pos, iterator_data& itr)
  159. {
  160. m_erase_func(m_container, itr_pos, itr);
  161. }
  162. RTTR_INLINE void insert(const iterator_data& itr_pos, argument& value, iterator_data& itr)
  163. {
  164. m_insert_func(m_container, value, itr_pos, itr);
  165. }
  166. RTTR_INLINE bool set_value(std::size_t index, argument& arg)
  167. {
  168. return m_set_value_func(m_container, index, arg);
  169. }
  170. RTTR_INLINE variant get_value(std::size_t index) const
  171. {
  172. return m_get_value_func(m_container, index);
  173. }
  174. private:
  175. static bool equal_cmp_dummy_func(const iterator_data& lhs_itr, const iterator_data& rhs_itr) RTTR_NOEXCEPT;
  176. using equality_func = decltype(&equal_cmp_dummy_func); // workaround because of 'noexcept' can only appear on function declaration
  177. using get_is_empty_func = bool(*)(void* container);
  178. using get_size_func = std::size_t(*)(void* container);
  179. using set_size_func = bool(*)(void* container, std::size_t size);
  180. using get_rank_func = std::size_t(*)(void);
  181. using get_rank_type_func= type(*)(std::size_t index);
  182. using begin_func = void(*)(void* container, iterator_data& itr);
  183. using end_func = void(*)(void* container, iterator_data& itr);
  184. using advance_func = void(*)(iterator_data& itr, std::ptrdiff_t index);
  185. using create_func = void(*)(iterator_data& itr_tgt, const iterator_data& itr_src);
  186. using delete_func = void(*)(iterator_data& itr);
  187. using get_data_func = variant (*)(const iterator_data& itr);
  188. using clear_func = void(*)(void* container);
  189. using is_dynamic_func = bool(*)();
  190. using erase_func = void(*)(void* container, const iterator_data& itr_pos, iterator_data& itr);
  191. using insert_func = void(*)(void* container, argument& value, const detail::iterator_data& itr_pos, detail::iterator_data& itr);
  192. using set_value_func = bool(*)(void* container, std::size_t index, argument& arg);
  193. using get_value_func = variant(*)(void* container, std::size_t index);
  194. type m_type;
  195. type m_value_type;
  196. void* m_container;
  197. get_is_empty_func m_get_is_empty_func;
  198. get_size_func m_get_size_func;
  199. set_size_func m_set_size_func;
  200. is_dynamic_func m_is_dynamic_func;
  201. get_rank_func m_get_rank_func;
  202. get_rank_type_func m_get_rank_type_func;
  203. begin_func m_begin_func;
  204. end_func m_end_func;
  205. equality_func m_equal_func;
  206. create_func m_create_func;
  207. delete_func m_delete_func;
  208. get_data_func m_get_data_func;
  209. advance_func m_advance_func;
  210. erase_func m_erase_func;
  211. clear_func m_clear_func;
  212. insert_func m_insert_func;
  213. set_value_func m_set_value_func;
  214. get_value_func m_get_value_func;
  215. };
  216. } // end namespace detail
  217. } // end namespace rttr
  218. #endif // RTTR_VARIANT_SEQUENTIAL_VIEW_PRIVATE_H_