variant_sequential_view.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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_H_
  28. #define RTTR_VARIANT_SEQUENTIAL_VIEW_H_
  29. #include "rttr/detail/base/core_prerequisites.h"
  30. #include "rttr/detail/misc/misc_type_traits.h"
  31. #include "rttr/variant.h"
  32. #include "rttr/detail/variant_sequential_view/variant_sequential_view_private.h"
  33. #include <cstddef>
  34. #include <vector>
  35. #include <memory>
  36. namespace rttr
  37. {
  38. class type;
  39. class instance;
  40. class argument;
  41. /*!
  42. * The \ref variant_sequential_view describes a class that refers to an
  43. * <a target="_blank" href=https://en.wikipedia.org/wiki/Sequence_container_(C%2B%2B)>sequence container</a> (e.g: `std::vector`)
  44. * inside a \ref variant.
  45. * With an instance of that class you can set/get values of such container,
  46. * without having access to the type declaration of the type or it's elements.
  47. *
  48. * A \ref variant_sequential_view can be created directly from a \ref variant with its member function \ref variant::create_sequential_view() "create_sequential_view()".
  49. * \remark The instance of an variant_sequential_view is always valid as long as the referenced \ref variant is valid, otherwise accessing a variant_sequential_view
  50. * is undefined behaviour.
  51. *
  52. * Meta Information
  53. * ----------------
  54. *
  55. * RTTR recognize whether a type is an sequential container or not with the help of the \ref sequential_container_mapper class template.
  56. * This call can access different container types via one common interface.
  57. * At the moment there exist specializations for following types:
  58. * `std::vector<T>`, `std::array<T, std::size_t>`, `std::list<T>`, `std::deque<T>`, `std::initializer_list<T>` and raw arrays
  59. *
  60. * Copying and Assignment
  61. * ----------------------
  62. * A \ref variant_sequential_view object can be copied and assigned,
  63. * however each copy will reference the data of same underlying \ref variant value.
  64. *
  65. * Typical Usage
  66. * ----------------------
  67. *
  68. * \code{.cpp}
  69. * std::vector<int> my_vec = { 1, 2, 3, 4, 5};
  70. * variant var = my_vec; // copies data into variant
  71. * if (var.is_sequential_container())
  72. * {
  73. * variant_sequential_view view = var.create_sequential_view(); // performs no copy of the underlying vector
  74. * std::cout << view.get_size() << std::endl; // prints: '5'
  75. * for (const auto& item : view)
  76. * {
  77. * // remark that the value is stored inside a 'std::reference_wrapper', however there is an automatic conversion for wrapper classes implemented.
  78. * std::cout << "data: " << item.to_string() << " ";
  79. * }
  80. * }
  81. * \endcode
  82. *
  83. * Output:
  84. * \code
  85. * 1 2 3 4 5
  86. * \endcode
  87. *
  88. * \see variant, type::is_sequential_container()
  89. */
  90. class RTTR_API variant_sequential_view
  91. {
  92. public:
  93. class const_iterator;
  94. /*!
  95. * \brief Constructs an invalid variant_sequential_view object.
  96. *
  97. * \see is_valid()
  98. */
  99. variant_sequential_view();
  100. /*!
  101. * \brief Constructs a copy of the given variant_sequential_view \p other.
  102. */
  103. variant_sequential_view(const variant_sequential_view& other);
  104. /*!
  105. * \brief Destroys the variant_sequential_view.
  106. *
  107. * \remark The underlying data is not destroyed.
  108. */
  109. ~variant_sequential_view() RTTR_NOEXCEPT;
  110. /*!
  111. * \brief Assigns the value of the \a other variant_sequential_view to this variant_sequential_view.
  112. *
  113. * \return A reference to the variant_sequential_view with the new data.
  114. */
  115. variant_sequential_view& operator=(const variant_sequential_view& other) RTTR_NOEXCEPT;
  116. /*!
  117. * \brief Returns true if this variant_sequential_view is valid, that means the object is holding some data.
  118. * When the variant_sequential_view doesn't hold any data it will return false.
  119. *
  120. * \return True if this \ref variant_sequential_view is valid, otherwise false.
  121. */
  122. bool is_valid() const RTTR_NOEXCEPT;
  123. /*!
  124. * \brief Convenience function to check if this \ref variant_sequential_view is valid or not.
  125. *
  126. * \see is_valid()
  127. *
  128. * \return True if this \ref variant_sequential_view is valid, otherwise false.
  129. */
  130. explicit operator bool() const RTTR_NOEXCEPT;
  131. /*!
  132. * \brief Swaps this variant_sequential_view with the \a other variant_sequential_view.
  133. */
  134. void swap(variant_sequential_view& other) RTTR_NOEXCEPT;
  135. /*!
  136. * \brief Returns the \ref type object of this sequential container.
  137. *
  138. * \remark When the view is not valid, this function will return an invalid type object.
  139. *
  140. * \return \ref type "Type" of the sequential container.
  141. */
  142. type get_type() const RTTR_NOEXCEPT;
  143. /*!
  144. * \brief Returns the \ref type object from the value of this sequential container.
  145. *
  146. * \remark When the view is not valid, this function will return an invalid type object.
  147. *
  148. * \return \ref type "Type" from the value of the sequential container.
  149. */
  150. type get_value_type() const RTTR_NOEXCEPT;
  151. /*!
  152. * \brief Checks if the container has no elements.
  153. *
  154. * \return `true` if container is empty, otherwise `false`.
  155. */
  156. bool is_empty() const RTTR_NOEXCEPT;
  157. /*!
  158. * \brief Returns `true` if this sequential view is dynamic, otherwise `false`.
  159. * When an sequential view is dynamic, it is possible to change its \ref set_size "size",
  160. * \ref clear its content or \ref insert and \ref erase values from it.
  161. *
  162. * \see set_size(), insert(), erase(), clear()
  163. *
  164. * \return A boolean flag which indicates whether this sequential container is dynamic or not.
  165. */
  166. bool is_dynamic() const RTTR_NOEXCEPT;
  167. /*!
  168. * \brief Gets the rank (number of dimensions) of this sequential container.
  169. *
  170. * Take a look at following return values:
  171. * - \p `int[4]` => `1`
  172. * - \p `int[4][4]` => `2`
  173. * - \p `int[4][4][4]` => `3`
  174. * - ...
  175. *
  176. * \return Returns the rank of the sequential container.
  177. */
  178. std::size_t get_rank() const RTTR_NOEXCEPT;
  179. /*!
  180. * \brief Gets the type of the given rank index.
  181. *
  182. * Take a look at following return value for an array of type: `int[2][10]`
  183. * - \p `get_rank_type(0)` => `int[2][10]`
  184. * - \p `get_rank_type(1)` => `int[10]`
  185. * - \p `get_rank_type(2)` => `int`
  186. * - \p `get_rank_type(3)` => **INVALID**
  187. *
  188. * \return The rank type at the given dimension \p index.
  189. */
  190. type get_rank_type(std::size_t index) const RTTR_NOEXCEPT;
  191. /*!
  192. * \brief Returns the number of elements in the sequential container.
  193. *
  194. * \return The number of elements in the sequential container.
  195. */
  196. std::size_t get_size() const RTTR_NOEXCEPT;
  197. /*!
  198. * \brief Sets the size of the sequential container.
  199. *
  200. * \return `true`, when the size of the container could be changed, otherwise `false`.
  201. *
  202. * \see is_dynamic()
  203. */
  204. bool set_size(std::size_t size) const RTTR_NOEXCEPT;
  205. /*!
  206. * \brief Insert a value into the container.
  207. *
  208. * \return An iterator to the inserted element, otherwise an invalid iterator, when the insertion was not possible.
  209. */
  210. const_iterator insert(const const_iterator& pos, argument value);
  211. /*!
  212. * \brief Removes the element (if one exists) at the position \p pos.
  213. *
  214. * \return Iterator following the last removed element.
  215. */
  216. const_iterator erase(const const_iterator& pos);
  217. /*!
  218. * \brief Removes all elements from the container.
  219. *
  220. * \remark Invalidates all references, pointers, or iterators referring to contained elements.
  221. */
  222. void clear();
  223. /*!
  224. * \brief Set the content of the the argument \p arg at the specified index \p index
  225. * into the underlying sequential container.
  226. *
  227. * \return `true` if the value could be set, otherwise `false`.
  228. */
  229. bool set_value(std::size_t index, argument arg);
  230. /*!
  231. * \brief Returns the current value at index \p index.
  232. *
  233. * \return The data at the specified index \p index, wrapped inside a `std::reference_wrapper<T>`.
  234. *
  235. * \remark Make sure the index is in a valid range, otherwise undefined behaviour may occurr.
  236. *
  237. * \see get_size()
  238. */
  239. variant get_value(std::size_t index) const;
  240. /*!
  241. * \brief Returns an iterator to the first element of the container.
  242. *
  243. * \see end()
  244. *
  245. * \return Iterator to the first element .
  246. */
  247. const_iterator begin() const;
  248. /*!
  249. * \brief Returns an iterator to the element following the last element of the container.
  250. *
  251. * \see begin()
  252. *
  253. * \return Iterator to the element following the last element.
  254. */
  255. const_iterator end() const;
  256. /*!
  257. * The \ref variant_sequential_view::const_iterator allows iteration over an sequential container in a variant.
  258. * An instance can only be created by an variant_sequential_view.
  259. *
  260. * Typical Usage
  261. * ----------------------
  262. *
  263. * \code{.cpp}
  264. * std::vector<int> my_vec = { 1, 2, 3, 4, 5};
  265. * variant var = my_vec; // copies data into variant
  266. * if (var.is_sequential_container())
  267. * {
  268. * variant_sequential_view view = var.create_sequential_view(); // performs no copy of the underlying vector
  269. * std::cout << view.get_size() << std::endl; // prints: '5'
  270. * for (const auto& item : view)
  271. * {
  272. * // remark that the value is stored inside a 'std::reference_wrapper', however there is an automatic conversion for wrapper classes implemented.
  273. * std::cout << "data: " << item.to_string() << " ";
  274. * }
  275. * }
  276. * \endcode
  277. *
  278. * \remark The iterator is valid as long as the variant_sequential_view and it corresponding variant is valid and not modified.
  279. */
  280. class RTTR_API const_iterator
  281. {
  282. public:
  283. using self_type = const_iterator;
  284. using value_type = variant;
  285. /*!
  286. * \brief Destroys the variant_sequential_view::const_iterator
  287. */
  288. ~const_iterator();
  289. /*!
  290. * \brief Creates a copy of \p other
  291. */
  292. const_iterator(const const_iterator& other);
  293. /*!
  294. * \brief Assigns \p other to `this`.
  295. */
  296. const_iterator& operator=(const_iterator other);
  297. /*!
  298. * Returns the underlying value in a variant stored in a `std::reference_wrapper<T>`.
  299. * When the data cannot be returns as reference from the container, the data is stored directly inside the variant.
  300. * E.g. for `std::vector<bool>` no reference can be returned.
  301. *
  302. * \see variant::extract_wrapped_value(), variant::get_wrapped_value<T>()
  303. */
  304. const variant operator*() const;
  305. /*!
  306. * \brief Returns the current value, stored inside a `std::reference_wrapper<T>`
  307. * and copied to a variant.
  308. *
  309. * \see operator*(), variant::extract_wrapped_value(), variant::get_wrapped_value<T>()
  310. */
  311. const variant get_data() const;
  312. /*!
  313. * \brief Pre-increment operator advances the iterator to the next item
  314. * in the container and returns an iterator to the new current item.
  315. *
  316. * \remark Calling this function on and iterator with value variant_sequential_view::end()
  317. * leads to undefined behaviour.
  318. */
  319. const_iterator &operator++();
  320. /*!
  321. * \brief Post-increment operator advances the iterator to the next item
  322. * in the container and returns an iterator to the previously current item.
  323. */
  324. const_iterator operator++(int);
  325. /*!
  326. * \brief Pre-decrement operator makes the preceding item current and returns
  327. * an iterator to the new current item.
  328. *
  329. * \remark Calling this function on and iterator with value variant_sequential_view::begin()
  330. * leads to undefined behaviour.
  331. */
  332. const_iterator &operator--();
  333. /*!
  334. * \brief Post-decrement operator makes the preceding item current
  335. * and returns an iterator to the previously current item.
  336. */
  337. const_iterator operator--(int);
  338. /*!
  339. * \brief Advances the iterator by i items.
  340. */
  341. const_iterator &operator+=(int i);
  342. /*!
  343. * \brief Returns an iterator to the item at i positions backward from this iterator.
  344. */
  345. const_iterator &operator-=(int i);
  346. /*!
  347. * \brief Returns an iterator to the item at i positions forward from this iterator.
  348. */
  349. const_iterator operator+(int i) const;
  350. /*!
  351. * \brief Returns an iterator to the item at i positions backward from this iterator.
  352. */
  353. const_iterator operator-(int i) const;
  354. /*!
  355. * \brief Returns `true` if \p other points to the same item
  356. * as this iterator; otherwise returns false.
  357. *
  358. * \see \ref const_iterator::operator!= "operator!="
  359. */
  360. bool operator==(const const_iterator& other) const;
  361. /*!
  362. * \brief Returns true if \p other points to a different item
  363. * than this iterator; otherwise returns false.
  364. *
  365. * \see \ref operator== "operator=="
  366. */
  367. bool operator!=(const const_iterator& other) const;
  368. private:
  369. const_iterator(const detail::variant_sequential_view_private* view) RTTR_NOEXCEPT;
  370. void swap(const_iterator& other);
  371. friend class variant_sequential_view;
  372. const detail::variant_sequential_view_private* m_view;
  373. detail::iterator_data m_itr;
  374. };
  375. private:
  376. friend class variant;
  377. friend class argument;
  378. detail::variant_sequential_view_private m_view;
  379. };
  380. } // end namespace rttr
  381. #endif // RTTR_VARIANT_SEQUENTIAL_VIEW_H_