Traits.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019-2023 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #ifndef RMLUI_CORE_TRAITS_H
  29. #define RMLUI_CORE_TRAITS_H
  30. #include "../Config/Config.h"
  31. #include "Debug.h"
  32. #include "Header.h"
  33. #include <type_traits>
  34. namespace Rml {
  35. class RMLUICORE_API NonCopyMoveable {
  36. public:
  37. NonCopyMoveable() {}
  38. ~NonCopyMoveable() {}
  39. NonCopyMoveable(const NonCopyMoveable&) = delete;
  40. NonCopyMoveable& operator=(const NonCopyMoveable&) = delete;
  41. NonCopyMoveable(NonCopyMoveable&&) = delete;
  42. NonCopyMoveable& operator=(NonCopyMoveable&&) = delete;
  43. };
  44. class ReleaserBase;
  45. class RMLUICORE_API Releasable : public NonCopyMoveable {
  46. protected:
  47. virtual ~Releasable() = default;
  48. virtual void Release() = 0;
  49. friend class Rml::ReleaserBase;
  50. };
  51. class RMLUICORE_API ReleaserBase {
  52. protected:
  53. void Release(Releasable* target) const { target->Release(); }
  54. };
  55. template <typename T>
  56. class RMLUICORE_API Releaser : public ReleaserBase {
  57. public:
  58. void operator()(T* target) const
  59. {
  60. static_assert(std::is_base_of<Releasable, T>::value, "Rml::Releaser can only operate with classes derived from ::Rml::Releasable.");
  61. Release(static_cast<Releasable*>(target));
  62. }
  63. };
  64. enum class FamilyId : int {};
  65. class RMLUICORE_API FamilyBase {
  66. protected:
  67. static int GetNewId();
  68. };
  69. template <typename T>
  70. class Family : FamilyBase {
  71. public:
  72. // Get a unique ID for a given type.
  73. // Note: An ID for a given type may not match across DLL-boundaries.
  74. static FamilyId Id()
  75. {
  76. static int id = GetNewId();
  77. return static_cast<FamilyId>(id);
  78. }
  79. };
  80. } // namespace Rml
  81. #ifdef RMLUI_CUSTOM_RTTI
  82. #define RMLUI_RTTI_Define(_NAME_) \
  83. using RttiClassType = _NAME_; \
  84. static void* GetStaticClassIdentifier() \
  85. { \
  86. static int dummy; \
  87. return &dummy; \
  88. } \
  89. virtual bool IsClass(void* type_identifier) const \
  90. { \
  91. return type_identifier == GetStaticClassIdentifier(); \
  92. }
  93. #define RMLUI_RTTI_DefineWithParent(_NAME_, _PARENT_) \
  94. using RttiClassType = _NAME_; \
  95. static void* GetStaticClassIdentifier() \
  96. { \
  97. static int dummy; \
  98. return &dummy; \
  99. } \
  100. bool IsClass(void* type_identifier) const override \
  101. { \
  102. static_assert(std::is_same<typename _PARENT_::RttiClassType, _PARENT_>::value, \
  103. "Parent does not implement RMLUI_RTTI_Define or RMLUI_RTTI_DefineWithParent"); \
  104. return type_identifier == GetStaticClassIdentifier() || _PARENT_::IsClass(type_identifier); \
  105. }
  106. template <class Derived, class Base>
  107. Derived rmlui_dynamic_cast(Base base_instance)
  108. {
  109. static_assert(std::is_pointer<Derived>::value && std::is_pointer<Base>::value, "rmlui_dynamic_cast can only cast pointer types");
  110. using T_Derived = typename std::remove_cv<typename std::remove_pointer<Derived>::type>::type;
  111. static_assert(std::is_same<typename T_Derived::RttiClassType, T_Derived>::value, "Derived type does not implement RMLUI_RTTI_DefineWithParent");
  112. if (base_instance->IsClass(T_Derived::GetStaticClassIdentifier()))
  113. return static_cast<Derived>(base_instance);
  114. else
  115. return nullptr;
  116. }
  117. template <class Derived, class Base>
  118. Derived rmlui_static_cast(Base base_instance)
  119. {
  120. static_assert(std::is_pointer<Derived>::value && std::is_pointer<Base>::value, "rmlui_static_cast can only cast pointer types");
  121. return static_cast<Derived>(base_instance);
  122. }
  123. template <class T>
  124. const char* rmlui_type_name(const T& /*var*/)
  125. {
  126. return "(type name unavailable)";
  127. }
  128. template <class T>
  129. const char* rmlui_type_name()
  130. {
  131. return "(type name unavailable)";
  132. }
  133. #else
  134. #include <typeinfo>
  135. #define RMLUI_RTTI_Define(_NAME_)
  136. #define RMLUI_RTTI_DefineWithParent(_NAME_, _PARENT_)
  137. template <class Derived, class Base>
  138. Derived rmlui_dynamic_cast(Base base_instance)
  139. {
  140. static_assert(std::is_pointer<Derived>::value && std::is_pointer<Base>::value, "rmlui_dynamic_cast can only cast pointer types");
  141. return dynamic_cast<Derived>(base_instance);
  142. }
  143. template <class Derived, class Base>
  144. Derived rmlui_static_cast(Base base_instance)
  145. {
  146. static_assert(std::is_pointer<Derived>::value && std::is_pointer<Base>::value, "rmlui_static_cast can only cast pointer types");
  147. RMLUI_ASSERT(dynamic_cast<Derived>(base_instance));
  148. return static_cast<Derived>(base_instance);
  149. }
  150. template <class T>
  151. const char* rmlui_type_name(const T& var)
  152. {
  153. return typeid(var).name();
  154. }
  155. template <class T>
  156. const char* rmlui_type_name()
  157. {
  158. return typeid(T).name();
  159. }
  160. #endif // RMLUI_CUSTOM_RTTI
  161. #endif // RMLUI_CORE_TRAITS_H