BsRTTIField.h 7.6 KB

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