BsRTTIReflectablePtrField.h 8.5 KB

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