iterator_wrapper.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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_ITERATOR_WRAPPER_H_
  28. #define RTTR_ITERATOR_WRAPPER_H_
  29. #include "rttr/detail/base/core_prerequisites.h"
  30. #include "rttr/variant.h"
  31. #include <memory>
  32. #include <type_traits>
  33. namespace rttr
  34. {
  35. namespace detail
  36. {
  37. using iterator_data = std::aligned_storage<sizeof(double)>::type;
  38. template<typename T, bool Can_Place = (sizeof(T) <= sizeof(iterator_data)) &&
  39. (std::alignment_of<T>::value <= std::alignment_of<iterator_data>::value)>
  40. using can_place_in_iterator_data = std::integral_constant<bool, Can_Place>;
  41. /////////////////////////////////////////////////////////////////////////////////////////
  42. template<typename Itr>
  43. struct iterator_wrapper_small
  44. {
  45. using type = Itr;
  46. using iterator = Itr;
  47. static const Itr& get_iterator(const iterator_data& data)
  48. {
  49. return reinterpret_cast<const Itr&>(data);
  50. }
  51. static Itr& get_iterator(iterator_data& data)
  52. {
  53. return reinterpret_cast<Itr&>(data);
  54. }
  55. static void create(iterator_data& itr_tgt, const iterator& itr_src)
  56. {
  57. new (&itr_tgt) iterator(itr_src);
  58. }
  59. static void create(iterator_data& itr_tgt, const iterator_data& itr_src)
  60. {
  61. new (&itr_tgt) iterator(get_iterator(itr_src));
  62. }
  63. static void destroy(iterator_data& itr)
  64. {
  65. get_iterator(itr).~Itr();
  66. }
  67. };
  68. /////////////////////////////////////////////////////////////////////////////////////////
  69. template<typename Itr>
  70. struct iterator_wrapper_big
  71. {
  72. using type = Itr;
  73. using iterator = Itr;
  74. static const Itr& get_iterator(const iterator_data& data)
  75. {
  76. return *reinterpret_cast<Itr* const &>(data);
  77. }
  78. static Itr& get_iterator(iterator_data& data)
  79. {
  80. return *reinterpret_cast<Itr*&>(data);
  81. }
  82. static void create(iterator_data& itr_tgt, const iterator& itr_src)
  83. {
  84. reinterpret_cast<Itr*&>(itr_tgt) = new iterator(itr_src);
  85. }
  86. static void create(iterator_data& itr_tgt, const iterator_data& itr_src)
  87. {
  88. reinterpret_cast<Itr*&>(itr_tgt) = new iterator(*reinterpret_cast<Itr* const &>(itr_src));
  89. }
  90. static void destroy(iterator_data& itr)
  91. {
  92. auto& it = get_iterator(itr);
  93. delete &it;
  94. }
  95. };
  96. /////////////////////////////////////////////////////////////////////////////////////////
  97. template<typename Itr, typename Tp = conditional_t<can_place_in_iterator_data<Itr>::value,
  98. iterator_wrapper_small<Itr>,
  99. iterator_wrapper_big<Itr>>>
  100. struct iterator_wrapper_base : Tp
  101. {
  102. static void advance(iterator_data& itr, std::ptrdiff_t idx)
  103. {
  104. std::advance(Tp::get_iterator(itr), idx);
  105. }
  106. static bool equal(const iterator_data& lhs_itr, const iterator_data& rhs_itr) RTTR_NOEXCEPT
  107. {
  108. return (Tp::get_iterator(lhs_itr) == Tp::get_iterator(rhs_itr));
  109. }
  110. };
  111. /////////////////////////////////////////////////////////////////////////////////////////
  112. template<typename Itr>
  113. struct iterator_wrapper_associative_container : iterator_wrapper_base<Itr>
  114. {
  115. static variant get_key(const iterator_data& itr)
  116. {
  117. auto& it = iterator_wrapper_base<Itr>::get_iterator(itr);
  118. return variant(std::ref(it->first));
  119. }
  120. static variant get_value(const iterator_data& itr)
  121. {
  122. auto& it = iterator_wrapper_base<Itr>::get_iterator(itr);
  123. return variant(std::ref(it->second));
  124. }
  125. };
  126. } // end namespace detail
  127. } // end namespace rttr
  128. #endif // RTTR_ITERATOR_WRAPPER_H_