instance.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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_H_
  28. #define RTTR_INSTANCE_H_
  29. #include "rttr/detail/base/core_prerequisites.h"
  30. #include "rttr/detail/misc/misc_type_traits.h"
  31. #include "rttr/detail/misc/data_address_container.h"
  32. namespace rttr
  33. {
  34. class variant;
  35. class type;
  36. class argument;
  37. /*!
  38. * The \ref instance class is used for forwarding the instance of an object to invoke a \ref property or \ref method.
  39. *
  40. * \remark The \ref instance class will internally hold a reference to the object. It will not perform any copy operation on the data itself.
  41. * Make sure you don't hold an \ref instance, while the underlying object is already destroyed. Otherwise it will lead to undefined behavior.
  42. */
  43. class RTTR_API instance
  44. {
  45. template<typename T, typename Tp = detail::decay_t<T>>
  46. using decay_instance_t = detail::enable_if_t<!std::is_same<instance, Tp>::value &&
  47. !std::is_same<variant, Tp>::value, T>;
  48. public:
  49. /*!
  50. * \brief Creates an \ref instance::is_valid() "invalid" instance object.
  51. *
  52. * Use this constructor, when you need to invoke a property or method where no instance is required.
  53. */
  54. RTTR_INLINE instance() RTTR_NOEXCEPT;
  55. /*!
  56. * \brief Creates an instance object from a \ref variant object.
  57. */
  58. RTTR_INLINE instance(const variant& var) RTTR_NOEXCEPT;
  59. /*!
  60. * \brief Copy constructor for an instance.
  61. */
  62. RTTR_INLINE instance(const instance& other) RTTR_NOEXCEPT;
  63. /*!
  64. * \brief Creates an instance object from type \p T.
  65. *
  66. * \remark Internally, the instance class will hold a reference to the address of the given object \p data.
  67. */
  68. template<typename T, typename Tp = decay_instance_t<T>>
  69. RTTR_INLINE instance(T& data) RTTR_NOEXCEPT;
  70. /*!
  71. * \brief This function will try to convert the underlying instance to the given type \p Target_Type*.
  72. * When the conversion succeeds, a valid pointer will be returned. Otherwise a nullptr.
  73. *
  74. * \return A pointer to the instance of \p Target_Type, when the conversion succeeds, otherwise a nullptr.
  75. */
  76. template<typename Target_Type>
  77. RTTR_INLINE Target_Type* try_convert() const RTTR_NOEXCEPT;
  78. /*!
  79. * \brief Returns true when the instance class contains a reference to an object. Otherwise false.
  80. *
  81. * \return True when a reference is stored, otherwise false.
  82. */
  83. RTTR_INLINE bool is_valid() const RTTR_NOEXCEPT;
  84. /*!
  85. * \brief Returns true when the instance class contains a reference to an object. Otherwise false.
  86. *
  87. * \return True when a reference is stored, otherwise false.
  88. */
  89. explicit operator bool() const RTTR_NOEXCEPT;
  90. /*!
  91. * \brief Returns the type of the internally hold instance.
  92. *
  93. * \remark When no reference is stored, an \ref type::is_valid() "invalid" \ref type object will be returned.
  94. *
  95. * \return Type object of stored reference.
  96. */
  97. RTTR_INLINE type get_type() const RTTR_NOEXCEPT;
  98. /*!
  99. * \brief Returns an \ref instance object for the wrapped instance.
  100. *
  101. * See following example code:
  102. * \code{.cpp}
  103. * std::shared_ptr<foo> f;
  104. * instance obj = f;
  105. *
  106. * obj.get_type() == type::get<std::shared_ptr<foo>>(); // yields to true
  107. * obj.get_wrapped_instance().get_type() == type::get<foo>(); // yields to true
  108. * \endcode
  109. *
  110. * \remark When the current instance is not a \ref type::is_wrapper() "wrapper type",
  111. * an \ref instance::is_valid() "invalid" instance will be returned.
  112. *
  113. * \return An \ref instance object from the wrapped type.
  114. */
  115. RTTR_INLINE instance get_wrapped_instance() const RTTR_NOEXCEPT;
  116. /*!
  117. * \brief Returns the most derived type of the hold instance.
  118. *
  119. * See following example code:
  120. * \code{.cpp}
  121. * struct base { RTTR_ENABLE() };
  122. * struct middle : base { RTTR_ENABLE(base) };
  123. * struct derived : middle { RTTR_ENABLE(middle) };
  124. * //...
  125. * derived d;
  126. * base& b = d;
  127. * instance obj = b;
  128. *
  129. * obj.get_type() == type::get<base>(); // yields to true
  130. * obj.get_derived_type() == type::get<derived>(); // yields to true
  131. * \endcode
  132. */
  133. RTTR_INLINE type get_derived_type() const RTTR_NOEXCEPT;
  134. private:
  135. instance& operator=(const instance& other) RTTR_NOEXCEPT;
  136. detail::data_address_container m_data_container;
  137. };
  138. } // end namespace rttr
  139. #include "rttr/detail/impl/instance_impl.h"
  140. #endif // RTTR_INSTANCE_H_