library.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. #include "rttr/library.h"
  28. #include "rttr/detail/library/library_p.h"
  29. #include <map>
  30. #include <mutex>
  31. namespace rttr
  32. {
  33. namespace detail
  34. {
  35. /////////////////////////////////////////////////////////////////////////////////////////
  36. /*!
  37. * A simple manager class, to hold all created library_private pointer objects. A private singleton.
  38. * After application exit it will unload all libs without any reference.
  39. */
  40. class library_manager
  41. {
  42. public:
  43. static std::shared_ptr<library_private> create_or_find_library(string_view file_name, string_view version)
  44. {
  45. auto& manager = get_instance();
  46. std::lock_guard<std::mutex> lock(manager.m_library_mutex);
  47. auto file_as_string = file_name.to_string();
  48. auto itr = manager.m_library_map.find(file_as_string);
  49. if (itr != manager.m_library_map.end())
  50. return itr->second;
  51. auto lib = std::make_shared<library_private>(file_name, version);
  52. manager.m_library_map.emplace(std::move(file_as_string), lib);
  53. return lib;
  54. }
  55. ~library_manager()
  56. {
  57. clean_all_libs();
  58. }
  59. static void remove_item(const std::shared_ptr<library_private>& item)
  60. {
  61. auto& manager = get_instance();
  62. std::lock_guard<std::mutex> lock(manager.m_library_mutex);
  63. auto itr = manager.m_library_map.find(item->get_file_name().to_string()); // because we use string_view to find the item
  64. if (itr != manager.m_library_map.end())
  65. manager.m_library_map.erase(itr);
  66. }
  67. private:
  68. library_manager() {}
  69. static library_manager& get_instance()
  70. {
  71. static library_manager obj;
  72. return obj;
  73. }
  74. void clean_all_libs()
  75. {
  76. for (auto& item : m_library_map)
  77. {
  78. // only the manager holds library pointer, thus we can now unload
  79. if (item.second.use_count() == 1)
  80. {
  81. item.second->set_load_count(1); // this will implicit force an unload of the library
  82. item.second->unload();
  83. }
  84. }
  85. }
  86. // use std::less in order to use string_view for finding the item
  87. std::map<std::string, std::shared_ptr<library_private>> m_library_map;
  88. std::mutex m_library_mutex;
  89. };
  90. }
  91. /////////////////////////////////////////////////////////////////////////////////////////
  92. library::library(string_view file_name, string_view version)
  93. : m_pimpl(detail::library_manager::create_or_find_library(file_name, version)),
  94. m_is_loaded(false)
  95. {
  96. }
  97. /////////////////////////////////////////////////////////////////////////////////////////
  98. library::~library()
  99. {
  100. // the library_manager holds an instance too, so the use_count is always >= 2
  101. // when 2 are left, it means there exist only one "library" instance
  102. if (m_pimpl.use_count() == 2 && m_pimpl->get_load_count() == 0)
  103. detail::library_manager::remove_item(m_pimpl);
  104. }
  105. /////////////////////////////////////////////////////////////////////////////////////////
  106. bool library::load()
  107. {
  108. if (m_is_loaded)
  109. return m_pimpl->is_loaded();
  110. m_is_loaded = true;
  111. return m_pimpl->load();
  112. }
  113. /////////////////////////////////////////////////////////////////////////////////////////
  114. bool library::unload()
  115. {
  116. if (m_is_loaded)
  117. {
  118. m_is_loaded = false;
  119. return m_pimpl->unload();
  120. }
  121. else
  122. {
  123. return false;
  124. }
  125. }
  126. /////////////////////////////////////////////////////////////////////////////////////////
  127. bool library::is_loaded() const RTTR_NOEXCEPT
  128. {
  129. return m_pimpl->is_loaded();
  130. }
  131. /////////////////////////////////////////////////////////////////////////////////////////
  132. string_view library::get_file_name() const RTTR_NOEXCEPT
  133. {
  134. return m_pimpl->get_qualified_filename().empty() ? m_pimpl->get_file_name() :
  135. m_pimpl->get_qualified_filename();
  136. }
  137. /////////////////////////////////////////////////////////////////////////////////////////
  138. string_view library::get_error_string() const RTTR_NOEXCEPT
  139. {
  140. return m_pimpl->get_error_string();
  141. }
  142. /////////////////////////////////////////////////////////////////////////////////////////
  143. array_range<type> library::get_types() const RTTR_NOEXCEPT
  144. {
  145. return m_pimpl->get_types();
  146. }
  147. /////////////////////////////////////////////////////////////////////////////////////////
  148. array_range<property> library::get_global_properties() const RTTR_NOEXCEPT
  149. {
  150. return m_pimpl->get_global_properties();
  151. }
  152. /////////////////////////////////////////////////////////////////////////////////////////
  153. array_range<method> library::get_global_methods() const RTTR_NOEXCEPT
  154. {
  155. return m_pimpl->get_global_methods();
  156. }
  157. /////////////////////////////////////////////////////////////////////////////////////////
  158. } // end namespace rttr