BsRTTIField.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #pragma once
  2. #include <string>
  3. #include <type_traits>
  4. #include "BsPrerequisitesUtil.h"
  5. #include "BsIReflectable.h"
  6. #include "BsManagedDataBlock.h"
  7. #include "BsException.h"
  8. #include "BsAny.h"
  9. namespace BansheeEngine
  10. {
  11. class RTTITypeBase;
  12. /** @cond INTERNAL */
  13. /** @addtogroup RTTI
  14. * @{
  15. */
  16. /**
  17. * Types of fields we can serialize:
  18. *
  19. * - Plain - Native data types, POD (Plain old data) structures, or in general types we don't want to (or can't) inherit from IReflectable.
  20. * Type must be copyable by memcpy.
  21. *
  22. * - DataBlock - Array of bytes of a certain size. When returning a data block you may specify if its managed or unmanaged.
  23. * Managed data blocks have their buffers deleted after they go out of scope. This is useful if you need to return some
  24. * temporary data. On the other hand if the data in the block belongs to your class, and isn't temporary, keep the data unmanaged.
  25. *
  26. * - Reflectable - Field that is of IReflectable type. Cannot be a pointer to IReflectable and must be actual value type.
  27. * Type and its fields are serialized recursively. Supports versioning so you may add/remove fields from the type
  28. * without breaking previously serialized data.
  29. *
  30. * - ReflectablePtr - A pointer to IReflectable. Same as "Reflectable" except that data isn't serialized as a value type,
  31. * but as a pointer, which may be referenced by multiple other instances. All references are saved upon
  32. * serialization and restored upon deserialization.
  33. */
  34. enum SerializableFieldType
  35. {
  36. SerializableFT_Plain,
  37. SerializableFT_DataBlock,
  38. SerializableFT_Reflectable,
  39. SerializableFT_ReflectablePtr
  40. };
  41. /** Various flags you can assign to RTTI fields. */
  42. enum RTTIFieldFlag
  43. {
  44. /** This flag is only used on field types of ReflectablePtr type, and it is used
  45. * to solve circular references. Circular references cause an issue when deserializing,
  46. * as the algorithm doesn't know which object to deserialize first. By making one of
  47. * the references weak, you tell the algorithm that it doesn't have to guarantee
  48. * the object will be fully deserialized before being assigned to the field.
  49. *
  50. * In short: If you make a reference weak, when "set" method of that field is called,
  51. * it is not guaranteed the value provided is fully initialized, so you should not access any of its
  52. * data until deserialization is fully complete. You only need to use this flag if the RTTI system
  53. * complains that is has found a circular reference.
  54. */
  55. RTTI_Flag_WeakRef = 0x01,
  56. /** This flags signals various systems that the flagged field should not be searched when looking for
  57. * object references. This normally means the value of this field will no be retrieved during reference
  58. * searches but it will likely still be retrieved during other operations (e.g. serialization).
  59. * This is used as an optimization to avoid retrieving values of potentially very expensive fields that
  60. * would not contribute to the reference search anyway. Whether or not a field contributes to the reference
  61. * search depends on the search and should be handled on a case by case basis.
  62. */
  63. RTTI_Flag_SkipInReferenceSearch = 0x02
  64. };
  65. /**
  66. * Structure that keeps meta-data concerning a single class field. You can use this data for setting and getting values
  67. * for that field on a specific class instance.
  68. *
  69. * Class also contains an unique field name, and an unique field ID. Fields may contain single types or an array of types.
  70. * See SerializableFieldType for information about specific field types.
  71. *
  72. * @note
  73. * Most of the methods for retrieving and setting data accept "void *" for both the data and the owning class instance.
  74. * It is up to the caller to ensure that pointer is of proper type.
  75. */
  76. struct BS_UTILITY_EXPORT RTTIField
  77. {
  78. Any valueGetter;
  79. Any valueSetter;
  80. Any arraySizeGetter;
  81. Any arraySizeSetter;
  82. String mName;
  83. UINT16 mUniqueId;
  84. bool mIsVectorType;
  85. SerializableFieldType mType;
  86. UINT64 mFlags;
  87. bool isPlainType() const { return mType == SerializableFT_Plain; }
  88. bool isDataBlockType() const { return mType == SerializableFT_DataBlock; }
  89. bool isReflectableType() const { return mType == SerializableFT_Reflectable; }
  90. bool isReflectablePtrType() const { return mType == SerializableFT_ReflectablePtr; }
  91. bool isArray() const { return mIsVectorType; }
  92. /** Returns flags that were set in the field meta-data. */
  93. UINT64 getFlags() const { return mFlags; }
  94. /**
  95. * Gets the size of an array contained by the field, if the field represents an array. Throws exception if field
  96. * is not an array.
  97. */
  98. virtual UINT32 getArraySize(void* object) = 0;
  99. /**
  100. * Changes the size of an array contained by the field, if the field represents an array. Throws exception if field
  101. * is not an array.
  102. */
  103. virtual void setArraySize(void* object, UINT32 size) = 0;
  104. /** Returns the type id for the type used in this field. */
  105. virtual UINT32 getTypeSize() = 0;
  106. /**
  107. * Query if the field has dynamic size.
  108. *
  109. * @note
  110. * Field should have dynamic size if:
  111. * - The field can have varying size
  112. * - The field size is over 255
  113. * @note
  114. * Types like integers, floats, bools, POD structs dont have dynamic size.
  115. * Types like strings, vectors, maps do.
  116. * @note
  117. * If your type has a static size but that size exceeds 255 bytes you also need to
  118. * use dynamic field size. (You will be warned during compilation if you don't follow this rule)
  119. */
  120. virtual bool hasDynamicSize() = 0;
  121. /**
  122. * Throws an exception if this field doesn't contain a plain value.
  123. *
  124. * @param[in] array If true then the field must support plain array type.
  125. */
  126. void checkIsPlain(bool array);
  127. /**
  128. * Throws an exception if this field doesn't contain a complex value.
  129. *
  130. * @param[in] array If true then the field must support complex array type.
  131. */
  132. void checkIsComplex(bool array);
  133. /**
  134. * Throws an exception if this field doesn't contain a complex pointer value.
  135. *
  136. * @param[in] array If true then the field must support complex pointer array type.
  137. */
  138. void checkIsComplexPtr(bool array);
  139. /**
  140. * Throws an exception depending if the field is or isn't an array.
  141. *
  142. * @param[in] array If true, then exception will be thrown if field is not an array.
  143. * If false, then it will be thrown if field is an array.
  144. */
  145. void checkIsArray(bool array);
  146. /** Throws an exception if this field doesn't contain a data block value. */
  147. void checkIsDataBlock();
  148. protected:
  149. void initAll(Any valueGetter, Any valueSetter, Any arraySizeGetter, Any arraySizeSetter,
  150. String mName, UINT16 mUniqueId, bool mIsVectorType, SerializableFieldType type, UINT64 flags)
  151. {
  152. this->valueGetter = valueGetter;
  153. this->valueSetter = valueSetter;
  154. this->arraySizeGetter = arraySizeGetter;
  155. this->arraySizeSetter = arraySizeSetter;
  156. this->mName = mName;
  157. this->mUniqueId = mUniqueId;
  158. this->mIsVectorType = mIsVectorType;
  159. this->mType = type;
  160. this->mFlags = flags;
  161. }
  162. };
  163. /** @} */
  164. /** @endcond */
  165. }