instance_impl.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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_INSTANCE_IMPL_H_
  28. #define RTTR_INSTANCE_IMPL_H_
  29. #include "rttr/variant.h"
  30. #include "rttr/type.h"
  31. #include "rttr/wrapper_mapper.h"
  32. #include "rttr/detail/misc/misc_type_traits.h"
  33. namespace rttr
  34. {
  35. /////////////////////////////////////////////////////////////////////////////////////////
  36. RTTR_INLINE instance::instance() RTTR_NOEXCEPT
  37. : m_data_container(detail::data_address_container{detail::get_invalid_type(), detail::get_invalid_type(), nullptr, nullptr})
  38. {
  39. }
  40. /////////////////////////////////////////////////////////////////////////////////////////
  41. RTTR_INLINE instance::instance(const variant& var) RTTR_NOEXCEPT
  42. : m_data_container(var.get_data_address_container())
  43. {
  44. }
  45. /////////////////////////////////////////////////////////////////////////////////////////
  46. RTTR_INLINE instance::instance(const instance& other) RTTR_NOEXCEPT
  47. : m_data_container(other.m_data_container)
  48. {
  49. }
  50. /////////////////////////////////////////////////////////////////////////////////////////
  51. template<typename T, typename Tp>
  52. RTTR_INLINE instance::instance(T& data) RTTR_NOEXCEPT
  53. : m_data_container(detail::data_address_container{
  54. rttr::type::get<T>(), rttr::type::get<detail::wrapper_mapper_t<T>>(),
  55. detail::as_void_ptr(detail::raw_addressof(data)), detail::as_void_ptr(detail::wrapped_raw_addressof(data))})
  56. {
  57. static_assert(!std::is_same<argument, T>::value, "Don't use the instance class for forwarding an argument!");
  58. }
  59. /////////////////////////////////////////////////////////////////////////////////////////
  60. template<typename Target_Type>
  61. RTTR_INLINE Target_Type* instance::try_convert() const RTTR_NOEXCEPT
  62. {
  63. Target_Type* target = static_cast<Target_Type*>(type::apply_offset(const_cast<instance*>(this)->m_data_container.m_data_address, m_data_container.m_type, type::get<Target_Type>()));
  64. if (!target)
  65. return (static_cast<Target_Type*>(type::apply_offset(const_cast<instance*>(this)->m_data_container.m_data_address_wrapped_type, m_data_container.m_wrapped_type, type::get<Target_Type>())));
  66. return target;
  67. }
  68. /////////////////////////////////////////////////////////////////////////////////////////
  69. RTTR_INLINE bool instance::is_valid() const RTTR_NOEXCEPT { return (m_data_container.m_data_address != nullptr); }
  70. /////////////////////////////////////////////////////////////////////////////////////////
  71. RTTR_INLINE instance::operator bool() const RTTR_NOEXCEPT { return (m_data_container.m_data_address != nullptr); }
  72. /////////////////////////////////////////////////////////////////////////////////////////
  73. RTTR_INLINE type instance::get_type() const RTTR_NOEXCEPT { return m_data_container.m_type; }
  74. /////////////////////////////////////////////////////////////////////////////////////////
  75. RTTR_INLINE instance instance::get_wrapped_instance() const RTTR_NOEXCEPT
  76. {
  77. instance obj;
  78. if (m_data_container.m_data_address != m_data_container.m_data_address_wrapped_type)
  79. {
  80. obj.m_data_container.m_data_address = m_data_container.m_data_address_wrapped_type;
  81. obj.m_data_container.m_type = m_data_container.m_wrapped_type;
  82. }
  83. return obj;
  84. }
  85. /////////////////////////////////////////////////////////////////////////////////////////
  86. RTTR_INLINE type instance::get_derived_type() const RTTR_NOEXCEPT
  87. {
  88. return type::get_derived_type(const_cast<instance*>(this)->m_data_container.m_data_address, m_data_container.m_type);
  89. }
  90. /////////////////////////////////////////////////////////////////////////////////////////
  91. } // end namespace rttr
  92. #endif // RTTR_INSTANCE_IMPL_H_