BsRTTIReflectablePtrField.h 8.7 KB

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