library_p.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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_LIBRARY_P_H_
  28. #define RTTR_LIBRARY_P_H_
  29. #include "rttr/detail/base/core_prerequisites.h"
  30. #include "rttr/string_view.h"
  31. #include "rttr/type.h"
  32. #include "rttr/property.h"
  33. #include "rttr/method.h"
  34. #include "rttr/detail/registration/registration_state_saver.h"
  35. #if RTTR_PLATFORM == RTTR_PLATFORM_WINDOWS
  36. #include <windows.h>
  37. #endif
  38. #include <atomic>
  39. namespace rttr
  40. {
  41. namespace detail
  42. {
  43. /*!
  44. * This class contains the private implementation of the \ref library class.
  45. *
  46. */
  47. class RTTR_LOCAL library_private
  48. {
  49. public:
  50. library_private(string_view file_name, string_view version)
  51. : m_file_name(file_name),
  52. m_version(version),
  53. m_load_count(0),
  54. m_handle(nullptr)
  55. {
  56. }
  57. ~library_private() = default;
  58. bool load()
  59. {
  60. if (m_handle)
  61. {
  62. ++m_load_count;
  63. return true;
  64. }
  65. m_state_saver.save_state_begin();
  66. auto result = load_native();
  67. if (result)
  68. {
  69. ++m_load_count;
  70. m_state_saver.save_state_end();
  71. }
  72. else
  73. {
  74. m_state_saver.reset();
  75. }
  76. return result;
  77. }
  78. bool unload()
  79. {
  80. if (!m_handle)
  81. return false;
  82. --m_load_count;
  83. if (m_load_count.load() == 0)
  84. {
  85. auto ret = unload_native();
  86. if (ret)
  87. {
  88. m_error_string.clear();
  89. m_state_saver.reset();
  90. m_handle = nullptr;
  91. }
  92. }
  93. return (m_handle == nullptr);
  94. }
  95. /////////////////////////////////////////////////////////////
  96. bool is_loaded() const RTTR_NOEXCEPT { return (m_handle != nullptr); }
  97. string_view get_error_string() const RTTR_NOEXCEPT { return m_error_string; }
  98. string_view get_file_name() const RTTR_NOEXCEPT { return m_file_name; }
  99. string_view get_qualified_filename() const RTTR_NOEXCEPT { return m_qualifed_file_name; }
  100. /////////////////////////////////////////////////////////////
  101. array_range<type> get_types() const RTTR_NOEXCEPT { return m_state_saver.get_types(); }
  102. array_range<property> get_global_properties() const RTTR_NOEXCEPT { return m_state_saver.get_global_properties(); }
  103. array_range<method> get_global_methods() const RTTR_NOEXCEPT { return m_state_saver.get_global_methods(); }
  104. int get_load_count() const RTTR_NOEXCEPT { return m_load_count.load(); }
  105. void set_load_count(int count) { m_load_count.store(count); }
  106. private:
  107. bool load_native();
  108. bool unload_native();
  109. private:
  110. std::string m_file_name;
  111. std::string m_version;
  112. std::string m_qualifed_file_name;
  113. std::string m_error_string;
  114. registration_state_saver m_state_saver;
  115. std::atomic_int m_load_count;
  116. #if RTTR_PLATFORM == RTTR_PLATFORM_WINDOWS
  117. HMODULE
  118. #else
  119. void*
  120. #endif
  121. m_handle;
  122. };
  123. } // end namespace detail
  124. } // end namespace rttr
  125. #endif // RTTR_LIBRARY_P_H_