registration_manager.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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_REGISTRATION_MANAGER_H_
  28. #define RTTR_REGISTRATION_MANAGER_H_
  29. #include "rttr/detail/base/core_prerequisites.h"
  30. #include "rttr/detail/type/type_register.h"
  31. #include "rttr/detail/property/property_wrapper_base.h"
  32. #include "rttr/detail/method/method_wrapper_base.h"
  33. #include "rttr/detail/constructor/constructor_wrapper_base.h"
  34. #include "rttr/detail/destructor/destructor_wrapper_base.h"
  35. #include "rttr/detail/enumeration/enumeration_wrapper_base.h"
  36. #include <vector>
  37. #include <memory>
  38. namespace rttr
  39. {
  40. namespace detail
  41. {
  42. /*!
  43. * This class saves the registration of all possible items per module (*.DLL, *.so, ...)
  44. * and will undo the registration when the instance is destroyed.
  45. */
  46. class RTTR_LOCAL registration_manager
  47. {
  48. public:
  49. registration_manager()
  50. {
  51. type_register::register_reg_manager(this);
  52. }
  53. ~registration_manager()
  54. {
  55. unregister();
  56. }
  57. type_data* add_item(std::unique_ptr<type_data> obj)
  58. {
  59. auto reg_type = type_register::register_type(obj.get());
  60. const auto was_type_stored = (reg_type == obj.get());
  61. if (was_type_stored)
  62. m_type_data_list.push_back(std::move(obj)); // so we have to unregister it later
  63. return reg_type;
  64. }
  65. void add_item(std::unique_ptr<constructor_wrapper_base> ctor)
  66. {
  67. if (type_register::register_constructor(ctor.get()))
  68. m_constructors.push_back(std::move(ctor));
  69. }
  70. void add_item(std::unique_ptr<destructor_wrapper_base> dtor)
  71. {
  72. if (type_register::register_destructor(dtor.get()))
  73. m_destructors.push_back(std::move(dtor));
  74. }
  75. void add_item(std::unique_ptr<property_wrapper_base> prop)
  76. {
  77. if (type_register::register_property(prop.get()))
  78. m_properties.push_back(std::move(prop));
  79. }
  80. void add_item(std::unique_ptr<method_wrapper_base> meth)
  81. {
  82. if (type_register::register_method(meth.get()))
  83. m_methods.push_back(std::move(meth));
  84. }
  85. void add_item(std::unique_ptr<enumeration_wrapper_base> enum_)
  86. {
  87. if (type_register::register_enumeration(enum_.get()))
  88. m_enumerations.push_back(std::move(enum_));
  89. }
  90. void add_global_item(std::unique_ptr<property_wrapper_base> prop)
  91. {
  92. if (type_register::register_global_property(prop.get()))
  93. m_global_properties.push_back(std::move(prop));
  94. }
  95. void add_global_item(std::unique_ptr<method_wrapper_base> meth)
  96. {
  97. if (type_register::register_global_method(meth.get()))
  98. m_global_methods.push_back(std::move(meth));
  99. }
  100. void add_global_item(std::unique_ptr<enumeration_wrapper_base> enum_)
  101. {
  102. if (type_register::register_enumeration(enum_.get()))
  103. m_enumerations.push_back(std::move(enum_));
  104. }
  105. void add_item(std::unique_ptr<type_converter_base> conv)
  106. {
  107. if (type_register::register_converter(conv.get()))
  108. m_type_converters.push_back(std::move(conv));
  109. }
  110. void add_equal_cmp(std::unique_ptr<type_comparator_base> cmp)
  111. {
  112. if (type_register::register_equal_comparator(cmp.get()))
  113. m_type_equal_cmps.push_back(std::move(cmp));
  114. }
  115. void add_less_than_cmp(std::unique_ptr<type_comparator_base> cmp)
  116. {
  117. if (type_register::register_less_than_comparator(cmp.get()))
  118. m_type_less_than_cmps.push_back(std::move(cmp));
  119. }
  120. void set_disable_unregister()
  121. {
  122. m_should_unregister = false;
  123. }
  124. void unregister()
  125. {
  126. if (!m_should_unregister)
  127. return;
  128. for (auto& prop : m_global_properties)
  129. type_register::unregister_global_property(prop.get());
  130. for (auto& meth : m_global_methods)
  131. type_register::unregister_global_method(meth.get());
  132. for (auto& enum_ : m_enumerations)
  133. type_register::unregister_enumeration(enum_.get());
  134. for (auto& item : m_type_converters)
  135. type_register::unregister_converter(item.get());
  136. for (auto& item : m_type_equal_cmps)
  137. type_register::unregister_equal_comparator(item.get());
  138. for (auto& item : m_type_less_than_cmps)
  139. type_register::unregister_less_than_comparator(item.get());
  140. for (auto& type : m_type_data_list)
  141. type_register::unregister_type(type.get());
  142. type_register::unregister_reg_manager(this);
  143. m_type_data_list.clear();
  144. m_constructors.clear();
  145. m_destructors.clear();
  146. m_properties.clear();
  147. m_global_properties.clear();
  148. m_methods.clear();
  149. m_global_methods.clear();
  150. m_enumerations.clear();
  151. m_type_converters.clear();
  152. m_type_equal_cmps.clear();
  153. m_type_less_than_cmps.clear();
  154. m_should_unregister = false;
  155. }
  156. // no copy, no assign
  157. registration_manager(const registration_manager&) = delete;
  158. registration_manager& operator=(const registration_manager&) = delete;
  159. private:
  160. bool m_should_unregister = true;
  161. std::vector<std::unique_ptr<type_data>> m_type_data_list;
  162. std::vector<std::unique_ptr<constructor_wrapper_base>> m_constructors;
  163. std::vector<std::unique_ptr<destructor_wrapper_base>> m_destructors;
  164. std::vector<std::unique_ptr<property_wrapper_base>> m_properties;
  165. std::vector<std::unique_ptr<property_wrapper_base>> m_global_properties;
  166. std::vector<std::unique_ptr<method_wrapper_base>> m_methods;
  167. std::vector<std::unique_ptr<method_wrapper_base>> m_global_methods;
  168. std::vector<std::unique_ptr<enumeration_wrapper_base>> m_enumerations;
  169. std::vector<std::unique_ptr<type_converter_base>> m_type_converters;
  170. std::vector<std::unique_ptr<type_comparator_base>> m_type_equal_cmps;
  171. std::vector<std::unique_ptr<type_comparator_base>> m_type_less_than_cmps;
  172. };
  173. /////////////////////////////////////////////////////////////////////////////////////////
  174. RTTR_LOCAL RTTR_INLINE registration_manager& get_registration_manager() RTTR_NOEXCEPT
  175. {
  176. static registration_manager obj;
  177. return obj;
  178. }
  179. /////////////////////////////////////////////////////////////////////////////////////////
  180. template<typename T>
  181. using is_global_item = std::integral_constant<bool, std::is_same<T, invalid_type>::value>;
  182. template<typename T, typename Item>
  183. RTTR_LOCAL RTTR_FORCE_INLINE enable_if_t<is_global_item<T>::value, void>
  184. store_item(Item item)
  185. {
  186. auto& obj = get_registration_manager();
  187. obj.add_global_item(std::move(item));
  188. }
  189. template<typename T, typename Item>
  190. RTTR_LOCAL RTTR_FORCE_INLINE enable_if_t<!is_global_item<T>::value, void>
  191. store_item(Item item)
  192. {
  193. auto& obj = get_registration_manager();
  194. obj.add_item(std::move(item));
  195. }
  196. /////////////////////////////////////////////////////////////////////////////////////////
  197. } // end namespace detail
  198. } // end namespace rttr
  199. #endif // RTTR_REGISTRATION_MANAGER_H_