BsRTTIReflectableField.h 7.5 KB

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