variant_associative_view_private.h 12 KB

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