destructor.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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_DESTRUCTOR_H_
  28. #define RTTR_DESTRUCTOR_H_
  29. #include "rttr/detail/base/core_prerequisites.h"
  30. #include "rttr/detail/misc/class_item_mapper.h"
  31. namespace rttr
  32. {
  33. class variant;
  34. class type;
  35. namespace detail
  36. {
  37. class destructor_wrapper_base;
  38. template<>
  39. RTTR_API destructor create_invalid_item();
  40. }
  41. /*!
  42. * The \ref destructor class provides a destructor for registered types.
  43. *
  44. * A instance of a destructor class can only be obtained from the \ref type class.
  45. * See \ref type::get_destructor().
  46. *
  47. * Copying and Assignment
  48. * ----------------------
  49. * A \ref destructor object is lightweight and can be copied by value. However, each copy will refer to the same underlying destructor.
  50. *
  51. * Typical Usage
  52. * ----------------------
  53. *
  54. * \code{.cpp}
  55. * constructor string_ctor == type::get_by_name("std::string").get_constructor({type::get<const char*>()});
  56. *
  57. * variant my_string = string_ctor.invoke("Hello World"); // returns an ptr to the object on the heap
  58. *
  59. * type::get("std::string").get_destructor().invoke(my_string);
  60. *
  61. * my_string.is_valid(); // yield to false
  62. * \endcode
  63. *
  64. * \see method, property, enumeration, constructor and type
  65. */
  66. class RTTR_API destructor
  67. {
  68. public:
  69. /*!
  70. * \brief Returns true whether this destructor object is valid; otherwise false.
  71. *
  72. * \return Returns true when the destructor is valid; otherwise false.
  73. */
  74. bool is_valid() const RTTR_NOEXCEPT;
  75. /*!
  76. * \brief Convenience function to check if this destructor is valid or not.
  77. *
  78. * \return True if this destructor is valid, otherwise false.
  79. */
  80. explicit operator bool() const RTTR_NOEXCEPT;
  81. /*!
  82. * Returns the class that declares this destructor.
  83. *
  84. * \remark When this destructor is not valid, this function will return an invalid type object (see \ref type::is_valid).
  85. *
  86. * \return \ref type "Type" of the declaring class/struct for this destructor.
  87. */
  88. type get_declaring_type() const RTTR_NOEXCEPT;
  89. /*!
  90. * \brief Returns the rttr::type for which this destructor can delete objects.
  91. *
  92. * \return The type of this destructor.
  93. */
  94. type get_destructed_type() const RTTR_NOEXCEPT;
  95. /*!
  96. * \brief Destroys the contained object in the variant \p obj.
  97. *
  98. * \remark When the \p obj could be destroyed the given \p obj is invalid after calling this method;
  99. * Otherwise it is still valid.
  100. *
  101. * \return True if the destructor of the object could be invoked, otherwise false.
  102. */
  103. bool invoke(variant& obj) const RTTR_NOEXCEPT;
  104. /*!
  105. * \brief Returns true if this destructor is the same like the \p other.
  106. *
  107. * \return True if both destructors are equal, otherwise false.
  108. */
  109. bool operator==(const destructor& other) const RTTR_NOEXCEPT;
  110. /*!
  111. * Returns true if this destructor is the not the same like the \p other.
  112. *
  113. * \return True if both destructors are different, otherwise false.
  114. */
  115. bool operator!=(const destructor& other) const RTTR_NOEXCEPT;
  116. private:
  117. destructor(const detail::destructor_wrapper_base* wrapper) RTTR_NOEXCEPT;
  118. template<typename T>
  119. friend T detail::create_item(const detail::class_item_to_wrapper_t<T>* wrapper);
  120. template<typename T>
  121. friend T detail::create_invalid_item();
  122. private:
  123. const detail::destructor_wrapper_base* m_wrapper;
  124. };
  125. } // end namespace rttr
  126. #endif // RTTR_DESTRUCTOR_H_