BsRTTIField.h 7.0 KB

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