variant_associative_view.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. #include "rttr/variant_associative_view.h"
  28. #include "rttr/argument.h"
  29. #include "rttr/instance.h"
  30. using namespace std;
  31. namespace rttr
  32. {
  33. /////////////////////////////////////////////////////////////////////////////////
  34. variant_associative_view::variant_associative_view()
  35. {
  36. }
  37. /////////////////////////////////////////////////////////////////////////////////
  38. variant_associative_view::variant_associative_view(const variant_associative_view& other)
  39. : m_view(other.m_view)
  40. {
  41. }
  42. /////////////////////////////////////////////////////////////////////////////////
  43. variant_associative_view::~variant_associative_view() RTTR_NOEXCEPT
  44. {
  45. }
  46. /////////////////////////////////////////////////////////////////////////////////
  47. void variant_associative_view::swap(variant_associative_view& other) RTTR_NOEXCEPT
  48. {
  49. std::swap(m_view, other.m_view);
  50. }
  51. /////////////////////////////////////////////////////////////////////////////////
  52. variant_associative_view& variant_associative_view::operator=(const variant_associative_view& other) RTTR_NOEXCEPT
  53. {
  54. variant_associative_view(other).swap(*this);
  55. return *this;
  56. }
  57. /////////////////////////////////////////////////////////////////////////////////////////
  58. bool variant_associative_view::is_valid() const RTTR_NOEXCEPT
  59. {
  60. return m_view.get_type().is_valid();
  61. }
  62. /////////////////////////////////////////////////////////////////////////////////////////
  63. variant_associative_view::operator bool() const RTTR_NOEXCEPT
  64. {
  65. return m_view.get_type().is_valid();
  66. }
  67. /////////////////////////////////////////////////////////////////////////////////////////
  68. type variant_associative_view::get_type() const RTTR_NOEXCEPT
  69. {
  70. return m_view.get_type();
  71. }
  72. /////////////////////////////////////////////////////////////////////////////////////////
  73. type variant_associative_view::get_key_type() const RTTR_NOEXCEPT
  74. {
  75. return m_view.get_key_type();
  76. }
  77. /////////////////////////////////////////////////////////////////////////////////////////
  78. type variant_associative_view::get_value_type() const RTTR_NOEXCEPT
  79. {
  80. return m_view.get_value_type();
  81. }
  82. /////////////////////////////////////////////////////////////////////////////////////////
  83. bool variant_associative_view::is_key_only_type() const RTTR_NOEXCEPT
  84. {
  85. return (m_view.get_value_type().is_valid() == false && is_valid());
  86. }
  87. /////////////////////////////////////////////////////////////////////////////////////////
  88. bool variant_associative_view::is_empty() const RTTR_NOEXCEPT
  89. {
  90. return m_view.is_empty();
  91. }
  92. /////////////////////////////////////////////////////////////////////////////////////////
  93. std::size_t variant_associative_view::get_size() const RTTR_NOEXCEPT
  94. {
  95. return m_view.get_size();
  96. }
  97. /////////////////////////////////////////////////////////////////////////////////////////
  98. std::pair<variant_associative_view::const_iterator, bool> variant_associative_view::insert(argument key)
  99. {
  100. const_iterator itr(&m_view);
  101. auto success = m_view.insert(key, itr.m_itr);
  102. return {itr, success};
  103. }
  104. /////////////////////////////////////////////////////////////////////////////////////////
  105. std::pair<variant_associative_view::const_iterator, bool> variant_associative_view::insert(argument key, argument value)
  106. {
  107. const_iterator itr(&m_view);
  108. auto success = m_view.insert(key, value, itr.m_itr);
  109. return {itr, success};
  110. }
  111. /////////////////////////////////////////////////////////////////////////////////////////
  112. variant_associative_view::const_iterator variant_associative_view::find(argument arg)
  113. {
  114. const_iterator itr(&m_view);
  115. m_view.find(itr.m_itr, arg);
  116. return itr;
  117. }
  118. /////////////////////////////////////////////////////////////////////////////////////////
  119. std::size_t variant_associative_view::erase(argument key)
  120. {
  121. return m_view.erase(key);
  122. }
  123. /////////////////////////////////////////////////////////////////////////////////////////
  124. void variant_associative_view::clear()
  125. {
  126. m_view.clear();
  127. }
  128. /////////////////////////////////////////////////////////////////////////////////////////
  129. std::pair<variant_associative_view::const_iterator, variant_associative_view::const_iterator>
  130. variant_associative_view::equal_range(argument key)
  131. {
  132. const_iterator itr_begin(&m_view);
  133. const_iterator itr_end(&m_view);
  134. m_view.equal_range(key, itr_begin.m_itr, itr_end.m_itr);
  135. return {itr_begin, itr_end};
  136. }
  137. /////////////////////////////////////////////////////////////////////////////////////////
  138. variant_associative_view::const_iterator variant_associative_view::begin() const
  139. {
  140. const_iterator itr(&m_view);
  141. m_view.begin(itr.m_itr);
  142. return itr;
  143. }
  144. /////////////////////////////////////////////////////////////////////////////////////////
  145. variant_associative_view::const_iterator variant_associative_view::end() const
  146. {
  147. const_iterator itr(&m_view);
  148. m_view.end(itr.m_itr);
  149. return itr;
  150. }
  151. /////////////////////////////////////////////////////////////////////////////////////////
  152. /////////////////////////////////////////////////////////////////////////////////////////
  153. /////////////////////////////////////////////////////////////////////////////////////////
  154. variant_associative_view::const_iterator::const_iterator(const detail::variant_associative_view_private* view) RTTR_NOEXCEPT
  155. : m_view(view)
  156. {
  157. }
  158. /////////////////////////////////////////////////////////////////////////////////////////
  159. variant_associative_view::const_iterator::~const_iterator()
  160. {
  161. m_view->destroy(m_itr);
  162. }
  163. /////////////////////////////////////////////////////////////////////////////////////////
  164. variant_associative_view::const_iterator::const_iterator(const const_iterator &other)
  165. : m_view(other.m_view),
  166. m_itr(other.m_itr)
  167. {
  168. m_view->copy(m_itr, other.m_itr);
  169. }
  170. /////////////////////////////////////////////////////////////////////////////////////////
  171. variant_associative_view::const_iterator& variant_associative_view::const_iterator::operator=(const_iterator other)
  172. {
  173. swap(other);
  174. return *this;
  175. }
  176. /////////////////////////////////////////////////////////////////////////////////////////
  177. void variant_associative_view::const_iterator::swap(const_iterator& other)
  178. {
  179. std::swap(m_itr, other.m_itr);
  180. std::swap(m_view, other.m_view);
  181. }
  182. /////////////////////////////////////////////////////////////////////////////////////////
  183. const std::pair<variant, variant> variant_associative_view::const_iterator::operator*() const
  184. {
  185. return m_view->get_key_value(m_itr);
  186. }
  187. /////////////////////////////////////////////////////////////////////////////////////////
  188. const variant variant_associative_view::const_iterator::get_key() const
  189. {
  190. return m_view->get_key(m_itr);
  191. }
  192. /////////////////////////////////////////////////////////////////////////////////////////
  193. const variant variant_associative_view::const_iterator::get_value() const
  194. {
  195. return m_view->get_value(m_itr);
  196. }
  197. /////////////////////////////////////////////////////////////////////////////////////////
  198. variant_associative_view::const_iterator& variant_associative_view::const_iterator::operator++()
  199. {
  200. m_view->advance(m_itr, 1);
  201. return *this;
  202. }
  203. /////////////////////////////////////////////////////////////////////////////////////////
  204. variant_associative_view::const_iterator variant_associative_view::const_iterator::operator++(int)
  205. {
  206. const_iterator result(m_view);
  207. m_view->copy(result.m_itr, m_itr);
  208. m_view->advance(m_itr, 1);
  209. return result;
  210. }
  211. /////////////////////////////////////////////////////////////////////////////////////////
  212. variant_associative_view::const_iterator& variant_associative_view::const_iterator::operator--()
  213. {
  214. m_view->advance(m_itr, -1);
  215. return *this;
  216. }
  217. /////////////////////////////////////////////////////////////////////////////////////////
  218. variant_associative_view::const_iterator variant_associative_view::const_iterator::operator--(int)
  219. {
  220. const_iterator result(m_view);
  221. m_view->copy(result.m_itr, m_itr);
  222. m_view->advance(m_itr, -1);
  223. return result;
  224. }
  225. /////////////////////////////////////////////////////////////////////////////////////////
  226. variant_associative_view::const_iterator& variant_associative_view::const_iterator::operator+=(int i)
  227. {
  228. m_view->advance(m_itr, i);
  229. return *this;
  230. }
  231. /////////////////////////////////////////////////////////////////////////////////////////
  232. variant_associative_view::const_iterator& variant_associative_view::const_iterator::operator-=(int i)
  233. {
  234. m_view->advance(m_itr, -i);
  235. return *this;
  236. }
  237. /////////////////////////////////////////////////////////////////////////////////////////
  238. variant_associative_view::const_iterator variant_associative_view::const_iterator::operator+(int i) const
  239. {
  240. const_iterator result(m_view);
  241. m_view->copy(result.m_itr, m_itr);
  242. result.m_view->advance(result.m_itr, i);
  243. return result;
  244. }
  245. /////////////////////////////////////////////////////////////////////////////////////////
  246. variant_associative_view::const_iterator variant_associative_view::const_iterator::operator-(int i) const
  247. {
  248. const_iterator result(m_view);
  249. m_view->copy(result.m_itr, m_itr);
  250. result.m_view->advance(result.m_itr, -i);
  251. return result;
  252. }
  253. /////////////////////////////////////////////////////////////////////////////////////////
  254. bool variant_associative_view::const_iterator::operator==(const const_iterator& other) const
  255. {
  256. return m_view->equal(m_itr, other.m_itr);
  257. }
  258. /////////////////////////////////////////////////////////////////////////////////////////
  259. bool variant_associative_view::const_iterator::operator!=(const const_iterator& other) const
  260. {
  261. return !m_view->equal(m_itr, other.m_itr);
  262. }
  263. /////////////////////////////////////////////////////////////////////////////////////////
  264. } // end namespace rttr