BsRTTIReflectableField.h 7.5 KB

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