variant_impl.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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_IMPL_H_
  28. #define RTTR_VARIANT_IMPL_H_
  29. #include "rttr/type.h"
  30. #include "rttr/detail/misc/misc_type_traits.h"
  31. #include "rttr/detail/misc/utility.h"
  32. #include "rttr/detail/type/type_converter.h"
  33. #include "rttr/detail/misc/data_address_container.h"
  34. #include "rttr/detail/variant/variant_data_policy.h"
  35. #include "rttr/variant_associative_view.h"
  36. #include "rttr/variant_sequential_view.h"
  37. namespace rttr
  38. {
  39. namespace detail
  40. {
  41. template<typename T>
  42. using variant_t = remove_cv_t<remove_reference_t<T>>;
  43. }
  44. /////////////////////////////////////////////////////////////////////////////////////////
  45. RTTR_INLINE variant::variant()
  46. : m_policy(&detail::variant_data_policy_empty::invoke)
  47. {
  48. }
  49. /////////////////////////////////////////////////////////////////////////////////////////
  50. template<typename T, typename Tp>
  51. RTTR_INLINE variant::variant(T&& val)
  52. : m_policy(&detail::variant_policy<Tp>::invoke)
  53. {
  54. static_assert(std::is_copy_constructible<Tp>::value || std::is_array<Tp>::value,
  55. "The given value is not copy constructible, try to add a copy constructor to the class.");
  56. detail::variant_policy<Tp>::create(std::forward<T>(val), m_data);
  57. }
  58. /////////////////////////////////////////////////////////////////////////////////////////
  59. RTTR_INLINE variant::~variant()
  60. {
  61. m_policy(detail::variant_policy_operation::DESTROY, m_data, detail::argument_wrapper());
  62. }
  63. /////////////////////////////////////////////////////////////////////////////////////////
  64. template<typename T, typename Tp>
  65. RTTR_INLINE variant& variant::operator=(T&& other)
  66. {
  67. *this = variant(std::forward<T>(other));
  68. return *this;
  69. }
  70. /////////////////////////////////////////////////////////////////////////////////////////
  71. RTTR_INLINE bool variant::operator==(const variant& other) const
  72. {
  73. auto ok = false;
  74. return compare_equal(other, ok);
  75. }
  76. /////////////////////////////////////////////////////////////////////////////////////////
  77. RTTR_INLINE bool variant::operator!=(const variant& other) const
  78. {
  79. auto ok = false;
  80. return !compare_equal(other, ok);
  81. }
  82. /////////////////////////////////////////////////////////////////////////////////////////
  83. RTTR_INLINE bool variant::operator<(const variant& other) const
  84. {
  85. bool ok = false;
  86. return compare_less(other, ok);
  87. }
  88. /////////////////////////////////////////////////////////////////////////////////////////
  89. RTTR_INLINE bool variant::operator<=(const variant& other) const
  90. {
  91. auto ok_equal = false, ok_less = false;
  92. return ((compare_equal(other, ok_equal) && ok_equal) ||
  93. (compare_less(other, ok_less) && ok_less));
  94. }
  95. /////////////////////////////////////////////////////////////////////////////////////////
  96. RTTR_INLINE bool variant::operator>=(const variant& other) const
  97. {
  98. auto ok_equal = false, ok_less = false;
  99. return ( ((compare_equal(other, ok_equal) && ok_equal) ||
  100. (!compare_less(other, ok_less) && ok_less))
  101. && is_valid() && other.is_valid());
  102. }
  103. /////////////////////////////////////////////////////////////////////////////////////////
  104. RTTR_INLINE bool variant::operator>(const variant& other) const
  105. {
  106. auto ok_equal = false, ok_less = false;
  107. return ((!compare_equal(other, ok_equal) && ok_equal) &&
  108. (!compare_less(other, ok_less) && ok_less));
  109. }
  110. /////////////////////////////////////////////////////////////////////////////////////////
  111. template<typename T>
  112. RTTR_INLINE T& variant::get_value()
  113. {
  114. using namespace detail;
  115. auto result = unsafe_variant_cast<variant_t<T>>(this);
  116. return *result;
  117. }
  118. /////////////////////////////////////////////////////////////////////////////////////////
  119. template<typename T>
  120. RTTR_INLINE const T& variant::get_value() const
  121. {
  122. using namespace detail;
  123. auto result = unsafe_variant_cast<variant_t<T>>(this);
  124. return *result;
  125. }
  126. /////////////////////////////////////////////////////////////////////////////////////////
  127. template<typename T>
  128. RTTR_INLINE const T& variant::get_wrapped_value() const
  129. {
  130. detail::data_address_container result{detail::get_invalid_type(), detail::get_invalid_type(), nullptr, nullptr};
  131. m_policy(detail::variant_policy_operation::GET_ADDRESS_CONTAINER, m_data, result);
  132. using nonRef = detail::remove_cv_t<T>;
  133. return *reinterpret_cast<const nonRef*>(result.m_data_address_wrapped_type);
  134. }
  135. /////////////////////////////////////////////////////////////////////////////////////////
  136. RTTR_INLINE void* variant::get_ptr() const
  137. {
  138. void* value;
  139. m_policy(detail::variant_policy_operation::GET_PTR, m_data, value);
  140. return value;
  141. }
  142. /////////////////////////////////////////////////////////////////////////////////////////
  143. RTTR_INLINE type variant::get_raw_type() const
  144. {
  145. type result = detail::get_invalid_type();
  146. m_policy(detail::variant_policy_operation::GET_RAW_TYPE, m_data, result);
  147. return result;
  148. }
  149. /////////////////////////////////////////////////////////////////////////////////////////
  150. RTTR_INLINE void* variant::get_raw_ptr() const
  151. {
  152. void* result;
  153. m_policy(detail::variant_policy_operation::GET_RAW_PTR, m_data, result);
  154. return result;
  155. }
  156. /////////////////////////////////////////////////////////////////////////////////////////
  157. RTTR_INLINE detail::data_address_container variant::get_data_address_container() const
  158. {
  159. detail::data_address_container result{detail::get_invalid_type(), detail::get_invalid_type(), nullptr, nullptr};
  160. m_policy(detail::variant_policy_operation::GET_ADDRESS_CONTAINER, m_data, result);
  161. return result;
  162. }
  163. /////////////////////////////////////////////////////////////////////////////////////////
  164. template<typename T>
  165. RTTR_INLINE bool variant::is_type() const
  166. {
  167. type src_type = detail::get_invalid_type();
  168. m_policy(detail::variant_policy_operation::GET_TYPE, m_data, src_type);
  169. return (type::get<T>() == src_type);
  170. }
  171. /////////////////////////////////////////////////////////////////////////////////////////
  172. template<typename T>
  173. RTTR_INLINE bool variant::can_convert() const
  174. {
  175. return can_convert(type::get<T>());
  176. }
  177. /////////////////////////////////////////////////////////////////////////////////////////
  178. template<typename T>
  179. RTTR_INLINE bool variant::try_basic_type_conversion(T& to) const
  180. {
  181. return m_policy(detail::variant_policy_operation::CONVERT, m_data, argument(to));
  182. }
  183. /////////////////////////////////////////////////////////////////////////////////////////
  184. template<typename T>
  185. typename std::enable_if<detail::pointer_count<T>::value == 1, bool>::type
  186. RTTR_INLINE variant::try_pointer_conversion(T& to, const type& source_type, const type& target_type) const
  187. {
  188. if (!source_type.is_pointer())
  189. return false;
  190. auto ptr = get_raw_ptr();
  191. if (ptr)
  192. {
  193. if ((ptr = type::apply_offset(ptr, source_type, target_type)) != nullptr)
  194. {
  195. to = reinterpret_cast<T>(ptr);
  196. return true;
  197. }
  198. }
  199. else // a nullptr
  200. {
  201. // check if a down cast is possible
  202. if (source_type.is_derived_from(target_type))
  203. {
  204. to = reinterpret_cast<T>(ptr);
  205. return true;
  206. }
  207. }
  208. return false;
  209. }
  210. /////////////////////////////////////////////////////////////////////////////////////////
  211. template<typename T>
  212. typename std::enable_if<detail::pointer_count<T>::value != 1, bool>::type
  213. RTTR_INLINE variant::try_pointer_conversion(T& to, const type& source_type, const type& target_type) const
  214. {
  215. return false;
  216. }
  217. /////////////////////////////////////////////////////////////////////////////////////////
  218. RTTR_INLINE bool variant::is_nullptr() const
  219. {
  220. return m_policy(detail::variant_policy_operation::IS_NULLPTR, m_data, detail::argument_wrapper());
  221. }
  222. /////////////////////////////////////////////////////////////////////////////////////////
  223. template<typename T>
  224. typename std::enable_if<detail::is_nullptr_t<T>::value, bool>::type
  225. static RTTR_INLINE ptr_to_nullptr(T& to)
  226. {
  227. to = nullptr;
  228. return true;
  229. }
  230. /////////////////////////////////////////////////////////////////////////////////////////
  231. template<typename T>
  232. typename std::enable_if<!detail::is_nullptr_t<T>::value, bool>::type
  233. static RTTR_INLINE ptr_to_nullptr(T& to)
  234. {
  235. return false;
  236. }
  237. /////////////////////////////////////////////////////////////////////////////////////////
  238. template<typename T>
  239. RTTR_INLINE bool variant::convert(T& value) const
  240. {
  241. bool ok = false;
  242. const type source_type = get_type();
  243. const type target_type = type::get<T>();
  244. if (source_type.is_wrapper() && !target_type.is_wrapper())
  245. {
  246. variant var = extract_wrapped_value();
  247. return var.convert<T>(value);
  248. }
  249. else if (!source_type.is_wrapper() && target_type.is_wrapper() &&
  250. target_type.get_wrapped_type() == source_type)
  251. {
  252. variant var = create_wrapped_value(target_type);
  253. if ((ok = var.is_valid()) == true)
  254. value = var.get_value<T>();
  255. }
  256. else if (target_type == source_type)
  257. {
  258. value = const_cast<variant&>(*this).get_value<T>();
  259. ok = true;
  260. }
  261. else if(try_basic_type_conversion(value))
  262. {
  263. ok = true;
  264. }
  265. else if (const auto& converter = source_type.get_type_converter(target_type))
  266. {
  267. const auto target_converter = static_cast<const detail::type_converter_target<T>*>(converter);
  268. value = target_converter->convert(get_ptr(), ok);
  269. }
  270. else if (target_type == type::get<std::nullptr_t>())
  271. {
  272. if (is_nullptr())
  273. ok = ptr_to_nullptr(value);
  274. }
  275. else
  276. {
  277. ok = try_pointer_conversion(value, source_type, target_type);
  278. }
  279. return ok;
  280. }
  281. /////////////////////////////////////////////////////////////////////////////////////////
  282. template<typename T>
  283. RTTR_INLINE detail::enable_if_t<std::is_arithmetic<T>::value, T> variant::convert_impl(bool* ok) const
  284. {
  285. T result = 0;
  286. const bool could_convert = convert<T>(result);
  287. if (ok)
  288. *ok = could_convert;
  289. return result;
  290. }
  291. /////////////////////////////////////////////////////////////////////////////////////////
  292. template<typename T>
  293. RTTR_INLINE detail::enable_if_t<!std::is_arithmetic<T>::value && !std::is_enum<T>::value, T> variant::convert_impl(bool* ok) const
  294. {
  295. static_assert(std::is_default_constructible<T>::value, "The given type T has no default constructor."
  296. "You can only convert to a type, with a default constructor.");
  297. T result;
  298. const bool could_convert = convert<T>(result);
  299. if (ok)
  300. *ok = could_convert;
  301. return result;
  302. }
  303. /////////////////////////////////////////////////////////////////////////////////////////
  304. template<typename T>
  305. RTTR_INLINE detail::enable_if_t<std::is_enum<T>::value, T> variant::convert_impl(bool* ok) const
  306. {
  307. const auto target_type = type::get<T>();
  308. if (get_type() == target_type)
  309. {
  310. T result;
  311. const auto could_convert = convert<T>(result);
  312. if (ok)
  313. *ok = could_convert;
  314. return result;
  315. }
  316. else
  317. {
  318. variant var = type::get<T>();
  319. auto wrapper = std::ref(var);
  320. const auto could_convert = convert<std::reference_wrapper<variant>>(wrapper);
  321. if (ok)
  322. *ok = could_convert;
  323. return var.get_value<T>();
  324. }
  325. }
  326. /////////////////////////////////////////////////////////////////////////////////////////
  327. template<typename T>
  328. RTTR_INLINE T variant::convert(bool* ok) const
  329. {
  330. return convert_impl<T>(ok);
  331. }
  332. /////////////////////////////////////////////////////////////////////////////////////////
  333. /////////////////////////////////////////////////////////////////////////////////////////
  334. /////////////////////////////////////////////////////////////////////////////////////////
  335. namespace detail
  336. {
  337. /////////////////////////////////////////////////////////////////////////////////////////
  338. template<class T>
  339. RTTR_INLINE T* unsafe_variant_cast(variant* operand) RTTR_NOEXCEPT
  340. {
  341. const void* value;
  342. operand->m_policy(detail::variant_policy_operation::GET_VALUE, operand->m_data, value);
  343. return reinterpret_cast<T*>(const_cast<void*>(value));
  344. }
  345. /////////////////////////////////////////////////////////////////////////////////////////
  346. template<class T>
  347. RTTR_INLINE const T* unsafe_variant_cast(const variant* operand) RTTR_NOEXCEPT
  348. {
  349. return unsafe_variant_cast<const T>(const_cast<variant*>(operand));
  350. }
  351. } // end namespace detail
  352. /////////////////////////////////////////////////////////////////////////////////////////
  353. template<class T>
  354. RTTR_INLINE T variant_cast(const variant& operand)
  355. {
  356. using namespace detail;
  357. static_assert(std::is_constructible<T, const variant_t<T>&>::value,
  358. "variant_cast<T>(variant&) requires T to be constructible from const remove_cv_t<remove_reference_t<T>>&");
  359. auto result = unsafe_variant_cast<variant_t<T>>(&operand);
  360. using ref_type = conditional_t<std::is_reference<T>::value, T, add_lvalue_reference_t<T>>;
  361. return static_cast<ref_type>(*result);
  362. }
  363. /////////////////////////////////////////////////////////////////////////////////////////
  364. template<class T>
  365. RTTR_INLINE T variant_cast(variant& operand)
  366. {
  367. using namespace detail;
  368. static_assert(std::is_constructible<T, variant_t<T>&>::value,
  369. "variant_cast<T>(variant&) requires T to be constructible from remove_cv_t<remove_reference_t<T>>&");
  370. auto result = unsafe_variant_cast<variant_t<T>>(&operand);
  371. using ref_type = conditional_t<std::is_reference<T>::value, T, add_lvalue_reference_t<T>>;
  372. return static_cast<ref_type>(*result);
  373. }
  374. /////////////////////////////////////////////////////////////////////////////////////////
  375. template<class T>
  376. RTTR_INLINE T variant_cast(variant&& operand)
  377. {
  378. using namespace detail;
  379. static_assert(std::is_constructible<T, variant_t<T>>::value,
  380. "variant_cast<T>(variant&&) requires T to be constructible from remove_cv_t<remove_reference_t<T>>");
  381. auto result = unsafe_variant_cast<variant_t<T>>(&operand);
  382. return std::move(*result);
  383. }
  384. /////////////////////////////////////////////////////////////////////////////////////////
  385. template<class T>
  386. RTTR_INLINE T* variant_cast(variant* operand) RTTR_NOEXCEPT
  387. {
  388. using namespace detail;
  389. return (type::get<T>() == operand->get_type()) ?
  390. unsafe_variant_cast<T>(operand) : nullptr;
  391. }
  392. /////////////////////////////////////////////////////////////////////////////////////////
  393. template<class T>
  394. RTTR_INLINE const T* variant_cast(const variant* operand) RTTR_NOEXCEPT
  395. {
  396. return variant_cast<T>(const_cast<variant*>(operand));
  397. }
  398. /////////////////////////////////////////////////////////////////////////////////////////
  399. } // end namespace rttr
  400. #endif // RTTR_VARIANT_IMPL_H_