variant_compare.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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/detail/variant/variant_compare.h"
  28. #include "rttr/type.h"
  29. #include "rttr/variant.h"
  30. #include <cmath>
  31. #include <algorithm>
  32. namespace rttr
  33. {
  34. namespace detail
  35. {
  36. /////////////////////////////////////////////////////////////////////////////////////////
  37. static RTTR_INLINE bool is_floating_point(const type& type)
  38. {
  39. return (type == type::get<float>() || type == type::get<double>());
  40. }
  41. /////////////////////////////////////////////////////////////////////////////////////////
  42. static RTTR_INLINE bool almost_equal(double p1, double p2)
  43. {
  44. return (std::abs(p1 - p2) * 1000000000000. <= std::min(std::abs(p1), std::abs(p2)));
  45. }
  46. /////////////////////////////////////////////////////////////////////////////////////////
  47. bool variant_compare_equal(const variant& lhs, const type& lhs_type, const variant& rhs, const type& rhs_type, bool& ok)
  48. {
  49. ok = true;
  50. if (is_floating_point(lhs_type) || is_floating_point(rhs_type))
  51. {
  52. return almost_equal(lhs.to_double(), rhs.to_double());
  53. }
  54. else
  55. {
  56. return (lhs.to_int64() == rhs.to_int64());
  57. }
  58. }
  59. /////////////////////////////////////////////////////////////////////////////////////////
  60. bool variant_compare_less(const variant& lhs, const type& lhs_type, const variant& rhs, const type& rhs_type, bool& ok)
  61. {
  62. ok = true;
  63. if (lhs_type.is_arithmetic() && rhs_type.is_arithmetic())
  64. {
  65. if (is_floating_point(lhs_type) || is_floating_point(rhs_type))
  66. return (lhs.to_double() < rhs.to_double());
  67. else
  68. return (lhs.to_int64() < rhs.to_int64());
  69. }
  70. else
  71. {
  72. variant lhs_tmp;
  73. if (lhs.convert(rhs_type, lhs_tmp))
  74. return lhs_tmp.compare_less(rhs, ok);
  75. if (!lhs.is_nullptr() && rhs.is_nullptr())
  76. return false;
  77. // as last try, do a string conversion
  78. bool ok1 = false;
  79. bool ok2 = false;
  80. auto ret = (lhs.to_string(&ok1) < rhs.to_string(&ok2));
  81. if (ok1 && ok2)
  82. {
  83. return ret;
  84. }
  85. else
  86. {
  87. ok = false;
  88. return false;
  89. }
  90. }
  91. }
  92. /////////////////////////////////////////////////////////////////////////////////////////
  93. } // end namespace detail
  94. } // end namespace rttr