BsRTTIReflectableField.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 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. struct RTTIReflectableFieldBase : public RTTIField
  15. {
  16. /**
  17. * @brief Retrieves the IReflectable value from the provided instance.
  18. *
  19. * @note Field type must not be an array.
  20. */
  21. virtual IReflectable& getValue(void* object) = 0;
  22. /**
  23. * @brief Retrieves the IReflectable value from an array on the provided instance
  24. * and index.
  25. *
  26. * @note Field type must be an array.
  27. */
  28. virtual IReflectable& getArrayValue(void* object, UINT32 index) = 0;
  29. /**
  30. * @brief Sets the IReflectable value in the provided instance.
  31. *
  32. * @note Field type must not be an array.
  33. */
  34. virtual void setValue(void* object, IReflectable& value) = 0;
  35. /**
  36. * @brief Sets the IReflectable value in an array on the provided instance
  37. * and index.
  38. *
  39. * @note Field type must be an array.
  40. */
  41. virtual void setArrayValue(void* object, UINT32 index, IReflectable& value) = 0;
  42. /**
  43. * @brief Creates a new object of the field type.
  44. */
  45. virtual std::shared_ptr<IReflectable> newObject() = 0;
  46. /**
  47. * @copydoc RTTIField::hasDynamicSize
  48. */
  49. virtual bool hasDynamicSize() { return true; }
  50. /**
  51. * @brief Retrieves the RTTI object for the type the field contains.
  52. */
  53. virtual RTTITypeBase* getType() = 0;
  54. };
  55. /**
  56. * @brief Class containing a reflectable field containing a specific type.
  57. */
  58. template <class DataType, class ObjectType>
  59. struct RTTIReflectableField : public RTTIReflectableFieldBase
  60. {
  61. /**
  62. * @brief Initializes a field containing a single data type implementing IReflectable interface.
  63. *
  64. * @param name Name of the field.
  65. * @param uniqueId Unique identifier for this field. Although name is also a unique
  66. * identifier we want a small data type that can be used for efficiently
  67. * serializing data to disk and similar. It is primarily used for compatibility
  68. * between different versions of serialized data.
  69. * @param getter The getter method for the field. Must be a specific signature: DataType&(ObjectType*)
  70. * @param setter The setter method for the field. Must be a specific signature: void(ObjectType*, DataType)
  71. * @param 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_Reflectable, flags);
  76. }
  77. /**
  78. * @brief Initializes a field containing an array of data types implementing IReflectable interface.
  79. *
  80. * @param name Name of the field.
  81. * @param uniqueId Unique identifier for this field. Although name is also a unique
  82. * identifier we want a small data type that can be used for efficiently
  83. * serializing data to disk and similar. It is primarily used for compatibility
  84. * between different versions of serialized data.
  85. * @param getter The getter method for the field. Must be a specific signature: DataType&(ObjectType*, UINT32)
  86. * @param getSize Getter method that returns the size of an array. Must be a specific signature: UINT32(ObjectType*)
  87. * @param setter The setter method for the field. Must be a specific signature: void(ObjectType*, UINT32, DataType)
  88. * @param setSize Setter method that allows you to resize an array. Must be a specific signature: void(ObjectType*, UINT32)
  89. * @param flags Various flags you can use to specialize how systems handle this field. See "RTTIFieldFlag".
  90. */
  91. void initArray(const String& name, UINT16 uniqueId, Any getter,
  92. Any getSize, Any setter, Any setSize, UINT64 flags)
  93. {
  94. initAll(getter, setter, getSize, setSize, name, uniqueId, true, SerializableFT_Reflectable, flags);
  95. }
  96. /**
  97. * @copydoc RTTIField::getTypeSize
  98. */
  99. virtual UINT32 getTypeSize()
  100. {
  101. return 0; // Complex types don't store size the conventional way
  102. }
  103. /**
  104. * @copydoc RTTIReflectableFieldBase::getValue
  105. */
  106. virtual IReflectable& getValue(void* object)
  107. {
  108. checkIsArray(false);
  109. ObjectType* castObjType = static_cast<ObjectType*>(object);
  110. std::function<DataType&(ObjectType*)> f = any_cast<std::function<DataType&(ObjectType*)>>(valueGetter);
  111. IReflectable& castDataType = f(castObjType);
  112. return castDataType;
  113. }
  114. /**
  115. * @copydoc RTTIReflectableFieldBase::getArrayValue
  116. */
  117. virtual IReflectable& getArrayValue(void* object, UINT32 index)
  118. {
  119. checkIsArray(true);
  120. ObjectType* castObjType = static_cast<ObjectType*>(object);
  121. std::function<DataType&(ObjectType*, UINT32)> f = any_cast<std::function<DataType&(ObjectType*, UINT32)>>(valueGetter);
  122. IReflectable& castDataType = f(castObjType, index);
  123. return castDataType;
  124. }
  125. /**
  126. * @copydoc RTTIReflectableFieldBase::setValue
  127. */
  128. virtual void setValue(void* object, IReflectable& value)
  129. {
  130. checkIsArray(false);
  131. if(valueSetter.empty())
  132. {
  133. BS_EXCEPT(InternalErrorException,
  134. "Specified field (" + mName + ") has no setter.");
  135. }
  136. ObjectType* castObjType = static_cast<ObjectType*>(object);
  137. DataType& castDataObj = static_cast<DataType&>(value);
  138. std::function<void(ObjectType*, DataType&)> f = any_cast<std::function<void(ObjectType*, DataType&)>>(valueSetter);
  139. f(castObjType, castDataObj);
  140. }
  141. /**
  142. * @copydoc RTTIReflectableFieldBase::setArrayValue
  143. */
  144. virtual void setArrayValue(void* object, UINT32 index, IReflectable& value)
  145. {
  146. checkIsArray(true);
  147. if(valueSetter.empty())
  148. {
  149. BS_EXCEPT(InternalErrorException,
  150. "Specified field (" + mName + ") has no setter.");
  151. }
  152. ObjectType* castObjType = static_cast<ObjectType*>(object);
  153. DataType& castDataObj = static_cast<DataType&>(value);
  154. std::function<void(ObjectType*, UINT32, DataType&)> f = any_cast<std::function<void(ObjectType*, UINT32, DataType&)>>(valueSetter);
  155. f(castObjType, index, castDataObj);
  156. }
  157. /**
  158. * @copydoc RTTIField::getArraySize
  159. */
  160. virtual UINT32 getArraySize(void* object)
  161. {
  162. checkIsArray(true);
  163. std::function<UINT32(ObjectType*)> f = any_cast<std::function<UINT32(ObjectType*)>>(arraySizeGetter);
  164. ObjectType* castObject = static_cast<ObjectType*>(object);
  165. return f(castObject);
  166. }
  167. /**
  168. * @copydoc RTTIField::setArraySize
  169. */
  170. virtual void setArraySize(void* object, UINT32 size)
  171. {
  172. checkIsArray(true);
  173. if(arraySizeSetter.empty())
  174. {
  175. BS_EXCEPT(InternalErrorException,
  176. "Specified field (" + mName + ") has no array size setter.");
  177. }
  178. std::function<void(ObjectType*, UINT32)> f = any_cast<std::function<void(ObjectType*, UINT32)>>(arraySizeSetter);
  179. ObjectType* castObject = static_cast<ObjectType*>(object);
  180. f(castObject, size);
  181. }
  182. /**
  183. * @copydoc RTTIReflectableFieldBase::newObject
  184. */
  185. virtual std::shared_ptr<IReflectable> newObject()
  186. {
  187. return DataType::getRTTIStatic()->newRTTIObject();
  188. }
  189. /**
  190. * @copydoc RTTIReflectableFieldBase::getType
  191. */
  192. virtual RTTITypeBase* getType()
  193. {
  194. return DataType::getRTTIStatic();
  195. }
  196. };
  197. }