variant.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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.h"
  28. #include "rttr/detail/variant/variant_data_policy.h"
  29. #include "rttr/variant_associative_view.h"
  30. #include "rttr/variant_sequential_view.h"
  31. #include "rttr/argument.h"
  32. #include <algorithm>
  33. #include <limits>
  34. #include <string>
  35. #include <set>
  36. namespace rttr
  37. {
  38. /////////////////////////////////////////////////////////////////////////////////////////
  39. variant::variant(const variant& other)
  40. : m_policy(other.m_policy)
  41. {
  42. m_policy(detail::variant_policy_operation::CLONE, other.m_data, m_data);
  43. }
  44. /////////////////////////////////////////////////////////////////////////////////////////
  45. variant::variant(variant&& other)
  46. : m_policy(other.m_policy)
  47. {
  48. other.m_policy(detail::variant_policy_operation::SWAP, other.m_data, m_data);
  49. other.m_policy = &detail::variant_data_policy_empty::invoke;
  50. }
  51. /////////////////////////////////////////////////////////////////////////////////////////
  52. void variant::swap(variant& other)
  53. {
  54. if (this == &other)
  55. return;
  56. const bool is_this_valid = is_valid();
  57. const bool is_other_valid = other.is_valid();
  58. if (!is_this_valid && !is_other_valid)
  59. return;
  60. if (is_this_valid && is_other_valid)
  61. {
  62. detail::variant_data tmp_data;
  63. detail::variant_policy_func tmp_policy_func = other.m_policy;
  64. other.m_policy(detail::variant_policy_operation::SWAP, other.m_data, tmp_data);
  65. m_policy(detail::variant_policy_operation::SWAP, m_data, other.m_data);
  66. other.m_policy = m_policy;
  67. tmp_policy_func(detail::variant_policy_operation::SWAP, tmp_data, m_data);
  68. m_policy = tmp_policy_func;
  69. }
  70. else
  71. {
  72. detail::variant_data& full_data = is_this_valid ? m_data : other.m_data;
  73. detail::variant_data& empty_data = is_this_valid ? other.m_data : m_data;
  74. detail::variant_policy_func full_policy_func = is_this_valid ? m_policy : other.m_policy;
  75. full_policy_func(detail::variant_policy_operation::SWAP, full_data, empty_data);
  76. std::swap(m_policy, other.m_policy);
  77. }
  78. }
  79. /////////////////////////////////////////////////////////////////////////////////////////
  80. variant& variant::operator=(const variant& other)
  81. {
  82. if (this == &other)
  83. return *this;
  84. m_policy(detail::variant_policy_operation::DESTROY, m_data, detail::argument_wrapper());
  85. other.m_policy(detail::variant_policy_operation::CLONE, other.m_data, m_data);
  86. m_policy = other.m_policy;
  87. return *this;
  88. }
  89. /////////////////////////////////////////////////////////////////////////////////////////
  90. variant& variant::operator=(variant&& other)
  91. {
  92. m_policy(detail::variant_policy_operation::DESTROY, m_data, detail::argument_wrapper());
  93. other.m_policy(detail::variant_policy_operation::SWAP, other.m_data, m_data);
  94. m_policy = other.m_policy;
  95. other.m_policy = &detail::variant_data_policy_empty::invoke;
  96. return *this;
  97. }
  98. /////////////////////////////////////////////////////////////////////////////////////////
  99. bool variant::compare_equal(const variant& other, bool& ok) const
  100. {
  101. ok = false;
  102. return m_policy(detail::variant_policy_operation::COMPARE_EQUAL, m_data, std::tie(*this, other, ok));
  103. }
  104. /////////////////////////////////////////////////////////////////////////////////////////
  105. bool variant::compare_less(const variant& other, bool& ok) const
  106. {
  107. return m_policy(detail::variant_policy_operation::COMPARE_LESS, m_data, std::tie(*this, other, ok));
  108. }
  109. /////////////////////////////////////////////////////////////////////////////////////////
  110. void variant::clear()
  111. {
  112. m_policy(detail::variant_policy_operation::DESTROY, m_data, detail::argument_wrapper());
  113. m_policy = &detail::variant_data_policy_empty::invoke;
  114. }
  115. /////////////////////////////////////////////////////////////////////////////////////////
  116. bool variant::is_valid() const
  117. {
  118. return m_policy(detail::variant_policy_operation::IS_VALID, m_data, detail::argument_wrapper());
  119. }
  120. /////////////////////////////////////////////////////////////////////////////////////////
  121. variant::operator bool() const
  122. {
  123. return m_policy(detail::variant_policy_operation::IS_VALID, m_data, detail::argument_wrapper());
  124. }
  125. /////////////////////////////////////////////////////////////////////////////////////////
  126. bool variant::is_associative_container() const
  127. {
  128. return m_policy(detail::variant_policy_operation::IS_ASSOCIATIVE_CONTAINER, m_data, detail::argument_wrapper());
  129. }
  130. /////////////////////////////////////////////////////////////////////////////////////////
  131. bool variant::is_sequential_container() const
  132. {
  133. return m_policy(detail::variant_policy_operation::IS_SEQUENTIAL_CONTAINER, m_data, detail::argument_wrapper());
  134. }
  135. /////////////////////////////////////////////////////////////////////////////////////////
  136. type variant::get_type() const
  137. {
  138. type src_type = detail::get_invalid_type();
  139. m_policy(detail::variant_policy_operation::GET_TYPE, m_data, src_type);
  140. return src_type;
  141. }
  142. /////////////////////////////////////////////////////////////////////////////////////////
  143. variant variant::extract_wrapped_value() const
  144. {
  145. variant var;
  146. m_policy(detail::variant_policy_operation::EXTRACT_WRAPPED_VALUE, m_data, var);
  147. return var;
  148. }
  149. /////////////////////////////////////////////////////////////////////////////////////////
  150. variant variant::create_wrapped_value(const type& wrapped_type) const
  151. {
  152. variant var;
  153. m_policy(detail::variant_policy_operation::CREATE_WRAPPED_VALUE, m_data, std::tie(var, wrapped_type));
  154. return var;
  155. }
  156. /////////////////////////////////////////////////////////////////////////////////////////
  157. variant_associative_view variant::create_associative_view() const
  158. {
  159. variant_associative_view result;
  160. m_policy(detail::variant_policy_operation::CREATE_ASSOCIATIV_VIEW, m_data, result.m_view);
  161. return result;
  162. }
  163. /////////////////////////////////////////////////////////////////////////////////////////
  164. variant_sequential_view variant::create_sequential_view() const
  165. {
  166. variant_sequential_view result;
  167. m_policy(detail::variant_policy_operation::CREATE_SEQUENTIAL_VIEW, m_data, result.m_view);
  168. return result;
  169. }
  170. /////////////////////////////////////////////////////////////////////////////////////////
  171. bool variant::can_convert(const type& target_type) const
  172. {
  173. if (!is_valid())
  174. return false;
  175. type source_type = get_type();
  176. source_type = (source_type.is_wrapper() && !target_type.is_wrapper()) ? source_type.get_wrapped_type() : source_type;
  177. if (source_type == target_type)
  178. return true;
  179. if (source_type.get_pointer_dimension() == 1 && target_type.get_pointer_dimension() == 1)
  180. {
  181. if (type::apply_offset(get_raw_ptr(), source_type, target_type))
  182. return true;
  183. }
  184. if (!source_type.is_wrapper() && target_type.is_wrapper())
  185. {
  186. if (target_type.get_wrapped_type() == source_type && target_type.m_type_data->create_wrapper)
  187. return true;
  188. }
  189. if (source_type.get_type_converter(target_type))
  190. return true;
  191. if (target_type == type::get<std::nullptr_t>() && is_nullptr())
  192. return true;
  193. const bool source_is_arithmetic = source_type.is_arithmetic();
  194. const bool target_is_arithmetic = target_type.is_arithmetic();
  195. const bool target_is_enumeration = target_type.is_enumeration();
  196. const type string_type = type::get<std::string>();
  197. return ((source_is_arithmetic && target_is_arithmetic) ||
  198. (source_is_arithmetic && target_type == string_type) ||
  199. (source_type == string_type && target_is_arithmetic) ||
  200. (source_type.is_enumeration() && target_is_arithmetic) ||
  201. (source_is_arithmetic && target_is_enumeration) ||
  202. (source_type == string_type && target_is_enumeration));
  203. }
  204. /////////////////////////////////////////////////////////////////////////////////////////
  205. bool variant::convert(const type& target_type, variant& target_var) const
  206. {
  207. if (!is_valid())
  208. return false;
  209. bool ok = false;
  210. const type source_type = get_type();
  211. const bool source_is_arithmetic = source_type.is_arithmetic();
  212. const bool target_is_arithmetic = target_type.is_arithmetic();
  213. const type string_type = type::get<std::string>();
  214. if (target_type == source_type)
  215. {
  216. target_var = *this;
  217. return true; // the current variant is already the target type, we don't need to do anything
  218. }
  219. else if (!source_type.is_wrapper() && target_type.is_wrapper() &&
  220. target_type.get_wrapped_type() == source_type)
  221. {
  222. target_var = create_wrapped_value(target_type);
  223. ok = target_var.is_valid();
  224. }
  225. else if (source_type.is_wrapper() && !target_type.is_wrapper())
  226. {
  227. variant var = extract_wrapped_value();
  228. ok = var.convert(target_type);
  229. target_var = var;
  230. }
  231. else if ((source_is_arithmetic && target_is_arithmetic) ||
  232. (source_is_arithmetic && target_type == string_type) ||
  233. (source_type == string_type && target_is_arithmetic) ||
  234. (source_type.is_enumeration() && target_is_arithmetic) ||
  235. (source_type.is_enumeration() && target_type == string_type))
  236. {
  237. if (target_type == type::get<bool>())
  238. {
  239. bool value;
  240. if ((ok = try_basic_type_conversion(value)) == true)
  241. target_var = value;
  242. }
  243. else if (target_type == type::get<char>())
  244. {
  245. char value;
  246. if ((ok = try_basic_type_conversion(value)) == true)
  247. target_var = value;
  248. }
  249. else if (target_type == type::get<int8_t>())
  250. {
  251. int8_t value;
  252. if ((ok = try_basic_type_conversion(value)) == true)
  253. target_var = value;
  254. }
  255. else if (target_type == type::get<int16_t>())
  256. {
  257. int16_t value;
  258. if ((ok = try_basic_type_conversion(value)) == true)
  259. target_var = value;
  260. }
  261. else if (target_type == type::get<int32_t>())
  262. {
  263. int32_t value;
  264. if ((ok = try_basic_type_conversion(value)) == true)
  265. target_var = value;
  266. }
  267. else if (target_type == type::get<int64_t>())
  268. {
  269. int64_t value;
  270. if ((ok = try_basic_type_conversion(value)) == true)
  271. target_var = value;
  272. }
  273. else if (target_type == type::get<uint8_t>())
  274. {
  275. uint8_t value;
  276. if ((ok = try_basic_type_conversion(value)) == true)
  277. target_var = value;
  278. }
  279. else if (target_type == type::get<uint16_t>())
  280. {
  281. uint16_t value;
  282. if ((ok = try_basic_type_conversion(value)) == true)
  283. target_var = value;
  284. }
  285. else if (target_type == type::get<uint32_t>())
  286. {
  287. uint32_t value;
  288. if ((ok = try_basic_type_conversion(value)) == true)
  289. target_var = value;
  290. }
  291. else if (target_type == type::get<uint64_t>())
  292. {
  293. uint64_t value;
  294. if ((ok = try_basic_type_conversion(value)) == true)
  295. target_var = value;
  296. }
  297. else if (target_type == type::get<float>())
  298. {
  299. float value;
  300. if ((ok = try_basic_type_conversion(value)) == true)
  301. target_var = value;
  302. }
  303. else if (target_type == type::get<double>())
  304. {
  305. double value;
  306. if ((ok = try_basic_type_conversion(value)) == true)
  307. target_var = value;
  308. }
  309. else if (target_type == string_type)
  310. {
  311. std::string value;
  312. if ((ok = try_basic_type_conversion(value)) == true)
  313. target_var = std::move(value);
  314. }
  315. }
  316. else if ((source_is_arithmetic || source_type == string_type)
  317. && target_type.is_enumeration())
  318. {
  319. variant var = target_type;
  320. auto wrapper = std::ref(var);
  321. if ((ok = try_basic_type_conversion(wrapper)) == true)
  322. target_var = std::move(var);
  323. }
  324. else
  325. {
  326. if (const auto& converter = source_type.get_type_converter(target_type))
  327. {
  328. void* ptr = get_ptr();
  329. target_var = converter->to_variant(ptr, ok);
  330. }
  331. else if (target_type == type::get<std::nullptr_t>() && is_nullptr())
  332. {
  333. target_var = nullptr;
  334. ok = true;
  335. }
  336. else if (source_type.is_pointer() &&
  337. (source_type.get_pointer_dimension() == 1 && target_type.get_pointer_dimension() == 1))
  338. {
  339. void* raw_ptr = get_raw_ptr();
  340. if (void* casted_ptr = type::apply_offset(raw_ptr, source_type, target_type))
  341. {
  342. // although we forward a void* to create a variant,
  343. // it will create a variant for the specific class type
  344. target_var = target_type.create_variant(casted_ptr);
  345. if (target_var.is_valid())
  346. ok = true;
  347. }
  348. }
  349. }
  350. return ok;
  351. }
  352. /////////////////////////////////////////////////////////////////////////////////////////
  353. bool variant::convert(const type& target_type)
  354. {
  355. return convert(target_type, *this);
  356. }
  357. /////////////////////////////////////////////////////////////////////////////////////////
  358. bool variant::to_bool() const
  359. {
  360. return convert<bool>(nullptr);
  361. }
  362. /////////////////////////////////////////////////////////////////////////////////////////
  363. int variant::to_int(bool *ok) const
  364. {
  365. return convert<int>(ok);
  366. }
  367. /////////////////////////////////////////////////////////////////////////////////////////
  368. std::string variant::to_string(bool *ok) const
  369. {
  370. return convert<std::string>(ok);
  371. }
  372. /////////////////////////////////////////////////////////////////////////////////////////
  373. float variant::to_float(bool* ok) const
  374. {
  375. return convert<float>(ok);
  376. }
  377. /////////////////////////////////////////////////////////////////////////////////////////
  378. double variant::to_double(bool* ok) const
  379. {
  380. return convert<double>(ok);
  381. }
  382. /////////////////////////////////////////////////////////////////////////////////////////
  383. int8_t variant::to_int8(bool *ok) const
  384. {
  385. return convert<int8_t>(ok);
  386. }
  387. /////////////////////////////////////////////////////////////////////////////////////////
  388. int16_t variant::to_int16(bool *ok) const
  389. {
  390. return convert<int16_t>(ok);
  391. }
  392. /////////////////////////////////////////////////////////////////////////////////////////
  393. int32_t variant::to_int32(bool *ok) const
  394. {
  395. return convert<int32_t>(ok);
  396. }
  397. /////////////////////////////////////////////////////////////////////////////////////////
  398. int64_t variant::to_int64(bool *ok) const
  399. {
  400. return convert<int64_t>(ok);
  401. }
  402. /////////////////////////////////////////////////////////////////////////////////////////
  403. uint8_t variant::to_uint8(bool *ok) const
  404. {
  405. return convert<uint8_t>(ok);
  406. }
  407. /////////////////////////////////////////////////////////////////////////////////////////
  408. uint16_t variant::to_uint16(bool *ok) const
  409. {
  410. return convert<uint16_t>(ok);
  411. }
  412. /////////////////////////////////////////////////////////////////////////////////////////
  413. uint32_t variant::to_uint32(bool *ok) const
  414. {
  415. return convert<uint32_t>(ok);
  416. }
  417. /////////////////////////////////////////////////////////////////////////////////////////
  418. uint64_t variant::to_uint64(bool *ok) const
  419. {
  420. return convert<uint64_t>(ok);
  421. }
  422. /////////////////////////////////////////////////////////////////////////////////////////
  423. } // end namespace rttr