BsRTTIReflectablePtrField.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. /**
  64. * @brief Class containing a reflectable pointer field containing a specific type.
  65. */
  66. template <class DataType, class ObjectType>
  67. struct RTTIReflectablePtrField : public RTTIReflectablePtrFieldBase
  68. {
  69. /**
  70. * @brief Initializes a field pointing to a single data type implementing IReflectable interface.
  71. *
  72. * @param name Name of the field.
  73. * @param uniqueId Unique identifier for this field. Although name is also a unique
  74. * identifier we want a small data type that can be used for efficiently
  75. * serializing data to disk and similar. It is primarily used for compatibility
  76. * between different versions of serialized data.
  77. * @param getter The getter method for the field. Must be a specific signature: DataType*(ObjectType*)
  78. * @param setter The setter method for the field. Must be a specific signature: void(ObjectType*, DataType*)
  79. * @param flags Various flags you can use to specialize how systems handle this field. See "RTTIFieldFlag".
  80. */
  81. void initSingle(const String& name, UINT16 uniqueId, Any getter, Any setter, UINT64 flags)
  82. {
  83. initAll(getter, setter, nullptr, nullptr, name, uniqueId, false, SerializableFT_ReflectablePtr, flags);
  84. }
  85. /**
  86. * @brief Initializes a field containing an array of pointers to data types implementing IReflectable interface.
  87. *
  88. * @param name Name of the field.
  89. * @param uniqueId Unique identifier for this field. Although name is also a unique
  90. * identifier we want a small data type that can be used for efficiently
  91. * serializing data to disk and similar. It is primarily used for compatibility
  92. * between different versions of serialized data.
  93. * @param getter The getter method for the field. Must be a specific signature: DataType*(ObjectType*, UINT32)
  94. * @param getSize Getter method that returns the size of an array. Must be a specific signature: UINT32(ObjectType*)
  95. * @param setter The setter method for the field. Must be a specific signature: void(ObjectType*, UINT32, DataType*)
  96. * @param setSize Setter method that allows you to resize an array. Can be null. Must be a specific signature: void(ObjectType*, UINT32)
  97. * @param flags Various flags you can use to specialize how systems handle this field. See "RTTIFieldFlag".
  98. */
  99. void initArray(const String& name, UINT16 uniqueId, Any getter,
  100. Any getSize, Any setter, Any setSize, UINT64 flags)
  101. {
  102. initAll(getter, setter, getSize, setSize, name, uniqueId, true, SerializableFT_ReflectablePtr, flags);
  103. }
  104. /**
  105. * @copydoc RTTIField::getTypeSize
  106. */
  107. virtual UINT32 getTypeSize()
  108. {
  109. return 0; // Complex types don't store size the conventional way
  110. }
  111. /**
  112. * @copydoc RTTIReflectablePtrFieldBase::getValue
  113. */
  114. virtual std::shared_ptr<IReflectable> getValue(void* object)
  115. {
  116. checkIsArray(false);
  117. ObjectType* castObjType = static_cast<ObjectType*>(object);
  118. std::function<std::shared_ptr<DataType>(ObjectType*)> f = any_cast<std::function<std::shared_ptr<DataType>(ObjectType*)>>(valueGetter);
  119. std::shared_ptr<IReflectable> castDataType = f(castObjType);
  120. return castDataType;
  121. }
  122. /**
  123. * @copydoc RTTIReflectablePtrFieldBase::getArrayValue
  124. */
  125. virtual std::shared_ptr<IReflectable> getArrayValue(void* object, UINT32 index)
  126. {
  127. checkIsArray(true);
  128. ObjectType* castObjType = static_cast<ObjectType*>(object);
  129. std::function<std::shared_ptr<DataType>(ObjectType*, UINT32)> f = any_cast<std::function<std::shared_ptr<DataType>(ObjectType*, UINT32)>>(valueGetter);
  130. std::shared_ptr<IReflectable> castDataType = f(castObjType, index);
  131. return castDataType;
  132. }
  133. /**
  134. * @copydoc RTTIReflectablePtrFieldBase::setValue
  135. */
  136. virtual void setValue(void* object, std::shared_ptr<IReflectable> value)
  137. {
  138. checkIsArray(false);
  139. if(valueSetter.empty())
  140. {
  141. BS_EXCEPT(InternalErrorException,
  142. "Specified field (" + mName + ") has no setter.");
  143. }
  144. ObjectType* castObjType = static_cast<ObjectType*>(object);
  145. std::shared_ptr<DataType> castDataObj = std::static_pointer_cast<DataType>(value);
  146. std::function<void(ObjectType*, std::shared_ptr<DataType>)> f = any_cast<std::function<void(ObjectType*, std::shared_ptr<DataType>)>>(valueSetter);
  147. f(castObjType, castDataObj);
  148. }
  149. /**
  150. * @copydoc RTTIReflectablePtrFieldBase::setArrayValue
  151. */
  152. virtual void setArrayValue(void* object, UINT32 index, std::shared_ptr<IReflectable> value)
  153. {
  154. checkIsArray(true);
  155. if(valueSetter.empty())
  156. {
  157. BS_EXCEPT(InternalErrorException,
  158. "Specified field (" + mName + ") has no setter.");
  159. }
  160. ObjectType* castObjType = static_cast<ObjectType*>(object);
  161. std::shared_ptr<DataType> castDataObj = std::static_pointer_cast<DataType>(value);
  162. std::function<void(ObjectType*, UINT32, std::shared_ptr<DataType>)> f = any_cast<std::function<void(ObjectType*, UINT32, std::shared_ptr<DataType>)>>(valueSetter);
  163. f(castObjType, index, castDataObj);
  164. }
  165. /**
  166. * @copydoc RTTIField::setArraySize
  167. */
  168. virtual UINT32 getArraySize(void* object)
  169. {
  170. checkIsArray(true);
  171. std::function<UINT32(ObjectType*)> f = any_cast<std::function<UINT32(ObjectType*)>>(arraySizeGetter);
  172. ObjectType* castObject = static_cast<ObjectType*>(object);
  173. return f(castObject);
  174. }
  175. /**
  176. * @copydoc RTTIField::setArraySize
  177. */
  178. virtual void setArraySize(void* object, UINT32 size)
  179. {
  180. checkIsArray(true);
  181. if(arraySizeSetter.empty())
  182. {
  183. BS_EXCEPT(InternalErrorException,
  184. "Specified field (" + mName + ") has no array size setter.");
  185. }
  186. std::function<void(ObjectType*, UINT32)> f = any_cast<std::function<void(ObjectType*, UINT32)>>(arraySizeSetter);
  187. ObjectType* castObject = static_cast<ObjectType*>(object);
  188. f(castObject, size);
  189. }
  190. /**
  191. * @copydoc RTTIReflectablePtrFieldBase::newObject
  192. */
  193. virtual std::shared_ptr<IReflectable> newObject()
  194. {
  195. return std::shared_ptr<IReflectable>(DataType::getRTTIStatic()->newRTTIObject());
  196. }
  197. /**
  198. * @copydoc RTTIReflectablePtrFieldBase::getRTTIId
  199. */
  200. virtual UINT32 getRTTIId()
  201. {
  202. return DataType::getRTTIStatic()->getRTTIId();
  203. }
  204. /**
  205. * @copydoc RTTIReflectablePtrFieldBase::getRTTIName
  206. */
  207. virtual const String& getRTTIName()
  208. {
  209. return DataType::getRTTIStatic()->getRTTIName();
  210. }
  211. };
  212. }