argument_impl.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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_ARGUMENT_IMPL_H_
  28. #define RTTR_ARGUMENT_IMPL_H_
  29. #include "rttr/type.h"
  30. #include "rttr/variant.h"
  31. namespace rttr
  32. {
  33. /////////////////////////////////////////////////////////////////////////////////////////
  34. RTTR_INLINE argument::argument() RTTR_NOEXCEPT : m_data(nullptr), m_variant(nullptr), m_type(detail::get_invalid_type()) {}
  35. /////////////////////////////////////////////////////////////////////////////////////////
  36. RTTR_INLINE argument::argument(argument&& arg) RTTR_NOEXCEPT : m_data(arg.m_data), m_variant(arg.m_variant), m_type(arg.m_type) {}
  37. /////////////////////////////////////////////////////////////////////////////////////////
  38. RTTR_INLINE argument::argument(const argument& other) RTTR_NOEXCEPT : m_data(other.m_data), m_variant(other.m_variant), m_type(other.m_type) {}
  39. /////////////////////////////////////////////////////////////////////////////////////////
  40. RTTR_INLINE argument::argument(variant& var) RTTR_NOEXCEPT : m_data(var.get_ptr()), m_variant(&var), m_type(var.get_type()) {}
  41. /////////////////////////////////////////////////////////////////////////////////////////
  42. RTTR_INLINE argument::argument(const variant& var) RTTR_NOEXCEPT : m_data(var.get_ptr()), m_variant(&var), m_type(var.get_type()) {}
  43. /////////////////////////////////////////////////////////////////////////////////////////
  44. template<typename T, typename Tp>
  45. argument::argument(const T& data) RTTR_NOEXCEPT
  46. : m_data(reinterpret_cast<const void*>(std::addressof(data))),
  47. m_variant(nullptr),
  48. m_type(rttr::type::get<T>())
  49. {
  50. static_assert(!std::is_same<instance, T>::value, "Don't use the argument class for forwarding an instance!");
  51. }
  52. /////////////////////////////////////////////////////////////////////////////////////////
  53. template<typename T, typename Tp>
  54. argument::argument(T& data) RTTR_NOEXCEPT
  55. : m_data(reinterpret_cast<const void*>(std::addressof(data))),
  56. m_variant(nullptr),
  57. m_type(rttr::type::get<T>())
  58. {
  59. static_assert(!std::is_same<instance, T>::value, "Don't use the argument class for forwarding an instance!");
  60. }
  61. /////////////////////////////////////////////////////////////////////////////////////////
  62. template<typename T>
  63. RTTR_INLINE argument::ptr_type<T> argument::is_type() const RTTR_NOEXCEPT
  64. {
  65. return ((rttr::type::get<T>() == m_type) ||
  66. m_type == type::get<std::nullptr_t>() ||
  67. (m_variant && type::get<variant*>() == type::get<T>()));
  68. }
  69. /////////////////////////////////////////////////////////////////////////////////////////
  70. template<typename T>
  71. RTTR_INLINE argument::non_ptr_type<T> argument::is_type() const RTTR_NOEXCEPT
  72. {
  73. return (rttr::type::get<T>() == m_type ||
  74. (m_variant && type::get<variant>() == type::get<T>()));
  75. }
  76. /////////////////////////////////////////////////////////////////////////////////////////
  77. RTTR_INLINE type argument::get_type() const RTTR_NOEXCEPT
  78. {
  79. return m_type;
  80. }
  81. /////////////////////////////////////////////////////////////////////////////////////////
  82. template<typename T>
  83. RTTR_INLINE argument::arg_value_t<T>& argument::get_value() const RTTR_NOEXCEPT
  84. {
  85. using raw_type = typename std::remove_reference<T>::type;
  86. return (*reinterpret_cast<raw_type*>(const_cast<void *>(m_data)));
  87. }
  88. /////////////////////////////////////////////////////////////////////////////////////////
  89. template<typename T>
  90. RTTR_INLINE argument::arg_rvalue_t<T>&& argument::get_value() const RTTR_NOEXCEPT
  91. {
  92. using raw_type = typename std::remove_reference<T>::type;
  93. return std::move(*reinterpret_cast<raw_type*>(const_cast<void *>(m_data)));
  94. }
  95. /////////////////////////////////////////////////////////////////////////////////////////
  96. template<typename T>
  97. RTTR_INLINE argument::is_variant_t<T>& argument::get_value() const RTTR_NOEXCEPT
  98. {
  99. using raw_type = typename std::remove_reference<T>::type;
  100. return (*reinterpret_cast<raw_type*>(const_cast<variant *>(m_variant)));
  101. }
  102. /////////////////////////////////////////////////////////////////////////////////////////
  103. template<typename T>
  104. RTTR_INLINE argument::is_variant_ref_t<T>&& argument::get_value() const RTTR_NOEXCEPT
  105. {
  106. using raw_type = typename std::remove_reference<T>::type;
  107. return std::move(*reinterpret_cast<raw_type*>(const_cast<variant *>(m_variant)));
  108. }
  109. /////////////////////////////////////////////////////////////////////////////////////////
  110. RTTR_INLINE argument& argument::operator=(const argument& other) RTTR_NOEXCEPT
  111. {
  112. m_data = other.m_data;
  113. const_cast<rttr::type&>(m_type) = other.m_type;
  114. m_variant = other.m_variant;
  115. return *this;
  116. }
  117. /////////////////////////////////////////////////////////////////////////////////////////
  118. } // end namespace rttr
  119. #endif // RTTR_ARGUMENT_IMPL_H_