BsRTTIReflectablePtrField.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "Prerequisites/BsPrerequisitesUtil.h"
  5. #include "Reflection/BsRTTIField.h"
  6. #include "Reflection/BsIReflectable.h"
  7. namespace bs
  8. {
  9. /** @addtogroup Internal-Utility
  10. * @{
  11. */
  12. /** @addtogroup RTTI-Internal
  13. * @{
  14. */
  15. /**
  16. * Base class containing common functionality for a reflectable pointer class field.
  17. *
  18. * @note
  19. * Reflectable fields are fields containing complex types deriving from IReflectable. They are serialized recursively
  20. * and you may add/remove fields from them without breaking the serialized data.
  21. * @note
  22. * ReflectablePtr fields are different from Reflectable fields because other types may reference the same Reflectable
  23. * object using a ReflectablePtr, while normal Reflectable fields are only referenced by a single field they're declared on.
  24. */
  25. struct RTTIReflectablePtrFieldBase : public RTTIField
  26. {
  27. /**
  28. * Retrieves the IReflectable value from the provided instance.
  29. *
  30. * @note Field type must not be an array.
  31. */
  32. virtual SPtr<IReflectable> getValue(void* object) = 0;
  33. /**
  34. * Retrieves the IReflectable value from an array on the provided instance and index.
  35. *
  36. * @note Field type must be an array.
  37. */
  38. virtual SPtr<IReflectable> getArrayValue(void* object, UINT32 index) = 0;
  39. /**
  40. * Sets the IReflectable value in the provided instance.
  41. *
  42. * @note Field type must not be an array.
  43. */
  44. virtual void setValue(void* object, SPtr<IReflectable> value) = 0;
  45. /**
  46. * Sets the IReflectable value in an array on the provided instance and index.
  47. *
  48. * @note Field type must be an array.
  49. */
  50. virtual void setArrayValue(void* object, UINT32 index, SPtr<IReflectable> value) = 0;
  51. /** Creates a new object of the field type. */
  52. virtual SPtr<IReflectable> newObject() = 0;
  53. /** Returns the RTTI identifier of the class owning the field. */
  54. virtual UINT32 getRTTIId() = 0;
  55. /** Returns the name of the class owning the field. */
  56. virtual const String& getRTTIName() = 0;
  57. /** @copydoc RTTIField::hasDynamicSize */
  58. bool hasDynamicSize() override { return true; }
  59. /** Retrieves the RTTI object for the type the field contains. */
  60. virtual RTTITypeBase* getType() = 0;
  61. };
  62. /** Reflectable field containing a pointer to a specific type with RTTI implemented. */
  63. template <class DataType, class ObjectType>
  64. struct RTTIReflectablePtrField : public RTTIReflectablePtrFieldBase
  65. {
  66. /**
  67. * Initializes a field pointing to a single data type implementing IReflectable interface.
  68. *
  69. * @param[in] name Name of the field.
  70. * @param[in] uniqueId Unique identifier for this field. Although name is also a unique identifier we want a
  71. * small data type that can be used for efficiently serializing data to disk and similar.
  72. * It is primarily used for compatibility between different versions of serialized data.
  73. * @param[in] getter The getter method for the field. Must be a specific signature: DataType*(ObjectType*)
  74. * @param[in] setter The setter method for the field. Must be a specific signature: void(ObjectType*, DataType*)
  75. * @param[in] flags Various flags you can use to specialize how systems handle this field. See "RTTIFieldFlag".
  76. */
  77. void initSingle(const String& name, UINT16 uniqueId, Any getter, Any setter, UINT64 flags)
  78. {
  79. initAll(getter, setter, nullptr, nullptr, name, uniqueId, false, SerializableFT_ReflectablePtr, flags);
  80. }
  81. /**
  82. * Initializes a field containing an array of pointers to data types implementing IReflectable interface.
  83. *
  84. * @param[in] name Name of the field.
  85. * @param[in] uniqueId Unique identifier for this field. Although name is also a unique identifier we want a
  86. * small data type that can be used for efficiently serializing data to disk and similar.
  87. * It is primarily used for compatibility between different versions of serialized data.
  88. * @param[in] getter The getter method for the field. Must be a specific signature: DataType*(ObjectType*, UINT32)
  89. * @param[in] getSize Getter method that returns the size of an array. Must be a specific signature: UINT32(ObjectType*)
  90. * @param[in] setter The setter method for the field. Must be a specific signature: void(ObjectType*, UINT32, DataType*)
  91. * @param[in] setSize Setter method that allows you to resize an array. Can be null. Must be a specific signature: void(ObjectType*, UINT32)
  92. * @param[in] flags Various flags you can use to specialize how systems handle this field. See "RTTIFieldFlag".
  93. */
  94. void initArray(const String& name, UINT16 uniqueId, Any getter,
  95. Any getSize, Any setter, Any setSize, UINT64 flags)
  96. {
  97. initAll(getter, setter, getSize, setSize, name, uniqueId, true, SerializableFT_ReflectablePtr, flags);
  98. }
  99. /** @copydoc RTTIField::getTypeSize */
  100. UINT32 getTypeSize() override
  101. {
  102. return 0; // Complex types don't store size the conventional way
  103. }
  104. /** @copydoc RTTIReflectablePtrFieldBase::getValue */
  105. SPtr<IReflectable> getValue(void* object) override
  106. {
  107. checkIsArray(false);
  108. ObjectType* castObjType = static_cast<ObjectType*>(object);
  109. std::function<SPtr<DataType>(ObjectType*)> f = any_cast<std::function<SPtr<DataType>(ObjectType*)>>(valueGetter);
  110. SPtr<IReflectable> castDataType = f(castObjType);
  111. return castDataType;
  112. }
  113. /** @copydoc RTTIReflectablePtrFieldBase::getArrayValue */
  114. SPtr<IReflectable> getArrayValue(void* object, UINT32 index) override
  115. {
  116. checkIsArray(true);
  117. ObjectType* castObjType = static_cast<ObjectType*>(object);
  118. std::function<SPtr<DataType>(ObjectType*, UINT32)> f = any_cast<std::function<SPtr<DataType>(ObjectType*, UINT32)>>(valueGetter);
  119. SPtr<IReflectable> castDataType = f(castObjType, index);
  120. return castDataType;
  121. }
  122. /** @copydoc RTTIReflectablePtrFieldBase::setValue */
  123. void setValue(void* object, SPtr<IReflectable> value) override
  124. {
  125. checkIsArray(false);
  126. if(valueSetter.empty())
  127. {
  128. BS_EXCEPT(InternalErrorException,
  129. "Specified field (" + mName + ") has no setter.");
  130. }
  131. ObjectType* castObjType = static_cast<ObjectType*>(object);
  132. SPtr<DataType> castDataObj = std::static_pointer_cast<DataType>(value);
  133. std::function<void(ObjectType*, SPtr<DataType>)> f = any_cast<std::function<void(ObjectType*, SPtr<DataType>)>>(valueSetter);
  134. f(castObjType, castDataObj);
  135. }
  136. /** @copydoc RTTIReflectablePtrFieldBase::setArrayValue */
  137. void setArrayValue(void* object, UINT32 index, SPtr<IReflectable> value) override
  138. {
  139. checkIsArray(true);
  140. if(valueSetter.empty())
  141. {
  142. BS_EXCEPT(InternalErrorException,
  143. "Specified field (" + mName + ") has no setter.");
  144. }
  145. ObjectType* castObjType = static_cast<ObjectType*>(object);
  146. SPtr<DataType> castDataObj = std::static_pointer_cast<DataType>(value);
  147. std::function<void(ObjectType*, UINT32, SPtr<DataType>)> f = any_cast<std::function<void(ObjectType*, UINT32, SPtr<DataType>)>>(valueSetter);
  148. f(castObjType, index, castDataObj);
  149. }
  150. /** @copydoc RTTIField::setArraySize */
  151. UINT32 getArraySize(void* object) override
  152. {
  153. checkIsArray(true);
  154. std::function<UINT32(ObjectType*)> f = any_cast<std::function<UINT32(ObjectType*)>>(arraySizeGetter);
  155. ObjectType* castObject = static_cast<ObjectType*>(object);
  156. return f(castObject);
  157. }
  158. /** @copydoc RTTIField::setArraySize */
  159. void setArraySize(void* object, UINT32 size) override
  160. {
  161. checkIsArray(true);
  162. if(arraySizeSetter.empty())
  163. {
  164. BS_EXCEPT(InternalErrorException,
  165. "Specified field (" + mName + ") has no array size setter.");
  166. }
  167. std::function<void(ObjectType*, UINT32)> f = any_cast<std::function<void(ObjectType*, UINT32)>>(arraySizeSetter);
  168. ObjectType* castObject = static_cast<ObjectType*>(object);
  169. f(castObject, size);
  170. }
  171. /** @copydoc RTTIReflectablePtrFieldBase::newObject */
  172. SPtr<IReflectable> newObject() override
  173. {
  174. return SPtr<IReflectable>(DataType::getRTTIStatic()->newRTTIObject());
  175. }
  176. /** @copydoc RTTIReflectablePtrFieldBase::getRTTIId */
  177. UINT32 getRTTIId() override
  178. {
  179. return DataType::getRTTIStatic()->getRTTIId();
  180. }
  181. /** @copydoc RTTIReflectablePtrFieldBase::getRTTIName */
  182. const String& getRTTIName() override
  183. {
  184. return DataType::getRTTIStatic()->getRTTIName();
  185. }
  186. /** @copydoc RTTIReflectablePtrFieldBase::getType */
  187. RTTITypeBase* getType() override
  188. {
  189. return DataType::getRTTIStatic();
  190. }
  191. };
  192. /** @} */
  193. /** @} */
  194. }