BsRTTIReflectablePtrField.h 8.9 KB

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